为什么在文件名中将“.xls”存储两次

时间:2017-01-16 20:11:41

标签: javascript html

为什么以下代码段将文件另存为.xls.xls

即。 xls出现两次? sheetname的结尾为“.xls”,但它不是我指定的内容......

请注意,Stack片段似乎允许window.open(这是一个错误 - 我错过了一个参考吗?),但如果你复制到FireFox,你会看到问题。

function myFunction() {

  var tab_text = "<table border='2px'><tr>";
  var table = document.getElementById("tab2excel");

  var style;
  for (var j = 0; j < table.rows.length; j++) {
    style = table.rows[j].className.split(" ");
    if (style.length < 2)
      tab_text = tab_text + table.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, "");

  window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
}
<div>
  <table class="table2xl" id="tab2excel">
    <thead>
      <tr>
        <th>Column 1</th>
        <th>Column 2</th>
        <th>Column 3</th>
        <th>Column 4</th>
      </tr>
    </thead>
    <tbody>

      <tr>
        <td>Test</td>
        <td>Bear</td>
        <td>23.8</td>
        <td>7</td>
      </tr>
      <tr>
        <td>Test2</td>
        <td>Kite</td>
        <td>3.8</td>
        <td>77</td>
      </tr>
    </tbody>
  </table>
</div>

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

){         var tab_text =“”;         var table = document.getElementById(tblId);

    var style;
    for (var j = 0; j < table.rows.length; j++) {
        style = table.rows[j].className.split(" ");
        if (style.length < 2)
            tab_text = tab_text + table.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, "");


    return window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));

}

0 个答案:

没有答案