PHPexcel 加载一个 html 格式的变量

PHPexcel load an html formatted variable

我正在尝试将 html 代码段变量加载到 spreadsheet.I 中,这些部分可能具有不同的动态 html 内容。所以我不能只是手动转换。

$dynamic_code_snippet = '<ul><li>item one</li><li>item two</li></ul>';

目前我正在尝试此操作但出现错误:

if(!empty($dynamic_code_snippet)){
                $excelHTMLReader = PHPExcel_IOFactory::createReader('HTML');
                $snippet = $excelHTMLReader->load($dynamic_code_snippet);
                $snippetWriter = PHPExcel_IOFactory::createWriter($snippet,'Excel2007');
                ob_start();
                $snippetWriter->save('php://output');
                $code  = ob_get_clean();
                $table->getActiveSheet()->setCellValue('A1, $code);
                }

我收到一个错误: 致命错误:未捕获 PHPExcel_Reader_Exception:无法在第 196 行 PHPExcel\Reader\Abstract.php 中打开

您认为这是因为它是一个片段而不是整页 html 文档(没有 <html><head><body> 标签)? 可以吗?还是我必须重新考虑?

PHPExcel 确实提供了一个助手向导,可用于将基本 html 标记转换为可设置为单元格值的 Riche 文本对象:

$wizard = new PHPExcel_Helper_HTML;
$richText = $wizard->toRichTextObject($html);

$objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A1', $richText);

但是,只有一些标记标签受到积极支持,ordered/unordered 列表不包括在内。

如果您想查看它,看看代码实际如何工作,可能修改它以使用列表,那么它是 /Classes/PHPExcel/Helper 中的 HTML.php 文件。