在html中渲染excel工作表,使用php,包括富文本和超链接


Render excel worksheet in html, using php, including rich text and hyperlinks

我正在寻找一种方法来读取excel文件(xls格式,但可能使用xlsx如果必要的话),并将该文件转换为html表,保留单元格,单元格边界的富文本,以及超链接。

我看了https://code.google.com/p/php-excel-reader2/,它看起来很好,除了一些边界错过了,超链接目标在文件名结束时缺少字母(奇怪)。如果有必要,我可能会调试这些,但另一个问题是这个代码库不再受支持。例如,请参阅http://www.steeplechasers.org/racecalendar-php-excel.php(电子表格在http://www.steeplechasers.org/racecalendar.xls)。

<?php
error_reporting(E_ALL ^ E_NOTICE);
require_once 'excel_reader2.php';
$data = new Spreadsheet_Excel_Reader("racecalendar.xls");
?>
<html>
<head>
<style>
table.excel {
    border-style:ridge;
    border-width:1;
    border-collapse:collapse;
    font-family:sans-serif;
    font-size:12px;
}
table.excel thead th, table.excel tbody th {
    background:#CCCCCC;
    border-style:ridge;
    border-width:1;
    text-align: center;
    vertical-align:bottom;
}
table.excel tbody th {
    text-align:center;
    width:20px;
}
table.excel tbody td {
    vertical-align:bottom;
}
table.excel tbody td {
    padding: 0 3px;
    border: 1px solid #EEEEEE;
}
</style>
</head>
<body>
<?php echo $data->dump(true,true); ?>
</body>
</html>

我也看了https://github.com/PHPOffice/PHPExcel,这似乎应该能够做到这一点,但使用简单的generateSheetData,边界富文本和超链接似乎没有被保留,我有麻烦的文件,看看如何复制这些属性到html。参见http://www.steeplechasers.org/racecalendar-PHPExcel.php示例(相同的输入文件)。

<?php
error_reporting(E_ALL ^ E_NOTICE);
require_once 'PHPExcel/Classes/PHPExcel.php';
$filename = 'racecalendar.xls';
//$filetype = PHPExcel_IOFactory::identify($filename);
$reader = PHPExcel_IOFactory::createReaderForFile($filename);
$reader->setReadDataOnly(true);
$excel = $reader->load($filename);
$writer = PHPExcel_IOFactory::createWriter($excel, "HTML");
?>
<html>
<head>
</head>
<style>
<?php
echo $writer->generateStyles(false);
?>
</style>
<body>
<?php
echo $writer->generateSheetData();
?>
</body>
</html>

将HTML转换为Excel富文本,反之亦然,似乎表明这还不是PHPExcel的功能。

PHPExcel -在导入时保留富文本格式根本没有回答这个问题,据我所知。

是否有另一种方法可以直接做到这一点,或者用PHPExcel来做到这一点?

注意:我实际上想遍历单元格,因为我想在html表中添加一列——我使用代码转储整个电子表格作为包行为的简单示例,这是我在调查中所得到的。

你正在设置

$reader->setReadDataOnly(true);

当你阅读电子表格文件

这告诉PHPExcel忽略电子表格文件中的任何格式信息,只读取单元格中的原始数据。这意味着所有的格式(边框,数字格式样式,字体颜色等)将被忽略

解决方案是不设置$reader->setReadDataOnly(true);

编辑

作为url的快速hack,您可以修改/Classes/PHPExcel/Reader/Excel5.php

第4564和4565行

$url = self::_encodeUTF16(substr($recordData, $offset, $us - 2), false);
$url .= $hasText ? '#' : '';

改变

$url = self::_encodeUTF16(substr($recordData, $offset, $us - 2), false);
$nullOffset = strpos($url, 0x00);
if ($nullOffset)
    $url = substr($url,0,$nullOffset);
$url .= $hasText ? '#' : '';

它通过测试null字符串结束符来修复URL(注意php-excel-reader2丢失URL的最后一个字符);而且似乎没有任何不利影响尽管它没有调整抵消量以允许调整。我需要检查BIFF规范,并在我将任何内容提交回github repo之前,对具有不同链接类型的BIFF 5和BIFF8文件运行更多的测试)

这里有一个链接可能会有所帮助:

同样,你可以得到Excel的XML版本(我相信Excel是以XML形式存储的),然后用PHP分析XML,然后在你的网站上放一个表。