dqf2015 2017-06-19 12:26
浏览 39
已采纳

在Excel中查看多个HTML表时无法设置TD宽度

Hello I write PHP header for downloading excel.

If I have 1 table I can set the width of <td> successfully by use width attribute

but if I have two table it's not working. How could I do? because I need multiple table in my excel page file

<?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>
  • 写回答

1条回答 默认 最新

  • dsdtszi0520538 2017-06-19 17:18
    关注

    Excel, when reading HTML files, really only has one HTML table. If you look at the Excel window you'll see that physically it's just a single table. If you insist on separating the data (which makes no difference to Excel) you can use multiple table bodies:

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

    Output:

    screenshot of Excel

    By the way, you are abusing MIME types terribly here! This document is not application/vnd.ms-excel or application/xhtml+xml, it's text/html and should not be saved with an XLS extension. Excel can read HTML files, but that doesn't make HTML files Excel files!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了