客户端的excel导出

时间:2016-12-07 07:13:13

标签: javascript json excel

在我的js文件中,我想使用ExcelPlus 2.4 js将json数据导出到excel文件。点击此处http://aymkdn.github.io/ExcelPlus/。请有人帮我编写ExcelPlus 2.4 js语法,将json格式的数据从我的ajax调用成功部分导出到我的excel。

function GetExcel() {
        $.ajax({
            type: "POST",
            url: "../TestHandler.ashx",
            contentType: "application/json; charset=utf-8",
            dataType: "json",        
            success: function (data) {
                debugger;

                ***var ep = new ExcelPlus();
                ep.createFile("Book1")
                  .write({ "content": data })**
                  .createSheet("Book2")
                  .write({ "cell": "A1", "content": "A1" })
                  .write({ "sheet": "Book1", "cell": "D1", "content": new Date() })
                  .saveAs("demo.xlsx");*


                alert('success');
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert("error");
            }
        });
    }

请帮我找一个将json数据导出到excel(xls / xlsx)文件的解决方案。请推荐一些有javascript / jquery插件的浏览器支持。

2 个答案:

答案 0 :(得分:0)

试试这个

 function export()
 {
   var tab_text = "";
   var textRange; var j = 0;
   tab = document.getElementById('your table id '); // id of table
   for (j = 0; j < tab.rows.length; j++) {
      tab_text = tab_text + tab.rows[j].innerHTML + "</tr>";       
    }
    tab_text = tab_text + "</table>";
    tab_text = tab_text.replace(/<A[^>]*>|<\/A>/g, ""); 
    tab_text = tab_text.replace(/<img[^>]*>/gi, ""); 
    tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); 
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");
    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))            // If Internet Explorer
   {
    txtArea1.document.open("txt/html", "replace");
    txtArea1.document.write(tab_text);
    txtArea1.document.close();
    txtArea1.focus();
      sa = txtArea1.document.execCommand("SaveAs", true, "print.xls");
   }
  else                 //other browser not tested on IE 11
      sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
    return (sa);
 }

答案 1 :(得分:0)

您可以使用ExcellentExport.js库。它有助于在客户端(浏览器)中创建XLS,XLSX或CSV文件,以从HTML表或JavaScript数组中获取数据。

https://github.com/jmaister/excellentexport/

示例代码:

<table id="datatable">
    <tr>
        <td>100</td> <td>200</td> <td>300</td>
    </tr>
    <tr>
        <td>400</td> <td>500</td> <td>600</td>
    </tr>
</table>

<a download="somedata.xlsx" href="#" onclick="return ExcellentExport.convert({ anchor: this, filename: 'data_123.array', format: 'xlsx'},[{name: 'Sheet Name Here 1', from: {table: 'datatable'}}]);">Export to CSV</a>

注意:我是ExcellentExport.js的开发人员