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 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据