【问题标题】:Export HTML table or CSV or XML or RTF to excel using javascript in Internet Explorer [closed]在 Internet Explorer 中使用 javascript 将 HTML 表或 CSV 或 XML 或 RTF 导出到 excel [关闭]
【发布时间】:2021-06-14 17:08:34
【问题描述】:

我正在尝试使用带有 Internet Explorer 的纯 JavaScript 将 HTML 表或 XML、CSV 或 RTF 文件转换为 Excel。 似乎我的 Javascript 或 jQuery 函数都不能在 IE 中运行。 我用其他现代浏览器运行相同的代码没有问题。 您能否给我一个在 Internet Explorer 中实际运行的简单示例?

【问题讨论】:

  • 我们能看到你试图在 IE 中运行的密码吗?
  • 另外,您能解释一下为什么要尝试支持 Internet Explorer 吗?为什么不是边缘?微软即将结束对 IE 的支持:blogs.windows.com/windowsexperience/2021/05/19/…
  • 没有密码..我尝试了在网上找到的简单示例并对其进行了修改,但我总是收到错误“JavaScript函数未定义”..我也尝试过Edge但成功有限...我将发布一个示例..

标签: javascript xml csv xlsx


【解决方案1】:
<!DOCTYPE html>
<head></head>
<body>
<table id="tblData">
    <tr>
        <th>Name</th>
        <th>Email</th>
        <th>Country</th>
    </tr>
    <tr>
        <td>John Doe</td>
        <td>john@gmail.com</td>
        <td>USA</td>
    </tr>
    <tr>
        <td>Michael Addison</td>
        <td>michael@gmail.com</td>
        <td>UK</td>
    </tr>
    <tr>
        <td>Sam Farmer</td>
        <td>sam@gmail.com</td>
        <td>France</td>
    </tr>
</table>

<button onclick="exportTableToExcel('tblData')">Export Table Data To Excel File</button>

<button onclick="exportTableToExcel('tblData', 'members-data')">Export Table Data To Excel File</button>

<script>
function exportTableToExcel(tableID, filename = ''){
    var downloadLink;
    var dataType = 'application/vnd.ms-excel';
    var tableSelect = document.getElementById(tableID);
    var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
    
    // Specify file name
    filename = filename?filename+'.xls':'excel_data.xls';
    
    // Create download link element
    downloadLink = document.createElement("a");
    
    document.body.appendChild(downloadLink);
    
    if(navigator.msSaveOrOpenBlob){
        var blob = new Blob(['\ufeff', tableHTML], {
            type: dataType
        });
        navigator.msSaveOrOpenBlob( blob, filename);
    }else{
        // Create a link to the file
        downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
    
        // Setting the file name
        downloadLink.download = filename;
        
        //triggering the function
        downloadLink.click();
    }
}
</script>
</body>
</html>

假设我有一个简单的 HTML 表格。这个例子在 IE 中不起作用。但它适用于 Edge。 我想对其他文件(XML、CSV、RTF)做同样的事情。 不幸的是,我使用的网络平台仅适用于 IE..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 2010-11-01
    • 2015-02-08
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多