4

我想导出(强制下载)HTML(带 CSS)到 EXCEL 表,现在我使用 PHPExcel 库来执行此操作,它生成 excel 文件但删除 CSS(使用内联 html 标签),谁能指导我,即如何将 CSS 保存在 Excel 工作表中。

我正在使用此代码,但我也想保留 css 并强制下载

//html
$html = "<table> 
<thead> <tr> <td colspan='2'> <h1> Main Heading </h1> <td> </tr> </thead>
<tbody>
<tr> 
  <th style='background:#ccc; color:red; font-size:15px'> Name <th> 
  <th style='background:#ccc; color:red; font-size:15px'> Class <th> 
</tr> 
<tr> 
  <td style='background:#fff; color:green; font-size:13px'> Jhon <th> 
  <td style='background:#fff; color:gree; font-size:13px'> 9th <th> 
</tr> 
</tbody>

</table>";

// Put the html into a temporary file
$tmpfile = time().'.html';
file_put_contents($tmpfile, $html);

// Read the contents of the file into PHPExcel Reader class
$reader = new PHPExcel_Reader_HTML; 
$content = $reader->load($tmpfile);  

// Pass to writer and output as needed
$objWriter = PHPExcel_IOFactory::createWriter($content, 'Excel2007');
$objWriter->save('excelfile.xlsx');

// Delete temporary file
unlink($tmpfile);
4

1 回答 1

2

目前你无法从 HTML 标记中读取样式,除非你重写 PHPExcel 的 HTML Reader 来处理样式;它根本不支持。如果您是从 HTML 构建电子表格,也许您应该重新考虑直接从一个新的 PHPExcel 对象构建它,这样您就可以访问 PHPExcel 的所有功能。

要发送到浏览器,请php://output使用适当的标题发送到,如 中所示Examples/01simple-download-xlsx.php,并在开发人员文档的标题为的部分中描述Redirect output to a client’s web browser

于 2014-12-10T01:25:14.950 回答