This article mainly introduces the common function guide and examples of phpExcel help manual. For more information, see PHPExcel basic 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 $ filename header ("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 A1 cells 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 Baby');
Set the cell style (horizontal/vertical center ):
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::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 );