简体   繁体   English

将 HTML 表格导出为带有格式的 Excel

[英]Export HTML table to excel with formatting

I am creating a web page where the data is uploaded to an HTML table.我正在创建一个将数据上传到 HTML 表的网页。 After some calculation, a output HTML table gets generated.The output HTML table must be dumped in Excel (.xlsx) format.经过一些计算,生成一个输出 HTML 表。输出 HTML 表必须以 Excel (.xlsx) 格式转储。 I'm using SheetJS library for uploading and downloading purpose.我正在使用SheetJS库进行上传和下载。

I have tried the following code which exports the data to excel without any formatting.我尝试了以下代码,它将数据导出到 excel 中而没有任何格式。

HTML Table code:- (Output HTML after calculation) HTML表格代码:-(计算后输出HTML)

<table border="1" id="tempTable">
    <thead>
        <tr>
            <th style="border:1px solid #000000;border-collapse: collapse">Column1</th>
            <th style="border:1px solid #000000;border-collapse: collapse">Column2</th>
            <th style="border:1px solid #000000;border-collapse: collapse">Column3</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td style="border:1px solid #000000;border-collapse: collapse">R1C1</td>
            <td style="border:1px solid #000000;border-collapse: collapse">R1C2</td>
            <td style="border:1px solid #000000;border-collapse: collapse">R1C3</td>
        </tr>
        <tr>
            <td style="border:1px solid #000000;border-collapse: collapse">R2C1</td>
            <td style="border:1px solid #000000;border-collapse: collapse">R2C2</td>
            <td style="border:1px solid #000000;border-collapse: collapse">R2C3</td>
        </tr>
    </tbody>
</table>

JavaScript code:- (Function that helps me to export data) JavaScript 代码:-(帮助我导出数据的功能)

function ExportAllData_HTML(){
        var wb = {SheetNames:[],Sheets:{}};
        var ws9 = XLSX.utils.table_to_sheet(document.getElementById('tempTable'),{raw:true});
        wb.SheetNames.push('Temp Table'); wb.Sheets["Temp Table"] = ws9;
        XLSX.writeFile(wb,"myTemp.xlsx",{cellStyles:true});
}

I've searched and found out that only Inline CSS works while exporting the data.我搜索并发现只有 Inline CSS 在导出数据时有效。 But that too doesn't seem to be working.但这似乎也不起作用。

I'm not able to export data to Excel with CSS that has been applied.我无法使用已应用的 CSS 将数据导出到 Excel。

Is there any other way that I can export data with CSS that has been applied.有没有其他方法可以使用已应用的 CSS 导出数据。

如果您只需要导出您看到的 html 表,我认为以下链接可以满足您的需求: Javascript to export html table to Excel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM