设置要从html表

时间:2015-07-29 08:06:52

标签: javascript c# asp.net excel

我有一个html表。现在我将此表导出到Excel Sheet 使用Javascript。当这个excel表被下载时, 文件名就像" download.xls"。现在我想将filename作为current_date 而不是"下载" ...下面是我的javascript代码:

function write_to_excel() {
   var tab_text = "<table border='2px'><tr>";
   var textRange; var j = 0;
   tab = document.getElementById('tt1'); // id of table
   tab1 = document.getElementById('testTable'); // id of table

   for (i = 0; i < tab.rows.length; i++) {
   tab_text = tab_text + tab.rows[i].innerHTML + "</tr>";
   //tab_text=tab_text+"</tr>";
   }
   for (j = 0; j < tab1.rows.length; j++) {
    tab_text = tab_text + tab1.rows[j].innerHTML + "</tr>";
    //tab_text=tab_text+"</tr>";
   }
   tab_text = tab_text + "</table>";
   tab_text = tab_text.replace(/<A[^>]*>|<\/A>/g, ""); //remove if u want links in your table
   tab_text = tab_text.replace(/<img[^>]*>/gi, ""); // remove if u want images in your table
   tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves     input params
  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, "Say Thanks to Sumit.xls");
   }
   else //other browser not tested on IE 11
   sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
   return (sa);
 }

This event is fired on a button click event. Its very important for me.
Thanks in advance.

2 个答案:

答案 0 :(得分:1)

function tableToExcel (table, name) {

            var uri = 'data:application/vnd.ms-excel;base64,'
                ,
                template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
                , base64 = function (s) {
                    return window.btoa(unescape(encodeURIComponent(s)))
                }
                , format = function (s, c) {
                    return s.replace(/{(\w+)}/g, function (m, p) {
                        return c[p];
                    })
                }
            if (!table.nodeType) table = document.getElementById(table)
            var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
            var a = document.createElement('a');
            a.href = uri + base64(format(template, ctx))
            a.download = name+'.xls';
            //triggering the function
            a.click();
        }

答案 1 :(得分:0)