dongyong2063 2014-07-29 18:44
浏览 134
已采纳

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

I'm looking for a way to read an excel file (xls format, but could probably use xlsx if necessary), and convert that file into an html table, preserving the rich text in the cells, cell borders, as well as the hyperlinks.

I've looked at https://code.google.com/p/php-excel-reader2/, which looks pretty good except some borders are missed and hyperlink targets have missing letter at end of filename (weird). I could probably debug these if I have to, but another problem with this is that this code base isn't supported any more. See http://www.steeplechasers.org/racecalendar-php-excel.php for example (spreadsheet is at 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>

I also looked at https://github.com/PHPOffice/PHPExcel, which seems like it should be able to do this somehow, but using simple generateSheetData, borders rich text and hyperlinks don't seem to be preserved, and I'm having trouble groking the docs to see how to copy these attributes into html. See http://www.steeplechasers.org/racecalendar-PHPExcel.php for example (same input file).

<?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>

Convert HTML to Excel Rich Text and vice versa seems to indicate this isn't a feature of PHPExcel yet.

PHPExcel - Preserve rich text format on import didn't answer the question at all, as far as I could tell.

Is there another way to do this directly, or some way to do this with PHPExcel?

NOTE: I would actually like to iterate through the cells because I want to add a column to the html table as I go -- I used code to dump the whole spreadsheet as simple examples of the package behaviors, and this is as far as I have gotten in my investigation.

  • 写回答

2条回答 默认 最新

  • drkrsx3135168 2014-07-29 21:01
    关注

    You're setting

    $reader->setReadDataOnly(true);
    

    when you read the spreadsheet file

    This tells PHPExcel to ignore any formatting information in the spreadsheet file, and only to read the raw data in the cells. It means that all formatting (borders, number formatting styles, font colours, etc) will be ignored

    The solution is not to set $reader->setReadDataOnly(true);

    EDIT

    As a quick hack for the URLs, you can modify /Classes/PHPExcel/Reader/Excel5.php

    Lines 4564 and 4565

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

    change to

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

    It fixes the URLs (note that php-excel-reader2 loses the last character of the URL) by testing for a null string terminator; and doesn't appear to have any adverse effects though it isn't adjusting the offset to allow for the adjustment. I need to check the BIFF specs and run a few more tests against both BIFF 5 and BIFF8 files with different link types before I'll commit anything back to the github repo)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件