拉索尔 發表於 2021-12-7 10:13:00

PHP之PHPExcel

<p>PHPExcel是一款非常强大的PHP操作EXCEL库,使用PHPExcel可以帮助我们简单、高效实现从Excel读取Excel的数据和导出数据到Excel。</p>
<p>&nbsp;</p>
<p>&nbsp;操作:</p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>include <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">PHPExcel.php</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;
include </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">PHPExcel/Writer/Excel2007.php</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">或者include 'PHPExcel/Writer/Excel5.php'; 用于输出.xls的</span>
<span style="color: rgba(0, 0, 0, 1)">创建一个excel
$objPHPExcel </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> PHPExcel();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 输出Excel表格到浏览器下载</span>
header(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Content-Type: application/vnd.ms-excel</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
header(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Content-Disposition: attachment;filename="abc.xls"</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
header(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Cache-Control: max-age=0</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> If you're serving to IE 9, then the following may be needed</span>
header(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Cache-Control: max-age=1</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> If you're serving to IE over SSL, then the following may be needed</span>
header(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Expires: Mon, 26 Jul 1997 05:00:00 GMT</span><span style="color: rgba(128, 0, 0, 1)">'</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Date in the past</span>
header(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Last-Modified: </span><span style="color: rgba(128, 0, 0, 1)">'</span> . gmdate(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D, d M Y H:i:s</span><span style="color: rgba(128, 0, 0, 1)">'</span>) . <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)"> GMT</span><span style="color: rgba(128, 0, 0, 1)">'</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> always modified</span>
header(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Cache-Control: cache, must-revalidate</span><span style="color: rgba(128, 0, 0, 1)">'</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> HTTP/1.1</span>
header(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Pragma: public</span><span style="color: rgba(128, 0, 0, 1)">'</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> HTTP/1.0</span>
$objWriter = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter</span>-&gt;save(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">php://output</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
——————————————————————————————————————–
设置excel的属性:
创建人
$objPHPExcel</span>-&gt;getProperties()-&gt;setCreator(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Maarten Balliauw</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
最后修改人
$objPHPExcel</span>-&gt;getProperties()-&gt;setLastModifiedBy(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Maarten Balliauw</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
标题
$objPHPExcel</span>-&gt;getProperties()-&gt;settitle(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Office 2007 XLSX Test Document</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
题目
$objPHPExcel</span>-&gt;getProperties()-&gt;setSubject(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Office 2007 XLSX Test Document</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
描述
$objPHPExcel</span>-&gt;getProperties()-&gt;setDescription(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Test document for Office 2007 XLSX, generated using PHP classes.</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
关键字
$objPHPExcel</span>-&gt;getProperties()-&gt;setKeywords(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">office 2007 openxml php</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
种类
$objPHPExcel</span>-&gt;getProperties()-&gt;setCategory(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Test result file</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
——————————————————————————————————————–

设置当前的sheet
$objPHPExcel</span>-&gt;setActiveSheetIndex(<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">);
设置sheet的name
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;settitle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Simple</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
设置单元格的值
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;setCellValue(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">A1</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">String</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;setCellValue(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">A2</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 128, 1)">12</span><span style="color: rgba(0, 0, 0, 1)">);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;setCellValue(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">A3</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;setCellValue(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">C5</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">=SUM(C2:C4)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;setCellValue(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">B8</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">=MIN(B2:C5)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
合并单元格
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;mergeCells(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">A18:E22</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
分离单元格
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;unmergeCells(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">A28:B28</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
冻结窗口
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;freezePane(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">A2</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
保护cell
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getProtection()-&gt;setSheet(<span style="color: rgba(0, 0, 255, 1)">true</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Needs to be set to true in order to enable any worksheet protection!</span>
$objPHPExcel-&gt;getActiveSheet()-&gt;protectCells(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">A3:E13</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">PHPExcel</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
设置格式
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Set cell number formats</span>
echo date(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">H:i:s</span><span style="color: rgba(128, 0, 0, 1)">'</span>) . <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)"> Set cell number formats\n</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">E4</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getNumberFormat()-&gt;<span style="color: rgba(0, 0, 0, 1)">setFormatCode(PHPExcel_style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;duplicatestyle( $objPHPExcel-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">E4</span><span style="color: rgba(128, 0, 0, 1)">'</span>), <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">E5:E13</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> );
设置宽width
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Set column widths</span>
$objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">B</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;setAutoSize(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getColumnDimension(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;setWidth(<span style="color: rgba(128, 0, 128, 1)">12</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置单元格高度
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 所有单元格默认高度</span>
$objPHPExcel-&gt;getActiveSheet()-&gt;getDefaultRowDimension()-&gt;setRowHeight(<span style="color: rgba(128, 0, 128, 1)">15</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第一行的默认高度</span>
$objPHPExcel-&gt;getActiveSheet()-&gt;getRowDimension(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;setRowHeight(<span style="color: rgba(128, 0, 128, 1)">30</span><span style="color: rgba(0, 0, 0, 1)">);
设置font
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">B1</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getFont()-&gt;setName(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Candara</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">B1</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getFont()-&gt;setSize(<span style="color: rgba(128, 0, 128, 1)">20</span><span style="color: rgba(0, 0, 0, 1)">);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">B1</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getFont()-&gt;setBold(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">B1</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getFont()-&gt;<span style="color: rgba(0, 0, 0, 1)">setUnderline(PHPExcel_style_Font::UNDERLINE_SINGLE);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">B1</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getFont()-&gt;getColor()-&gt;<span style="color: rgba(0, 0, 0, 1)">setARGB(PHPExcel_style_Color::COLOR_WHITE);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">E1</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getFont()-&gt;getColor()-&gt;<span style="color: rgba(0, 0, 0, 1)">setARGB(PHPExcel_style_Color::COLOR_WHITE);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D13</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getFont()-&gt;setBold(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">E13</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getFont()-&gt;setBold(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
设置align
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D11</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getAlignment()-&gt;<span style="color: rgba(0, 0, 0, 1)">setHorizontal(PHPExcel_style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D12</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getAlignment()-&gt;<span style="color: rgba(0, 0, 0, 1)">setHorizontal(PHPExcel_style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D13</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getAlignment()-&gt;<span style="color: rgba(0, 0, 0, 1)">setHorizontal(PHPExcel_style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">A18</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getAlignment()-&gt;<span style="color: rgba(0, 0, 0, 1)">setHorizontal(PHPExcel_style_Alignment::HORIZONTAL_JUSTIFY);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">垂直居中</span>
$objPHPExcel-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">A18</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getAlignment()-&gt;<span style="color: rgba(0, 0, 0, 1)">setVertical(PHPExcel_style_Alignment::VERTICAL_CENTER);
设置column的border
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">A4</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getBorders()-&gt;getTop()-&gt;<span style="color: rgba(0, 0, 0, 1)">setBorderstyle(PHPExcel_style_Border::BORDER_THIN);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">B4</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getBorders()-&gt;getTop()-&gt;<span style="color: rgba(0, 0, 0, 1)">setBorderstyle(PHPExcel_style_Border::BORDER_THIN);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">C4</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getBorders()-&gt;getTop()-&gt;<span style="color: rgba(0, 0, 0, 1)">setBorderstyle(PHPExcel_style_Border::BORDER_THIN);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D4</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getBorders()-&gt;getTop()-&gt;<span style="color: rgba(0, 0, 0, 1)">setBorderstyle(PHPExcel_style_Border::BORDER_THIN);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">E4</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getBorders()-&gt;getTop()-&gt;<span style="color: rgba(0, 0, 0, 1)">setBorderstyle(PHPExcel_style_Border::BORDER_THIN);
设置border的color
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D13</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getBorders()-&gt;getLeft()-&gt;getColor()-&gt;setARGB(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">FF993300</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D13</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getBorders()-&gt;getTop()-&gt;getColor()-&gt;setARGB(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">FF993300</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D13</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getBorders()-&gt;getBottom()-&gt;getColor()-&gt;setARGB(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">FF993300</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">E13</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getBorders()-&gt;getTop()-&gt;getColor()-&gt;setARGB(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">FF993300</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">E13</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getBorders()-&gt;getBottom()-&gt;getColor()-&gt;setARGB(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">FF993300</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">E13</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getBorders()-&gt;getRight()-&gt;getColor()-&gt;setARGB(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">FF993300</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
设置填充颜色
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">A1</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getFill()-&gt;<span style="color: rgba(0, 0, 0, 1)">setFillType(PHPExcel_style_Fill::FILL_SOLID);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">A1</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getFill()-&gt;getStartColor()-&gt;setARGB(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">FF808080</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">B1</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getFill()-&gt;<span style="color: rgba(0, 0, 0, 1)">setFillType(PHPExcel_style_Fill::FILL_SOLID);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">B1</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;getFill()-&gt;getStartColor()-&gt;setARGB(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">FF808080</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
加图片
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">实例化插入图片类</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
$objDrawing </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> PHPExcel_Worksheet_Drawing();
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">设置图片路径 切记:只能是本地图片</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
$objDrawing</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">setPath($img_val);
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">设置图片高度</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
$objDrawing</span>-&gt;setWidth(<span style="color: rgba(128, 0, 128, 1)">200</span><span style="color: rgba(0, 0, 0, 1)">);
$img_height[] </span>= $objDrawing-&gt;<span style="color: rgba(0, 0, 0, 1)">getHeight();
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">设置图片要插入的单元格</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
$objDrawing</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">setCoordinates($img_k[$j].$i);
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">设置图片所在单元格的格式</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
$objDrawing</span>-&gt;setOffsetX(<span style="color: rgba(128, 0, 128, 1)">10</span><span style="color: rgba(0, 0, 0, 1)">);
$objDrawing</span>-&gt;setOffsetY(<span style="color: rgba(128, 0, 128, 1)">10</span><span style="color: rgba(0, 0, 0, 1)">);
$objDrawing</span>-&gt;setRotation(<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">);
$objDrawing</span>-&gt;getShadow()-&gt;setVisible(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
$objDrawing</span>-&gt;getShadow()-&gt;setDirection(<span style="color: rgba(128, 0, 128, 1)">50</span><span style="color: rgba(0, 0, 0, 1)">);
$objDrawing</span>-&gt;setWorksheet($objPHPExcel-&gt;<span style="color: rgba(0, 0, 0, 1)">getActiveSheet());
导出Excel表格例子
</span>&lt;?<span style="color: rgba(0, 0, 0, 1)">php
$datas </span>=<span style="color: rgba(0, 0, 0, 1)"> array(
array(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">王城</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">男</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">18</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1997-03-13</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">18948348924</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">),
array(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">李飞虹</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">男</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">21</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1994-06-13</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">159481838924</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">),
array(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">王芸</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">女</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">18</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1997-03-13</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">18648313924</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">),
array(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">郭瑞</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">男</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">17</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1998-04-13</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">15543248924</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">),
array(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">李晓霞</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">女</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">19</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1996-06-13</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">18748348924</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">),
);
include </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">PHPExcel.php</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;
include </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">PHPExcel/Writer/Excel2007.php</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">或者include 'PHPExcel/Writer/Excel5.php'; 用于输出.xls的
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建一个excel</span>
$objPHPExcel = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> PHPExcel();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Set document properties</span>
$objPHPExcel-&gt;getProperties()-&gt;setCreator(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Phpmarker</span><span style="color: rgba(128, 0, 0, 1)">"</span>)-&gt;setLastModifiedBy(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Phpmarker</span><span style="color: rgba(128, 0, 0, 1)">"</span>)-&gt;settitle(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Phpmarker</span><span style="color: rgba(128, 0, 0, 1)">"</span>)-&gt;setSubject(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Phpmarker</span><span style="color: rgba(128, 0, 0, 1)">"</span>)-&gt;setDescription(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Phpmarker</span><span style="color: rgba(128, 0, 0, 1)">"</span>)-&gt;setKeywords(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Phpmarker</span><span style="color: rgba(128, 0, 0, 1)">"</span>)-&gt;setCategory(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Phpmarker</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
$objPHPExcel</span>-&gt;setActiveSheetIndex(<span style="color: rgba(128, 0, 128, 1)">0</span>)-&gt;setCellValue(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">A1</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">名字</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;setCellValue(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">B1</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">性别</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;setCellValue(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">C1</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">年龄</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;setCellValue(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D1</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">出生日期</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;setCellValue(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">E1</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">电话号码</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Rename worksheet</span>
$objPHPExcel-&gt;getActiveSheet()-&gt;settitle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Phpmarker-</span><span style="color: rgba(128, 0, 0, 1)">'</span> . date(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Y-m-d</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">));

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Set active sheet index to the first sheet, so Excel opens this as the first sheet</span>
$objPHPExcel-&gt;setActiveSheetIndex(<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getDefaultRowDimension()-&gt;setRowHeight(<span style="color: rgba(128, 0, 128, 1)">15</span><span style="color: rgba(0, 0, 0, 1)">);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;freezePane(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">A2</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
$i </span>= <span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">foreach</span>($datas <span style="color: rgba(0, 0, 255, 1)">as</span><span style="color: rgba(0, 0, 0, 1)"> $data){
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;setCellValue(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">A</span><span style="color: rgba(128, 0, 0, 1)">'</span> . $i, $data[<span style="color: rgba(128, 0, 128, 1)">0</span>])-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">A</span><span style="color: rgba(128, 0, 0, 1)">'</span>.$i)-&gt;getAlignment()-&gt;<span style="color: rgba(0, 0, 0, 1)">setHorizontal(PHPExcel_style_Alignment::VERTICAL_CENTER);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;setCellValue(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">B</span><span style="color: rgba(128, 0, 0, 1)">'</span> . $i, $data[<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">]);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;setCellValue(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">C</span><span style="color: rgba(128, 0, 0, 1)">'</span> . $i, $data[<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">]);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;setCellValueExplicit(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D</span><span style="color: rgba(128, 0, 0, 1)">'</span>. $i, $data[<span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">],PHPExcel_Cell_DataType::TYPE_STRING);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D</span><span style="color: rgba(128, 0, 0, 1)">'</span> . $i)-&gt;getNumberFormat()-&gt;setFormatCode(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">@</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置文本格式</span>
$objPHPExcel-&gt;getActiveSheet()-&gt;setCellValueExplicit(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">E</span><span style="color: rgba(128, 0, 0, 1)">'</span>. $i, $data[<span style="color: rgba(128, 0, 128, 1)">4</span><span style="color: rgba(0, 0, 0, 1)">],PHPExcel_Cell_DataType::TYPE_STRING);
$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getstyle(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">E</span><span style="color: rgba(128, 0, 0, 1)">'</span> . $i)-&gt;getAlignment()-&gt;setWrapText(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
$i ;
}
$objActSheet </span>= $objPHPExcel-&gt;<span style="color: rgba(0, 0, 0, 1)">getActiveSheet();

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置CELL填充颜色</span>
$cell_fill =<span style="color: rgba(0, 0, 0, 1)"> array(
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">A1</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">B1</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">C1</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D1</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">E1</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
);
</span><span style="color: rgba(0, 0, 255, 1)">foreach</span>($cell_fill <span style="color: rgba(0, 0, 255, 1)">as</span><span style="color: rgba(0, 0, 0, 1)"> $cell_fill_val){
$cellstyle </span>= $objActSheet-&gt;<span style="color: rgba(0, 0, 0, 1)">getstyle($cell_fill_val);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> background
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> $cellstyle-&gt;getFill()-&gt;setFillType(PHPExcel_style_Fill::FILL_SOLID)-&gt;getStartColor()-&gt;setARGB('fafa00');
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> set align</span>
$cellstyle-&gt;getAlignment()-&gt;setHorizontal(PHPExcel_style_Alignment::HORIZONTAL_LEFT)-&gt;<span style="color: rgba(0, 0, 0, 1)">setVertical(PHPExcel_style_Alignment::VERTICAL_CENTER);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> font</span>
$cellstyle-&gt;getFont()-&gt;setSize(<span style="color: rgba(128, 0, 128, 1)">12</span>)-&gt;setBold(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> border</span>
$cellstyle-&gt;getBorders()-&gt;getTop()-&gt;setBorderstyle(PHPExcel_style_Border::BORDER_THIN)-&gt;getColor()-&gt;setARGB(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">FFFF0000</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
$cellstyle</span>-&gt;getBorders()-&gt;getBottom()-&gt;setBorderstyle(PHPExcel_style_Border::BORDER_THIN)-&gt;getColor()-&gt;setARGB(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">FFFF0000</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
$cellstyle</span>-&gt;getBorders()-&gt;getLeft()-&gt;setBorderstyle(PHPExcel_style_Border::BORDER_THIN)-&gt;getColor()-&gt;setARGB(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">FFFF0000</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
$cellstyle</span>-&gt;getBorders()-&gt;getRight()-&gt;setBorderstyle(PHPExcel_style_Border::BORDER_THIN)-&gt;getColor()-&gt;setARGB(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">FFFF0000</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
}

$objPHPExcel</span>-&gt;getActiveSheet()-&gt;getRowDimension(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;setRowHeight(<span style="color: rgba(128, 0, 128, 1)">30</span><span style="color: rgba(0, 0, 0, 1)">);

$objActSheet</span>-&gt;getColumnDimension(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">A</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;setWidth(<span style="color: rgba(128, 0, 128, 1)">18.5</span><span style="color: rgba(0, 0, 0, 1)">);
$objActSheet</span>-&gt;getColumnDimension(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">B</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;setWidth(<span style="color: rgba(128, 0, 128, 1)">23.5</span><span style="color: rgba(0, 0, 0, 1)">);
$objActSheet</span>-&gt;getColumnDimension(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">C</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;setWidth(<span style="color: rgba(128, 0, 128, 1)">12</span><span style="color: rgba(0, 0, 0, 1)">);
$objActSheet</span>-&gt;getColumnDimension(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;setWidth(<span style="color: rgba(128, 0, 128, 1)">12</span><span style="color: rgba(0, 0, 0, 1)">);
$objActSheet</span>-&gt;getColumnDimension(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">E</span><span style="color: rgba(128, 0, 0, 1)">'</span>)-&gt;setWidth(<span style="color: rgba(128, 0, 128, 1)">12</span><span style="color: rgba(0, 0, 0, 1)">);

$filename </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">2015030423</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;
ob_end_clean();</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">清除缓冲区,避免乱码 </span>
header(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Content-Type: application/vnd.ms-excel</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
header(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Content-Disposition: attachment;filename="</span><span style="color: rgba(128, 0, 0, 1)">'</span>.$filename.<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">.xls"</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
header(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Cache-Control: max-age=0</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> If you're serving to IE 9, then the following may be needed</span>
header(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Cache-Control: max-age=1</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> If you're serving to IE over SSL, then the following may be needed</span>
header(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Expires: Mon, 26 Jul 1997 05:00:00 GMT</span><span style="color: rgba(128, 0, 0, 1)">'</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Date in the past</span>
header(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Last-Modified: </span><span style="color: rgba(128, 0, 0, 1)">'</span> . gmdate(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">D, d M Y H:i:s</span><span style="color: rgba(128, 0, 0, 1)">'</span>) . <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)"> GMT</span><span style="color: rgba(128, 0, 0, 1)">'</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> always modified</span>
header(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Cache-Control: cache, must-revalidate</span><span style="color: rgba(128, 0, 0, 1)">'</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> HTTP/1.1</span>
header(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Pragma: public</span><span style="color: rgba(128, 0, 0, 1)">'</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> HTTP/1.0</span>
$objWriter = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter</span>-&gt;save(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">php://output</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
</span>?&gt;</pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>常见问题:</p>
<p>1.最近在使用PHPExcel循环生成多个sheet时,遇到You tried to set a sheet active by the out of bounds index: 1. The actual number of sheets is 1错误</p>
<p>原因:产生这个错误的原因是PHPExcel会自动创建第一个sheet,因此我们可以直接创建一个PHPEXCEL对象并且操作第一个sheet</p>
<div class="cnblogs_code">
<pre>$excel = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> PHPExcel();
$excel</span>-&gt;setactivesheetindex(<span style="color: rgba(128, 0, 128, 1)">0</span>);</pre>
</div>
<p>但是默认第一个sheet是已经创建的, 所以只需要创建后面的就可以</p>
<div class="cnblogs_code">
<pre>$excel = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> PHPExcel();
</span><span style="color: rgba(0, 0, 255, 1)">for</span>($i=<span style="color: rgba(128, 0, 128, 1)">0</span>;$i&lt;<span style="color: rgba(128, 0, 128, 1)">5</span>;$i++<span style="color: rgba(0, 0, 0, 1)">){
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>($i&gt;<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">){
      $excel</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">createSheet();
    }
    $excel</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">setactivesheetindex($i);
}</span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/xingxia/p/php_phpexcel.html
頁: [1]
查看完整版本: PHP之PHPExcel