【问题标题】:Excel html table exportExcel html表格导出
【发布时间】:2013-01-28 17:06:10
【问题描述】:

所以我有这个代码:

$output = '
<table class="sticky-enabled">
 <thead><tr><th>Text0</th><th>Text1</th><th>Text2</th> </tr></thead>
<tbody id="">
 <tr class="odd"><td>7</td><td>texttt</td><td>texttt</td> </tr>
</table>';



    header("Content-type: application/vnd.ms-excel;");
    header("Cache-Control: no-store, no-cache");
    header("Content-Disposition: attachment; filename=file.xls");
    print chr(255) . chr(254) . mb_convert_encoding($output, 'UTF-16LE', 'UTF-8');
    exit;

这个东西会下载一个带有表格的 Excel 文件。

不过有两个问题:

  1. 当我使用 Excel 打开它时,它会显示您尝试打开的文件格式与扩展错误不同
  2. 当我尝试保存它时,默认文件扩展名是 .htm 而不是 xls

有没有办法操纵标题等以解决这两个问题?

【问题讨论】:

  • 最简单的答案是使用 PHPExcel 或可以执行此操作的许多其他 PHP 库之一创建真正的 BIFF Excel 文件;那么你也不必用笨拙的编码更改来搞砸你的输出

标签: php html excel header html-table


【解决方案1】:
header('Content-type : text/csv');

并将其打印为逗号分隔值文件。您不能只将 HTML 表格打印成 .xls 或 .xlsx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 2018-06-24
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    相关资源
    最近更新 更多