2013-03-22 220 views
4

我想在用戶點擊按鈕時生成Excel表格。基本上我想要做的正是在這裏dicussed使用jquery從html表生成Excel表格

how to pass html table values to excel sheet cells (卡勒H.Väravas答案)

JavaScript to export HTML tables to Excel

但不知何故,當我點擊按鈕沒有任何反應。我正在使用Mozilla瀏覽器。我的代碼需要啓用ActiveXObject。我需要做些額外的事情來完成它。 ?

我創建了這個小提琴進行測試。如果這個工程,我會嘗試我的真實代碼。如果這對你有用,那麼請讓我知道。由於

jFiddle

代碼:

JS:

<script type="text/javascript"> 
    function CreateExcelSheet() { 
     var i, j, str, 
       myTable = document.getElementById('mytable'), 
       rowCount = myTable.rows.length, 
       excel = new ActiveXObject('Excel.Application');// Activates Excel 
     excel.Workbooks.Add(); // Opens a new Workbook 
     excel.Application.Visible = true; // Shows Excel on the screen 
     for (i = 0; i < rowCount; i++) { 
      for (j = 0; j < myTable.rows[i].cells.length; j++) { 
       str = myTable.rows[i].cells[j].innerText; 
       excel.ActiveSheet.Cells(i + 1, j + 1).Value = str; // Writes to the sheet 
      } 
     } 
     return; 
    } 
</script> 

HTML:

<table id ="myTable" > 
      <tr> 
       <td>1</td> 
       <td>Jan</td> 
       <td>01/04/2012</td> 
       <td>Approved</td> 
      </tr> 
      <tr> 
       <td>2</td> 
       <td>Feb</td> 
       <td>01/04/2012</td> 
       <td>Approved</td> 
      </tr> 
     </table> 
     <form> 
      <input type="button" onclick="CreateExcelSheet();" value="Create Excel Sheet" > 
     </form> 
+4

ActiveXObjects是Internet Explorer中只,遠我所知。 – hexblot 2013-03-22 09:44:53

+0

您可以從服務器端創建excel。你使用什麼服務器端語言? – Sharun 2013-03-22 09:50:51

+0

@hexblot我想你是對的。我的朋友只是指出了我。我如何才能在所有的Web瀏覽器上工作? – rockstar 2013-03-22 09:55:24

回答

16

下面是答案:Export dynamic html table to excel in javascript in firefox browser通過insin

var tableToExcel = (function() { 
     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"><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]; }) } 
     return function(table, name) { 
     if (!table.nodeType) table = document.getElementById(table) 
     var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML} 
     window.location.href = uri + base64(format(template, ctx)) 
     } 
    })() 
+0

它在Mozilla中工作正常,但在IE中不起作用任何想法 – Alz 2015-02-04 05:09:29

11

要回答有關IE的問題(由於聲譽重新定義,我無法在評論框中回答),Sharun發佈的上述代碼將無法在IE 10+('permission denied'錯誤)上運行。這裏有一個片段,將在IE瀏覽器10+與舊版本的IE,Chrome瀏覽器和FF一起:

var TableToExcel = (function() { 
    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"><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 cellspacing="0" rules="rows" border="1" style="color:Black;background-color:White;border-color:#CCCCCC;border-width:1px;border-style:None;width:100%;border-collapse:collapse;font-size:9pt;text-align:center;">{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]; }) } 
    return function (table, name) { 
     if (!table.nodeType) table = document.getElementById(table) 
     var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML } 
     if (navigator.msSaveBlob) { 
      var blob = new Blob([format(template, ctx)], { type: 'application/vnd.ms-excel', endings: 'native' }); 
      navigator.msSaveBlob(blob, 'export.xls') 
     } else { 
      window.location.href = uri + base64(format(template, ctx)) 
     } 
    } 
})() 
1
 $("#btnExport").click(function(e) { 
       e.preventDefault(); 

      //getting data from table 
      var data_type = 'data:application/vnd.ms-excel'; 
      var table_div = document.getElementById('table_wrapper');//table_wrapper is table id 
      var table_html = table_div.outerHTML.replace(/ /g, '%20'); 

      var a = document.createElement('a'); 
      a.href = data_type + ', ' + table_html; 
      a.download = 'table_name_' + Math.floor((Math.random() * 9999999) + 1000000) + '.xls'; 
      a.click(); 
      });