【问题标题】:Cannot set TD width when viewing multiple HTML tables in Excel在 Excel 中查看多个 HTML 表格时无法设置 TD 宽度
【发布时间】:2017-06-19 12:26:41
【问题描述】:

您好,我为下载 excel 编写 PHP 标头。

如果我有 1 个表格,我可以通过使用宽度属性成功设置 <td> 的宽度

但如果我有两张桌子,它就不起作用。我该怎么办?因为我的 excel 页面文件中需要多个表

<?php
header("Content-Type: application/vnd.ms-excel; charset=TIS-620");
header('Content-Disposition: attachment; filename="report_schedule_teacher.xls"');#ชื่อไฟล์

?>

<html>
<head>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=TIS-620" />


</head>
<body>

    <table width="100%" style="border-collapse: collapse;overflow:wrap; font-size:9pt;">
        <thead>
        <tr>
            <td width="300">Hello1</td>
            <td width="400">Hello2</td>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>World1</td>
            <td>World2</td>
        </tr>
        </tbody>
    </table>

    <table width="100%" style="border-collapse: collapse;overflow:wrap; font-size:9pt;">  
        <thead>
        <tr>
            <td width="300">Why I cannot set width if I have multiple table</td>
            <td width="400">Noooo</td>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>kub</td>
            <td>pom</td>
        </tr>
        </tbody>
    </table>


</body>
</html>

【问题讨论】:

  • 当你使用宽度时,你必须写单位(px、%等)。
  • 还是不行..
  • 你到底想做什么?
  • 我想设置 td 的宽度,但是在 excel 页面中,当我有多个表格时会出现问题。
  • 您在寻找 吗?所有表都是独立的。当你改变宽度表改变宽度。

标签: php html css excel http-headers


【解决方案1】:

Excel 在读取 HTML 文件时,实际上只有一个 HTML 表格。如果您查看 Excel 窗口,您会发现它实际上只是一个表格。如果您坚持分离数据(这对 Excel 没有影响),您可以使用多个表体:

<html>
<head>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=TIS-620" />


</head>
<body>

    <table width="100%" style="border-collapse: collapse;overflow:wrap; font-size:9pt;">
        <thead>
        <tr>
            <td width="300">Hello1</td>
            <td width="400">Hello2</td>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>World1</td>
            <td>World2</td>
        </tr>
        </tbody>

        <thead>
        <tr>
            <td width="300">Why I cannot set width if I have multiple table</td>
            <td width="400">Noooo</td>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>kub</td>
            <td>pom</td>
        </tr>
        </tbody>
    </table>


</body>
</html>

输出:

顺便说一句,您在这里非常滥用 MIME 类型!此文档不是application/vnd.ms-excelapplication/xhtml+xml,而是text/html,不应使用XLS 扩展名保存。 Excel 可以读取 HTML 文件,但不能将 HTML 文件变成 Excel 文件!

【讨论】:

    猜你喜欢
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 2020-11-30
    • 2015-10-09
    • 2018-08-07
    • 2016-12-14
    • 2013-07-21
    相关资源
    最近更新 更多