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

你好,我写PHP头来下载excel。

如果我有一个表,我可以通过使用width属性来成功设置<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> 

在阅读HTML文件时,Excel实际上只有一个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> 

输出:

Excel的截图

顺便说一下,你在这里滥用MIMEtypes! 这个文件不是application/vnd.ms-excelapplication/xhtml+xml ,它是text/html ,不能用XLS扩展名保存。 Excel可以读取HTML文件,但不会使HTML文件生成Excel文件!