使用 html table 指定 excel 的列宽和行高

Specify column width and row height for excel using html table

我正在做一个项目,我需要使用 jquery 将 html table 导出到 excel sheet。我有所需的插件,但我无法做的就是在 excel 打开文件时设置列宽。

我试过设置 <th style="width:70px;"> 和还为相关的 table 设置 table-layout:fixed; width:100%;。我也在 WorkSheetOptions 中尝试 <DefaultColumnWidth>1</DefaultColumnWidth> 但没有成功。

我只需要所有列的宽度相等,行高根据单元格内容而变化。有什么办法可以做到这一点?我也可以使用将 html table 转换为 excel 的其他插件,但它需要有选项来设置列宽和行高。任何帮助是极大的赞赏。

这是我正在处理的示例 table:

<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>
    <meta name=ProgId content=Excel.Sheet>
    <meta name=Generator content="Microsoft Excel 11">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>Hourly Summary Report</x:Name><x:WorksheetOptions><x:DefaultColumnWidth>16</x:DefaultColumnWidth><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
</head>
<body>
    <table style="table-layout:fixed; width:100%;"><thead>
    <tr>
        <th style="text-align:center; vertical-align:middle; word-wrap: break-word;">This sentence badly needs a word wrap</th>
        <th style="text-align:center; vertical-align:middle;">Industrial Robots</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td class="bg-black disabled color-palette" style="text-align:center; vertical-align:middle; font-weight:bold;">13 Jun 2017 12:00 AM - 13 Jun 2017 01:00 AM</td>
    </tr>
</tbody>
</table>

事实证明,excel 在 html 中提供嵌套的 table 时会很奇怪。因此,一旦我删除了 table 标签,所有样式都开始正常使用 excel。为了方便其他人查看此答案,请注意删除 <table>, </table> 标签是必要且充分的。 DOM 的其余部分与 excel 一起工作正常。