Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将多个html表格转换为包含多个工作表的Excel工作簿_Javascript_Html_Xml_Excel_Html Table - Fatal编程技术网

Javascript 将多个html表格转换为包含多个工作表的Excel工作簿

Javascript 将多个html表格转换为包含多个工作表的Excel工作簿,javascript,html,xml,excel,html-table,Javascript,Html,Xml,Excel,Html Table,我正在尝试将一个网页中的多个表格导出到一个Excel工作簿中,每个表格有一个工作表,任何一个都可以做到这一点,而无需将表格转换为,并利用htmlxml,即在中 目前我正在使用下面的函数,但是,虽然它确实创建了多个工作表,但它会将所有表放入第一个工作表中 function arrayToExcel(tablesId, filename) { var uri = 'data:application/vnd.ms-excel;base64,'; var worksheetTemplate =

我正在尝试将一个网页中的多个表格导出到一个Excel工作簿中,每个表格有一个工作表,任何一个都可以做到这一点,而无需将表格转换为
,并利用html
xml,即在

目前我正在使用下面的函数,但是,虽然它确实创建了多个工作表,但它会将所有表放入第一个工作表中

 function arrayToExcel(tablesId, filename) {
  var uri = 'data:application/vnd.ms-excel;base64,';
  var worksheetTemplate = '<x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions><table>{table}</table></x:ExcelWorksheet>';
  var format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
  var worksheets = tablesId.map(function(name){
      return format(worksheetTemplate, {worksheet: name});
    }).join('');
  var tables = tablesId.map(function(txt){
      var table = document.getElementById(txt).innerHTML;
      return format(tableTemplate, {table});
    }).join('');
  var formattedXML = '<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><o:DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"><o:Author>Dominik Dumaine</o:Author><o:Created>'+ (new Date()).getTime() +'</o:Created></o:DocumentProperties>'
          +'<x:ExcelWorkbook><x:ExcelWorksheets>'
          + worksheets
          +'</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body>'
          + tables
          +'</body></html>'
    , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
window.location.href = uri + base64(formattedXML);
}
注:我看过布塔尼·维杰的答案,它不符合我的要求
table_to_book和table_to_sheet实用程序函数接受一个domtable元素并遍历子节点

虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,则仅链接的答案可能无效。
<table id="tbl1" class="table2excel">
  <tr>
   <td>Product</td>
   <td>Price</td>
   <td>Available</td>
   <td>Count</td>
  </tr>
  <tr>
   <td>Bred</td>
   <td>1</td>
   <td>2</td>
   <td>3</td>
  </tr>
  <tr>
   <td>Butter</td>
   <td>4</td>
   <td>5</td>
   <td>6</td>
  </tr>
 </table>
<hr>
<table id="tbl2" class="table2excel">
  <tr>
    <td>Product</td>
    <td>Price</td>
    <td>Available</td>
    <td>Count</td>
  </tr>
  <tr>
    <td>Bred</td>
    <td>7</td>
    <td>8</td>
    <td>9</td>
  </tr>
  <tr>
    <td>Butter</td>
    <td>14</td>
    <td>15</td>
    <td>16</td>
  </tr>
</table>