简体   繁体   中英

Export HTML table to excel with formatting

I am creating a web page where the data is uploaded to an HTML table. After some calculation, a output HTML table gets generated.The output HTML table must be dumped in Excel (.xlsx) format. I'm using SheetJS library for uploading and downloading purpose.

I have tried the following code which exports the data to excel without any formatting.

HTML Table code:- (Output HTML after calculation)

<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)

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. But that too doesn't seem to be working.

I'm not able to export data to Excel with CSS that has been applied.

Is there any other way that I can export data with CSS that has been applied.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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