PHPExcel:HTML到Excel,写入删除excel文件中的CSS

时间:2014-12-09 13:02:41

标签: phpexcel phpexcelreader

我想导出(强制下载)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);

1 个答案:

答案 0 :(得分:2)

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

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

相关问题