PHP excel class phpExcel method of use

  • 2020-03-31 21:06:14
  • OfStack

Download address: (link: http://www.codeplex.com/PHPExcel)
Here are a few ways to use it
The include 'PHPExcel. PHP';
The include 'PHPExcel/Writer/Excel2007. PHP';
/ / or include 'PHPExcel/Writer/Excel5. PHP'; For the output of. XLS
Create an excel
$objPHPExcel = new PHPExcel ();
Save the excel - 2007 format
$objWriter = new PHPExcel_Writer_Excel2007 ($objPHPExcel);
// or $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); The 2007 format
$objWriter - > Save (" XXX. XLSX ");
Output directly to the browser
$objWriter = new PHPExcel_Writer_Excel5 ($objPHPExcel);
The header (" Pragma: public ");
The header (" Expires: 0 ");
The header (" cache-control: must revalidate, post - check = 0, the pre - check = 0 ");
The content-type header (" : application/force - download ");
The content-type header (" : application/VND. Ms - execl ");
The content-type header (" : application/octet - stream ");
The content-type header (" : application/download ");.
The header (' Content - Disposition: attachment; Filename = "resume. XLS" ");
The header (" the Content - Transfer - Encoding: binary ");
$objWriter - > Save (' PHP: / / output);
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- �
Set the properties of excel:
founder
$objPHPExcel - > The getProperties () - > SetCreator (" Maarten Balliauw ");
Last modifier
$objPHPExcel - > The getProperties () - > SetLastModifiedBy (" Maarten Balliauw ");
The title
$objPHPExcel - > The getProperties () - > SetTitle ("Office 2007 XLSX Test Document");
The title
$objPHPExcel - > The getProperties () - > SetSubject ("Office 2007 XLSX Test Document");
describe
$objPHPExcel - > The getProperties () - > SetDescription ("Test document for Office 2007 XLSX, generated using PHP classes.");
The keyword
$objPHPExcel - > The getProperties () - > SetKeywords (" office openxml PHP 2007 ");
species
$objPHPExcel - > The getProperties () - > SetCategory (" Test result file ");
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- �
Set the current sheet
$objPHPExcel - > SetActiveSheetIndex (0);
Set the name of the sheet
$objPHPExcel - > GetActiveSheet () - > SetTitle (" Simple ");
Sets the value of the cell
$objPHPExcel - > GetActiveSheet () - > SetCellValue (' A1 ', 'String');
$objPHPExcel - > GetActiveSheet () - > SetCellValue (' A2 ', 12);
$objPHPExcel - > GetActiveSheet () - > SetCellValue (' A3 ', true);
$objPHPExcel - > GetActiveSheet () - > C5 setCellValue (' ', '= SUM (C2: C4)');
$objPHPExcel - > GetActiveSheet () - > B8 setCellValue (' ', '= MIN (B2: C5)');
Merge cell
$objPHPExcel - > GetActiveSheet () - > A18: E22 mergeCells (' ');
Detached cell
$objPHPExcel - > GetActiveSheet () - > A28: B28 unmergeCells (' ');
To protect the cell
$objPHPExcel - > GetActiveSheet () - > GetProtection () - > SetSheet (true); // Needs to be set to true in order to enable any worksheet protection!
$objPHPExcel - > GetActiveSheet () - > A3: E13 protectCells (' ', 'PHPExcel');
Set the format
// Set cell number formats
Echo date('H: I :s'). "Set cell number formatsn";
$objPHPExcel - > GetActiveSheet () - > E4 getStyle (' ') - > GetNumberFormat () - > SetFormatCode (PHPExcel_Style_NumberFormat: : FORMAT_CURRENCY_EUR_SIMPLE);
$objPHPExcel - > GetActiveSheet () - > DuplicateStyle ($objPHPExcel - > GetActiveSheet () - > E4 getStyle (' '), 'E5: E13');
Set wide width
/ / Set the column widths
$objPHPExcel - > GetActiveSheet () - > GetColumnDimension (' B ') - > SetAutoSize (true);
$objPHPExcel - > GetActiveSheet () - > GetColumnDimension (' D ') - > SetWidth (12);
Set the font
$objPHPExcel - > GetActiveSheet () - > B1 getStyle (' ') - > GetFont () - > Elegantly-named setName (" Candara ");
$objPHPExcel - > GetActiveSheet () - > B1 getStyle (' ') - > GetFont () - > SetSize (20);
$objPHPExcel - > GetActiveSheet () - > B1 getStyle (' ') - > GetFont () - > SetBold (true);
$objPHPExcel - > GetActiveSheet () - > B1 getStyle (' ') - > GetFont () - > SetUnderline (PHPExcel_Style_Font: : UNDERLINE_SINGLE);
$objPHPExcel - > GetActiveSheet () - > B1 getStyle (' ') - > GetFont () - > GetColor () - > SetARGB (PHPExcel_Style_Color: : COLOR_WHITE);
$objPHPExcel - > GetActiveSheet () - > E1 'getStyle () - > GetFont () - > GetColor () - > SetARGB (PHPExcel_Style_Color: : COLOR_WHITE);
$objPHPExcel - > GetActiveSheet () - > D13 getStyle (' ') - > GetFont () - > SetBold (true);
$objPHPExcel - > GetActiveSheet () - > E13 getStyle (' ') - > GetFont () - > SetBold (true);
Set the align
$objPHPExcel - > GetActiveSheet () - > D11 getStyle (' ') - > GetAlignment () - > SetHorizontal (PHPExcel_Style_Alignment: : HORIZONTAL_RIGHT);
$objPHPExcel - > GetActiveSheet () - > GetStyle (' ') - D12 > GetAlignment () - > SetHorizontal (PHPExcel_Style_Alignment: : HORIZONTAL_RIGHT);
$objPHPExcel - > GetActiveSheet () - > D13 getStyle (' ') - > GetAlignment () - > SetHorizontal (PHPExcel_Style_Alignment: : HORIZONTAL_RIGHT);
$objPHPExcel - > GetActiveSheet () - > A18 getStyle (' ') - > GetAlignment () - > SetHorizontal (PHPExcel_Style_Alignment: : HORIZONTAL_JUSTIFY);
// center vertically
$objPHPExcel - > GetActiveSheet () - > A18 getStyle (' ') - > GetAlignment () - > SetVertical (PHPExcel_Style_Alignment: : VERTICAL_CENTER);
Sets the border for the column
$objPHPExcel - > GetActiveSheet () - > A4 getStyle (' ') - > GetBorders () - > GetTop () - > SetBorderStyle (PHPExcel_Style_Border: : BORDER_THIN);
$objPHPExcel - > GetActiveSheet () - > B4 getStyle (' ') - > GetBorders () - > GetTop () - > SetBorderStyle (PHPExcel_Style_Border: : BORDER_THIN);
$objPHPExcel - > GetActiveSheet () - > C4 getStyle (' ') - > GetBorders () - > GetTop () - > SetBorderStyle (PHPExcel_Style_Border: : BORDER_THIN);
$objPHPExcel - > GetActiveSheet () - > D4 'getStyle () - > GetBorders () - > GetTop () - > SetBorderStyle (PHPExcel_Style_Border: : BORDER_THIN);
$objPHPExcel - > GetActiveSheet () - > E4 getStyle (' ') - > GetBorders () - > GetTop () - > SetBorderStyle (PHPExcel_Style_Border: : BORDER_THIN);
Sets the border color
$objPHPExcel - > GetActiveSheet () - > D13 getStyle (' ') - > GetBorders () - > GetLeft () - > GetColor () - > FF993300 setARGB (' ');
$objPHPExcel - > GetActiveSheet () - > D13 getStyle (' ') - > GetBorders () - > GetTop () - > GetColor () - > FF993300 setARGB (' ');
$objPHPExcel - > GetActiveSheet () - > D13 getStyle (' ') - > GetBorders () - > GetBottom () - > GetColor () - > FF993300 setARGB (' ');
$objPHPExcel - > GetActiveSheet () - > E13 getStyle (' ') - > GetBorders () - > GetTop () - > GetColor () - > FF993300 setARGB (' ');
$objPHPExcel - > GetActiveSheet () - > E13 getStyle (' ') - > GetBorders () - > GetBottom () - > GetColor () - > FF993300 setARGB (' ');
$objPHPExcel - > GetActiveSheet () - > E13 getStyle (' ') - > GetBorders () - > GetRight () - > GetColor () - > FF993300 setARGB (' ');
Set the fill color
$objPHPExcel - > GetActiveSheet () - > GetStyle (' ') - A1 > GetFill () - > SetFillType (PHPExcel_Style_Fill: : FILL_SOLID);
$objPHPExcel - > GetActiveSheet () - > GetStyle (' ') - A1 > GetFill () - > GetStartColor () - > FF808080 setARGB (' ');
$objPHPExcel - > GetActiveSheet () - > B1 getStyle (' ') - > GetFill () - > SetFillType (PHPExcel_Style_Fill: : FILL_SOLID);
$objPHPExcel - > GetActiveSheet () - > B1 getStyle (' ') - > GetFill () - > GetStartColor () - > FF808080 setARGB (' ');
Add pictures
$objDrawing = new PHPExcel_Worksheet_Drawing ();
$objDrawing - > Elegantly-named setName (' Logo ');
$objDrawing - > SetDescription (' Logo ');
$objDrawing - > SetPath (". / images/officelogo. JPG ");
$objDrawing - > SetHeight (36);
$objDrawing - > SetWorksheet ($objPHPExcel - > GetActiveSheet ());
$objDrawing = new PHPExcel_Worksheet_Drawing ();
$objDrawing - > Elegantly-named setName (' Paid ');
$objDrawing - > SetDescription (' Paid ');
$objDrawing - > SetPath (". / images/paid. PNG ');
$objDrawing - > B15 setCoordinates (' ');
$objDrawing - > SetOffsetX (110);
$objDrawing - > SetRotation (25);
$objDrawing - > GetShadow () - > SetVisible (true);
$objDrawing - > GetShadow () - > SetDirection (45);
$objDrawing - > SetWorksheet ($objPHPExcel - > GetActiveSheet ());
After the default sheet, create a worksheet
Echo date('H: I :s'). "Create new Worksheet objectn";
$objPHPExcel - > CreateSheet ();
$objWriter = PHPExcel_IOFactory: : createWriter ($objExcel, 'Excel5');
$objWriter - save (' PHP: / / output);


Related articles: