Heim  >  Artikel  >  php教程  >  PHP导出Excel的类库,支持附带图片

PHP导出Excel的类库,支持附带图片

PHP中文网
PHP中文网Original
2016-05-25 17:13:061084Durchsuche

PHP导出Excel的类库,支持附带图片         

<?php
require_once &#39;./Classes/PHPExcel.php&#39;;
require_once &#39;./Classes/PHPExcel/IOFactory.php&#39;;
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
                             ->setLastModifiedBy("Maarten Balliauw")
                             ->setTitle("Office 2007 XLSX Test Document")
                             ->setSubject("Office 2007 XLSX Test Document")
                             ->setDescription("Document for Office 2007 XLSX, generated using PHP classes.")
                             ->setKeywords("office 2007 openxml php")
                             ->setCategory("Test result file");
                              
$objPHPExcel->setActiveSheetIndex(0);
$objRichText = new PHPExcel_RichText();
$objRichText->createText(&#39;&#39;);
$objPayable = $objRichText->createTextRun(&#39;PHP导出的Excel&#39;);
$objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_RED ));
$objPayable->getFont()->setBold(true);
$objPayable->getFont()->setSize(24);
$objPHPExcel->getActiveSheet()->getCell(&#39;B1&#39;)->setValue($objRichText);
$objPHPExcel->getActiveSheet()->getStyle(&#39;B1&#39;)->getFont()->setBold(true);       // 加粗
$objPHPExcel->getActiveSheet()->getStyle(&#39;B1&#39;)->getFont()->setSize(24);         // 字体大小
$objPHPExcel->getActiveSheet()->getStyle(&#39;B1&#39;)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED); // 文本颜色
$objPHPExcel->getActiveSheet()->getStyle(&#39;A1:G1&#39;)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle(&#39;A1:G1&#39;)->getFill()->getStartColor()->setARGB(&#39;00FFFFE3&#39;);          // 底纹
 
// 列宽
//$objPHPExcel->getActiveSheet()->getColumnDimension(&#39;A&#39;)->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension(&#39;A&#39;)->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension(&#39;B&#39;)->setWidth(18);
$objPHPExcel->getActiveSheet()->getColumnDimension(&#39;C&#39;)->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension(&#39;D&#39;)->setWidth(18);
$objPHPExcel->getActiveSheet()->getColumnDimension(&#39;E&#39;)->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension(&#39;F&#39;)->setWidth(18);
$objPHPExcel->getActiveSheet()->getColumnDimension(&#39;G&#39;)->setWidth(20);
 
// 行高
for($i = 2; $i <= 17; $i++)
{
    $objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(22);
}
 
$objPHPExcel->getActiveSheet()->setCellValue(&#39;A2&#39;, &#39;姓名&#39;);
$objPHPExcel->getActiveSheet()->setCellValue(&#39;A3&#39;, &#39;PHP&#39;);
$objPHPExcel->getActiveSheet()->setCellValue(&#39;A4&#39;, &#39;EXCEL&#39;);
$objPHPExcel->getActiveSheet()->setCellValue(&#39;A5&#39;, &#39;&#39;);
$objPHPExcel->getActiveSheet()->setCellValue(&#39;A6&#39;, &#39;XXX&#39;);
$objPHPExcel->getActiveSheet()->setCellValue(&#39;A7&#39;, &#39;V&#39;);
$objPHPExcel->getActiveSheet()->setCellValue(&#39;A8&#39;, &#39;d&#39;);
$objPHPExcel->getActiveSheet()->setCellValue(&#39;A9&#39;, &#39;name&#39;);
$objPHPExcel->getActiveSheet()->setCellValue(&#39;A10&#39;, &#39;描述&#39;);
for($i = 2; $i <= 16; $i++)
{
    $objPHPExcel->getActiveSheet()->getStyle(&#39;A&#39; . $i)->getFont()->setBold(true);       // 加粗
}
 
$objPHPExcel->getActiveSheet()->setCellValue(&#39;B2&#39;, &#39;[姓名]&#39;);
$objPHPExcel->getActiveSheet()->setCellValue(&#39;B3&#39;, &#39;导出带图片的EXCEL&#39;);
$objPHPExcel->getActiveSheet()->setCellValue(&#39;B4&#39;, &#39;XXXXX&#39;);
 
// 为excel加图片
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName(&#39;Photo&#39;);
$objDrawing->setDescription(&#39;Photo&#39;);
$objDrawing->setPath(&#39;./images/PIC.jpg&#39;);
$objDrawing->setHeight(170);
$objDrawing->setWidth(120);
$objDrawing->setCoordinates(&#39;G2&#39;);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
 
$objPHPExcel->getActiveSheet()->setCellValue(&#39;A17&#39;, &#39;其它要求&#39;);
$objPHPExcel->getActiveSheet()->setCellValue(&#39;B17&#39;, &#39;[其它要求]&#39;);
 
$objPHPExcel->getActiveSheet()->getStyle(&#39;A17&#39;)->getFont()->setBold(true);      // 加粗
 
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle(&#39;Excel表&#39;);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, &#39;Excel5&#39;);
$objWriter->save(&#39;PHP导出Excel.xls&#39;);

         

2. [文件] PHPExcel.rar

PHP导出Excel的类库,支持附带图片PHPExcel.rar


       

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:PHP生成excel并向其输入数据Nächster Artikel:php二分法