使用文本和图像将HTML表格导出为Excel

时间:2015-04-07 20:06:01

标签: javascript php jquery html export-to-excel

此问题已被问过其他一些帖子,但未提供解决方案。 我想从带有照片和文本的HTML表格中获取Excel输出。我找到了以下两个Excel输出插件(我认为最完整的插件):

http://ngiriraj.com/pages/htmltable_export/demo.php

http://www.jqueryscript.net/table/Export-Html-Table-To-Excel-Spreadsheet-using-jQuery-table2excel.html

<table id="customers" border="1" width="100%" >
<thead>         
    <tr class='warning'>
        <th>Country</th>
        <th>Population</th>
        <th>Date</th>
        <th>%ge</th>
        <th>Photo</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>Chinna</td>
        <td>1,363,480,000</td>
        <td>March 24, 2014</td>
        <td>19.1</td>
         <td><img src="http://i.stack.imgur.com/CekuQ.jpg" width='60'  height='60'/></td>
    </tr>
    <tr>
        <td>India</td>
        <td>1,241,900,000</td>
        <td>March 24, 2014</td>
        <td>17.4</td>
       <td><img src="http://i.stack.imgur.com/CekuQ.jpg" width='60'  height='60'/></td>
    </tr>
    <tr>
        <td>United States</td>
        <td>317,746,000</td>
        <td>March 24, 2014</td>
        <td>4.44</td>
          <td><img src="http://i.stack.imgur.com/CekuQ.jpg" width='60'  height='60'/></td>
    </tr>
    <tr>
        <td>Indonesia</td>
        <td>249,866,000</td>
        <td>July 1, 2013</td>
        <td>3.49</td>
         <td><img src="http://i.stack.imgur.com/CekuQ.jpg" width='60'  height='60'/></td>
    </tr>
    <tr>
        <td>Brazil</td>
        <td>201,032,714</td>
        <td>July 1, 2013</td>
        <td>2.81</td>
        <td><img src="http://i.stack.imgur.com/CekuQ.jpg" width='60'  height='60'/></td>
    </tr>
</tbody>

并导出为

<a href="#" onClick="$('#customers').tableExport({type:'excel',escape:'false'});">export</a>

我还在this fiddle中显示了包含图片的列

如果我将此表格导出到Excel,则图像不会显示。那么如何将这些图像与数据一起导出呢?应该使用什么样的输出?

如果在Excel输出中无法做到这一点,那么应该使用什么格式进行输出?

1 个答案:

答案 0 :(得分:1)

如果您不需要原始的Excel文件,而是需要在Excel中显示表格列的文件(并且可以在Excel或LibreOffice Calc或许多其他程序中编辑),您应该考虑使用CSV (comma-separated values)

易于创建和编辑,无需使用任何插件或库。

可能的输出

Column Name 1, Column Name 2, Column Name 3
'Row1Val1', 'Row1Val2', 'Row1Val3'
'Row2Val1', 'Row2Val2', 'Row2Val3'
'Row3Val1', 'Row3Val2', 'Row3Val3'

转换为Excel

但是如果你真的想要一个Excel文件, PHPExcel将成为你的朋友。它使用CSV文件创建完全可操作的Excel文件。

include 'PHPExcel/IOFactory.php';

$objReader = PHPExcel_IOFactory::createReader('CSV');

// If the files uses a delimiter other than a comma (e.g. a tab), then tell the reader
$objReader->setDelimiter("\t");
// If the files uses an encoding other than UTF-8 or ASCII, then tell the reader
$objReader->setInputEncoding('UTF-16LE');

$objPHPExcel = $objReader->load('MyCSVFile.csv');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('MyExcelFile.xls');

示例代码段:csv to excel conversion

图像插入

PHP Excel提供了包含图像的功能:

$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('PHPExcel logo');
$objDrawing->setDescription('PHPExcel logo');
$objDrawing->setPath('./images/phpexcel_logo.gif');       // filesystem reference for the image file
$objDrawing->setHeight(36);            
$objDrawing->setCoordinates('D24');    
$objDrawing->setOffsetX(10);                
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

摘自CodePlex