其他几篇帖子也问过这个问题,但没有提供解决方案。我想从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="https://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="https://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="https://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="https://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="https://i.stack.imgur.com/CekuQ.jpg" width='60' height='60'/></td>
</tr>
</tbody>
并作为导出
<a href="#" onClick="$('#customers').tableExport({type:'excel',escape:'false'});">export</a>
我还加入了一个专栏,其中的图片如这把小提琴中的所示
如果我将此表导出到Excel,则不会显示图像。那么,我如何才能将这些图像与数据一起导出呢。应该使用哪种输出?
如果在Excel输出中无法做到这一点,输出应使用什么格式?
如果您不需要原始Excel文件,而是需要一个在Excel中显示表的列的文件(并且可以在Excel或LibreOffice Calc或许多其他程序中编辑),您应该考虑使用CSV(逗号分隔值)。
它很容易创建和编辑,而且不需要使用任何插件或库。
可能的输出
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到excel转换
图像插入
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中的代码段。