【问题标题】:exporting a table element to a xlsx file, with borders将表格元素导出到带有边框的 xlsx 文件
【发布时间】:2015-07-28 12:21:09
【问题描述】:

我想将一个 html 表格元素导出到一个带边框的 xlsx 文件。

我可以用 xls(不是 xlsx)来做到这一点。使用 xls,Excel 可以打开任何 html 文件并转换内容,包括边框。对于 xlsx,我必须使用 PHPExcel,但我无法格式化我的表格(添加边框...)。

我可以使用下面的代码导出到 xlsx,但没有边框:

   <?php
    require_once '/PHPExcel/Classes/PHPExcel.php';
    $file=$_POST['file']."_".date("ymd").".xlsx";
    $table = "<table border='1'>".$_POST['table']."</table>";
    $table = mb_convert_encoding($table, 'UTF-16LE', 'UTF-8'); 
    $table = "\xFF\xFE" . $table;

    // save $table inside temporary file that will be deleted later
    $tmpfile = tempnam(sys_get_temp_dir(), 'html');
    file_put_contents($tmpfile, $table);

    // insert $table into $objPHPExcel's Active Sheet through $excelHTMLReader
    $objPHPExcel     = new PHPExcel();

    $excelHTMLReader = PHPExcel_IOFactory::createReader('HTML');
    $excelHTMLReader->loadIntoExisting($tmpfile, $objPHPExcel);
    $objPHPExcel->getActiveSheet()->setTitle('biblio_uq9_excel'); // Change sheet's title if you want

    unlink($tmpfile); // delete temporary file because it isn't needed anymore

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // header for .xlxs file
    header('Content-Disposition: attachment;filename='.$file); // specify the download file name
    header('Cache-Control: max-age=0');

    // Creates a writer to output the $objPHPExcel's content
    $writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $writer->save('php://output');
    exit;

    ?>

编辑:请参阅下面的答案。我们无能为力。 Excel .xlsx 格式不允许自动转换分隔/标记文件。

【问题讨论】:

    标签: php html-table phpexcel


    【解决方案1】:

    都是原生 Excel。

    旧版 Excel (.xls) 允许直接打开分隔文本文件或标记文件(xml、html),Excel 可以正确转换这些文件。 Excel 也可以转换一些 css 样式,比如像这样应用边框:&lt;table border=1&gt;

    新 Excel (.xlsx) 不允许这样做,因此需要像 PHPExcel 这样的工具。 PHPExcel 将 html 表格转换为纯 Excel,但我们失去了所有格式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-26
      • 2015-09-27
      • 2018-06-10
      • 2017-04-05
      • 2021-12-28
      相关资源
      最近更新 更多