This article mainly introduces phpExcel's Guide to common functions and examples. For more information, see
This article mainly introduces phpExcel's Guide to common functions and examples. For more information, see
Basic PHPExcel operations:
Define an EXCEL Object
Define a php excel object and set the content displayed in the EXCEL Object
// Start with Excel // prepare the EXCEL files. // Error reporting error_reporting (0); // PHPExcel require_once dirname (_ FILE __). 'phpexcel. php '; // generate a new excel Object $ objPHPExcel = new PHPExcel (); // set the attributes of the excel document $ objPHPExcel-> getProperties ()-> setCreator ("Sam. c ")-> setLastModifiedBy (" Sam. c Test ")-> setTitle (" Microsoft Office Excel Document ")-> setSubject (" Test ")-> setDescription (" Test ")-> setKeywords (" Test ") -> setCategory ("Test result file"); // start to operate the excel table // operate the first worksheet $ objPHPExcel-> setActiveSheetIndex (0 ); // set the workbook name $ objPHPExcel-> getActiveSheet ()-> setTitle (iconv ('gbk', 'utf-8', 'phpexcel test ')); // set the default font and size $ objPHPExcel-> getDefaultStyle ()-> getFont ()-> setName (iconv ('gbk', 'utf-8 ', 'body'); $ objPHPExcel-> getDefaultStyle ()-> getFont ()-> setSize (10 );
Iii. output file
// If You Need to output the EXCEL format if ($ m_exportType = "excel") {$ objWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel5 '); // output $ filenameheader ("Pragma: public") from the browser; header ("Expires: 0"); header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 "); header (" Content-Type: application/force-download "); header (" Content-Type: application/vnd. ms-excel; "); header (" Content-Type: application/octet-stream "); header (" Content-Type: application/download "); header ("Content-Disposition: attachment; filename = ". $ filename); header ("Content-Transfer-Encoding: binary"); $ objWriter-> save ("php: // output ");} // if You Need to output the PDF format if ($ m_exportType = "pdf") {$ objWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'pdf '); $ objWriter-> setSheetIndex (0); header ("Pragma: public"); header ("Expires: 0"); header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 "); header (" Content-Type: application/force-download "); header (" Content-Type: application/pdf "); header (" Content-Type: application/octet-stream "); header (" Content-Type: application/download "); header ("Content-Disposition: attachment; filename = ". $ m_strOutputPdfFileName); header ("Content-Transfer-Encoding: binary"); $ objWriter-> save ("php: // output ");}
Set the width of a column:
$ ObjPHPExcel-> getActiveSheet ()-> getColumnDimension ('A')-> setWidth (15 );
Set the height of a row:
$ ObjPHPExcel-> getActiveSheet ()-> getRowDimension ('6')-> setRowHeight (30 );
Merge cells:
$ ObjPHPExcel-> getActiveSheet ()-> mergeCells ('a1: p1 ');
Set A1 cell to bold and center:
$ StyleArray1 = array ('font' => array ('bold '=> true, 'SIZE' => 12, 'color' => array ('arg' => '000000',),), 'alignment '=> array ('horizontal' => PHPExcel_Style_Alignment :: HORIZONTAL_CENTER,),); // You Can bold and center the A1 cell to $ objPHPExcel-> getActiveSheet ()-> getStyle ('a1')-> applyFromArray ($ styleArray1 ); $ objPHPExcel-> getActiveSheet ()-> getStyle ('b1 ')-> getFont ()-> setBold (true );
Write content to a specific cell:
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('a1', 'Hello baba ');
Set the cell style (horizontal/vertical center ):
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('a1')-> getAlignment ()-> setHorizontal (Region: HORIZONTAL_CENTER); $ objPHPExcel-> getActiveSheet () -> getStyle ('a1')-> getAlignment ()-> setVertical (PHPExcel_Style_Alignment: VERTICAL_CENTER );
Set the cell style (black font ):
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('h5')-> getFont ()-> getColor ()-> setARGB (PHPExcel_Style_Color: COLOR_BLACK); // black
Set the cell format (background ):
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('h5')-> getFill ()-> getStartColor ()-> setARGB ('00ff99cc '); // set the background to pale pink
Set the cell format (Number Format ):
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('F'. $ iLineNumber)-> getNumberFormat ()-> setFormatCode ('0. 000 ');
Put an image into the cell:
// Place the data center image in the J1 cell $ objDrawing = new PHPExcel_Worksheet_Drawing (); $ objDrawing-> setName ('logo '); $ objDrawing-> setDescription ('logo '); $ objDrawing-> setPath('test.jpg '); $ objDrawing-> setWidth (400); $ objDrawing-> setHeight (123); $ objDrawing-> setCoordinates ('j1 '); $ objDrawing-> setWorksheet ($ objPHPExcel-> getActiveSheet ());
Set hyperlinks in cells:
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('h8', iconv ('gbk', 'utf-8', 'annan Tian ')); $ objPHPExcel-> getActiveSheet ()-> getCell ('h8')-> getHyperlink ()-> setUrl ('HTTP: // www.jb51.net /');
Set cell borders
$ StyleThinBlackBorderOutline = array ('borders '=> array ('outline' => array ('style' => PHPExcel_Style_Border: BORDER_THIN, // set the border style // 'style' => PHPExcel_Style_Border: BORDER_THICK, another style 'color' => array ('arg' => 'ff000000 '), // set the border color),),); $ objPHPExcel-> getActiveSheet ()-> getStyle ('a4: e10')-> applyFromArray ($ styleThinBlackBorderOutline ); // Add a new worksheet $ objExcel-> createSheet (); $ objActSheet = $ objExcel-> getSheet ($ s); $ objActSheet-> setTitle ('table '. $ GSheet );