Php excel phpExcel usage, excelphpexcel

Source: Internet
Author: User

Php excel phpExcel usage, excelphpexcel

PhpExcel allows you to easily add images, and supports jpg and png formats.

: Http://www.codeplex.com/PHPExcel

The following are the usage methods
Include 'phpexcel. php ';
Include 'phpexcel/Writer/excel2007.php ';
// Or include 'phpexcel/Writer/excel5.php'; for output. xls
Create an excel file
$ ObjPHPExcel = new PHPExcel ();
Save the excel-2007 format
$ ObjWriter = new PHPExcel_Writer_Excel2007 ($ objPHPExcel );
// Or $ objWriter = new PHPExcel_Writer_Excel5 ($ objPHPExcel); Non-2007 format
$ ObjWriter-> save ("xxx.xlsx ");
Output directly to the browser
$ ObjWriter = new PHPExcel_Writer_Excel5 ($ objPHPExcel );
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/vnd. ms-execl ");
Header ("Content-Type: application/octet-stream ");
Header ("Content-Type: application/download ");;
Header ('content-Disposition: attachment; filename = "resume.xls "');
Header ("Content-Transfer-Encoding: binary ");
$ ObjWriter-> save ('php: // output ');
--

If you are not familiar with the phpexcel class library, you can read the content in the phpexcel Chinese help manual. For more information, see related examples in the phpexcel quick development guide.
Set excel attributes:
Created
$ ObjPHPExcel-> getProperties ()-> setCreator ("Maarten Balliauw ");
Last modified
$ ObjPHPExcel-> getProperties ()-> setLastModifiedBy ("Maarten Balliauw ");
Title
$ ObjPHPExcel-> getProperties ()-> setTitle ("Office 2007 XLSX Test Document ");
Question
$ ObjPHPExcel-> getProperties ()-> setSubject ("Office 2007 XLSX Test Document ");
Description
$ ObjPHPExcel-> getProperties ()-> setDescription ("Test document for Office 2007 XLSX, generated using PHP classes .");
Keywords
$ ObjPHPExcel-> getProperties ()-> setKeywords ("office 2007 openxml php ");
Type
$ ObjPHPExcel-> getProperties ()-> setCategory ("Test result file ");
---------------------------------------
Set the current sheet
$ ObjPHPExcel-> setActiveSheetIndex (0 );
Set sheet name
$ ObjPHPExcel-> getActiveSheet ()-> setTitle ('simple ');
Set the cell value
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('a1', 'string ');
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('a2 ', 12 );
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('a3 ', true );
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('c5 ',' = SUM (C2: C4 )');
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('b8', '= MIN (B2: C5 )');
Merge Cells
$ ObjPHPExcel-> getActiveSheet ()-> mergeCells ('a18: e22 ′);
Separate Cells
$ ObjPHPExcel-> getActiveSheet ()-> unmergeCells ('a28: b28 ′);
Cell protection
$ ObjPHPExcel-> getActiveSheet ()-> getProtection ()-> setSheet (true); // Needs to be set to true in order to enable any worksheet protection!
$ ObjPHPExcel-> getActiveSheet ()-> protectCells ('a3: e13', 'phpexcel ');
Set format
// Set cell number formats
Echo date ('H: I: s'). "Set cell number formatsn ";
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('e4 ')-> getNumberFormat ()-> setFormatCode (PHPExcel_Style_NumberFormat: FORMAT_CURRENCY_EUR_SIMPLE );
$ ObjPHPExcel-> getActiveSheet ()-> duplicateStyle ($ objPHPExcel-> getActiveSheet ()-> getStyle ('e4 '), 'e5: e13 ′);
Set width
// Set column widths
$ ObjPHPExcel-> getActiveSheet ()-> getColumnDimension ('B')-> setAutoSize (true );
$ ObjPHPExcel-> getActiveSheet ()-> getColumnDimension ('D')-> setWidth (12 );
Set font
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('b1 ')-> getFont ()-> setName ('candara ');
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('b1 ')-> getFont ()-> setSize (20 );
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('b1 ')-> getFont ()-> setBold (true );
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('b1 ')-> getFont ()-> setUnderline (PHPExcel_Style_Font: UNDERLINE_SINGLE );
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('b1 ')-> getFont ()-> getColor ()-> setARGB (PHPExcel_Style_Color: COLOR_WHITE );
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('e1 ')-> getFont ()-> getColor ()-> setARGB (PHPExcel_Style_Color: COLOR_WHITE );
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('d13')-> getFont ()-> setBold (true );
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('e13')-> getFont ()-> setBold (true );
Set align
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('d11')-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_RIGHT );
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('d12')-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_RIGHT );
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('d13')-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_RIGHT );
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('a18')-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_JUSTIFY );
// Vertical center
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('a18')-> getAlignment ()-> setVertical (PHPExcel_Style_Alignment: VERTICAL_CENTER );
Set the border of column
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('a4 ')-> getBorders ()-> getTop ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('b4 ')-> getBorders ()-> getTop ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('c4 ')-> getBorders ()-> getTop ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('d4 ')-> getBorders ()-> getTop ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('e4 ')-> getBorders ()-> getTop ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
Set border color
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('d13')-> getBorders ()-> getLeft ()-> getColor ()-> setARGB ('ff993300 ′);
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('d13')-> getBorders ()-> getTop ()-> getColor ()-> setARGB ('ff993300 ′);
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('d13')-> getBorders ()-> getBottom ()-> getColor ()-> setARGB ('ff993300 ′);
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('e13')-> getBorders ()-> getTop ()-> getColor ()-> setARGB ('ff993300 ′);
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('e13')-> getBorders ()-> getBottom ()-> getColor ()-> setARGB ('ff993300 ′);
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('e13')-> getBorders ()-> getRight ()-> getColor ()-> setARGB ('ff993300 ′);
Set fill color
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('a1')-> getFill ()-> setFillType (PHPExcel_Style_Fill: FILL_SOLID );
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('a1')-> getFill ()-> getStartColor ()-> setARGB ('ff808080 ′);
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('b1 ')-> getFill ()-> setFillType (PHPExcel_Style_Fill: FILL_SOLID );
$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('b1 ')-> getFill ()-> getStartColor ()-> setARGB ('ff808080 ′);

>>> For more php tutorial content, please pay attention to the php programming section on this site.
Add Images
$ ObjDrawing = new PHPExcel_Worksheet_Drawing ();
$ ObjDrawing-> setName ('logo ');
$ ObjDrawing-> setDescription ('logo ');
$ ObjDrawing-> setPath ('./images/officelogo.jpg ');
$ ObjDrawing-> setHeight (36 );
$ ObjDrawing-> setWorksheet ($ objPHPExcel-> getActiveSheet ());
$ ObjDrawing = new PHPExcel_Worksheet_Drawing ();
$ ObjDrawing-> setName ('paid ');
$ ObjDrawing-> setDescription ('paid ');
$ ObjDrawing-> setPath ('./images/paid.png ');
$ ObjDrawing-> setCoordinates ('b15 ′);
$ 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 ');


The Excel file generated by PHPExcel cannot be read.

Summary php export Excel php import Excel PhpExcel instructions for use PhpExcel user manual 2009/03/06 am Method 1: features, simplicity, worry-free,

<? Php
Header ("Content-type: application/vnd. ms-excel ");
Header ("Content-Disposition: attachment?filename=test_data.xls ");

$ Tx = 'header ';
Echo $ tx. "\ n ";
// The output content is as follows:
Echo "name". "\ t ";
Echo "Age". "\ t ";
Echo "education". "\ t ";
Echo "\ n ";
Echo "James". "\ t ";
Echo "25". "\ t ";
Echo "Undergraduate". "\ t ";
?>

Method 2: reference the small library recommended by google code (similar to method 1, more complex)

Code.google.com/p/php-excel/downloads/list

Method 3: PHPEXCEL class library. It has powerful functions and supports win Excel2003 and Win Excel2007.

Www.codeplex.com/PHPExcel

<?
// Set the include path of the PHPExcel class library
Set_include_path ('.'. PATH_SEPARATOR.
'D: \ Zeal \ PHP_LIBS '. PATH_SEPARATOR.
Get_include_path ());

/**
* The following is an example. The optional methods are different for rows starting //.
* Open the comment of the corresponding row.
* If Excel5 is used, the output content should be GBK encoded.
*/
Require_once 'phpexcel. php ';

// Uncomment
/// Require_once 'phpexcel/Writer/Excel5.ph ...... the remaining full text>

Is there anything in php similar to phpexcel? Introduction

Phpexcel PHPExcel is quite powerful MS Office Excel file generation class library, when you need to output more complex format data, PHPExcel is a good choice. However, the method is relatively cumbersome. List to remember.
-- PHP Code --
<?
// Set the include path of the PHPExcel class library
Set_include_path ('.'. PATH_SEPARATOR.
'D: \ Zeal \ PHP_LIBS '. PATH_SEPARATOR.
Get_include_path ());
/**
* The following is an example. The optional methods are different for rows starting //.
* Open the comment of the corresponding row.
* If Excel5 is used, the output content should be GBK encoded.
*/
Require_once 'phpexcel. php ';
// Uncomment
/// Require_once 'phpexcel/Writer/excel5.php'; // used for other earlier versions of xls
// Or
/// Require_once 'phpexcel/Writer/excel2007.php'; // used for excel-2007 format
// Create a processing object instance
$ ObjExcel = new PHPExcel ();
// Create a file format to write the object instance, uncomment
//// $ ObjWriter = new PHPExcel_Writer_Excel5 ($ objExcel); // used for other version formats
// Or
//// $ ObjWriter = new PHPExcel_Writer_Excel2007 ($ objExcel); // used in 2007 format
// $ ObjWriter-> setOffice2003Compatibility (true );
//*************************************
// Set the basic attributes of the document
$ ObjProps = $ objExcel-> getProperties ();
$ ObjProps-> setCreator ("Zeal Li"); // set the author
$ ObjProps-> setLastModifiedBy ("Zeal Li"); // sets the last modified person
$ ObjProps-> setTitle ("Office XLS Test Document"); // set the title
$ ObjProps-> setSubject ("Office XLS Test Document, Demo"); // you can specify a topic.
$ ObjProps-> setDescription ("Test document, generated by... the remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.