简体   繁体   English

客户端导出excel

[英]excel export in client side

In my js file I would like to use ExcelPlus 2.4 js for exporting json data to excel file.在我的 js 文件中,我想使用 ExcelPlus 2.4 js 将 json 数据导出到 excel 文件。 Checked here http://aymkdn.github.io/ExcelPlus/ .在这里检查http://aymkdn.github.io/ExcelPlus/ Please anyone help me to write ExcelPlus 2.4 js syntax for exporting data in json format from my ajax call success part to my excel.请任何人帮助我编写 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");
            }
        });
    }

Please help me to find a solution for exporting json data to excel (xls/xlsx) file.请帮我找到将 json 数据导出到 excel (xls/xlsx) 文件的解决方案。 Please suggest me some working javascript/jquery plugin which has all browser support.请向我推荐一些具有所有浏览器支持的有效 javascript/jquery 插件。

try this尝试这个

 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);
 }

You could use ExcellentExport.js library.您可以使用ExcellentExport.js 库。 It helps to create an XLS, XLSX or CSV file in the client side (browser) getting data from an HTML table or a JavaScript array.它有助于在客户端(浏览器)中创建 XLS、XLSX 或 CSV 文件,以从 HTML 表或 JavaScript 数组中获取数据。

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

Sample code:示例代码:

<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>

Note: I am the developer of ExcellentExport.js注:我是ExcellentExport.js的开发者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM