PHPExcel_Reader_HTML生成空文件

时间:2014-08-13 17:32:03

标签: php phpexcel

我尝试使用phpexcel库将最简单的html表转换为excel,但无济于事。现在,我这样做:

require_once ('/PHPExcel/Classes/PHPExcel.php');
file_put_contents('tmp.html','<table border="1"><tr><td>123</td></tr></table>');
$objReader = new PHPExcel_Reader_HTML;
$objPHPExcel = $objReader->load('tmp.html');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save("myExcelFile.xlsx");

当我最终打开myExcelFile.xlsx时,我发现它是空的,而tmp.html则不是。 我使用了库的PHPExcel_1.8.0版本。

修改

感谢Mark,它现在正在运作。但是,我看到,有些困难的html无法正确呈现。像这个表头一样,例如:

<Table style="border-collapse: collapse;" cellspacing="0" cellpadding="4"><THead><TR><TH style="background-color:#F8F8F8; border:1px solid #CCCCCC ; width:100px "  rowspan="5" >1</TH><TH style="background-color:#F8F8F8; border:1px solid #CCCCCC ; width:100px "  rowspan="5" >2</TH><TH style="background-color:#F8F8F8; border:1px solid #CCCCCC ; width:100px "  rowspan="5" >3</TH><TH style="background-color:#F8F8F8; border:1px solid #CCCCCC "  colspan="7"  rowspan="1" >4</TH><TH style="background-color:#F8F8F8; border:1px solid #CCCCCC ; width:100px "  rowspan="5" >16</TH></TR><TR><TH style="background-color:#F8F8F8; border:1px solid #CCCCCC "  rowspan="1" >5</TH><TH style="background-color:#F8F8F8; border:1px solid #CCCCCC ; width:100px "  rowspan="4" >7</TH><TH style="background-color:#F8F8F8; border:1px solid #CCCCCC "  colspan="5"  rowspan="1" >8</TH></TR><TR><TH style="background-color:#F8F8F8; border:1px solid #CCCCCC ; width:100px "  rowspan="3" >6</TH><TH style="background-color:#F8F8F8; border:1px solid #CCCCCC ; width:100px "  rowspan="3" >9</TH><TH style="background-color:#F8F8F8; border:1px solid #CCCCCC ; width:100px "  rowspan="3" >10</TH><TH style="background-color:#F8F8F8; border:1px solid #CCCCCC "  colspan="3"  rowspan="1" >11</TH></TR><TR><TH style="background-color:#F8F8F8; border:1px solid #CCCCCC "  colspan="2"  rowspan="1" >12</TH><TH style="background-color:#F8F8F8; border:1px solid #CCCCCC ; width:100px "  rowspan="2" >15</TH></TR><TR><TH style="background-color:#F8F8F8; border:1px solid #CCCCCC ; width:100px " >13</TH><TH style="background-color:#F8F8F8; border:1px solid #CCCCCC ; width:100px " >14</TH></TR></THead></Table>

1 个答案:

答案 0 :(得分:5)

有趣的是,我们有时会在登录github问题日志后的第二天出现问题:

编辑Classes/PHPExcel/Reader/HTML.php的第427行,其中包含

$loaded = $dom->loadHTMLFile($pFilename, PHPExcel_Settings::getLibXmlLoaderOptions());

并将其更改为

$loaded = $dom->loadHTMLFile($pFilename);