Php uses phpexcel to generate an excel document instance-PHP source code

Source: Internet
Author: User
Tags getcolor
Ec (2); ini_set (& quot; display_errors & quot;, 1); Whether to display the error message ini_set (include_path, ini_get (include_path ).; d: www.111cn. nethtdocslianxi1.7.2Classes); set this page to contain the path require_once (& quot ;. 1.7.2 script ec (2); script

// Ini_set ("display_errors", 1); // whether the error message is displayed
// Ini_set ('include _ path', ini_get ('include _ path '). '; D: \ www.111cn.net \ htdocs \ lianxi \ 1.7.2 \ Classes'); // set the path of the page

Require_once ("./1.7.2/Classes/PHPExcel. php ");
Require_once ("./1.7.2/Classes/PHPExcel/Writer/Excel5.php ");

$ ObjExcel = new PHPExcel ();
// Print_r ($ objExcel );
$ ObjWriter = new PHPExcel_Writer_Excel5 ($ objExcel );
// $ ObjWriter = new PHPExcel_Writer_Excel2007 ($ objExcel); // used in 2007 format
// $ ObjWriter-> setOffice2003Compatibility (true );

// Set the basic attributes of the document www.111cn.net
$ ObjProps tutorial = $ objExcel-> getProperties ();
// Print_r ($ objProps );
$ ObjProps-> setCreator ("Zeal Li ");
$ ObjProps-> setLastModifiedBy ("Zeal Li ");
$ ObjProps-> setTitle ("Office XLS Test Document ");
$ ObjProps-> setSubject ("Office XLS Test Document, Demo ");
$ ObjProps-> setDescription ("Test document, generated by PHPExcel .");
$ ObjProps-> setKeywords ("office excel PHPExcel ");
$ ObjProps-> setCategory ("Test ");

//*************************************
// Set the current sheet index for subsequent content operations.
// Display the call only when multiple sheets are used.
// By default, PHPExcel will automatically create the first sheet and set SheetIndex = 0

$ ObjExcel-> setActiveSheetIndex (0 );


$ ObjActSheet = $ objExcel-> getActiveSheet ();
$ ObjActSheet = $ objExcel-> getActiveSheet ();
// Set the name of the current active sheet
$ ObjActSheet-> setTitle ('test Sheet ');

//*************************************
// Set the cell content
//
// PHPExcel automatically determines the cell content type based on the input content
$ ObjActSheet-> setCellValue ('a1', 'string content'); // string content
$ ObjActSheet-> setCellValue ('a2 ', '26'); // Value
$ ObjActSheet-> setCellValue ('a3 ', true); // Boolean Value
$ ObjActSheet-> setCellValue ('a4 ',' = SUM (A2: A2) '); // Formula

// Explicitly specify the content type
$ ObjActSheet-> setCellValueExplicit ('a5 ', '000000', PHPExcel_Cell_DataType: TYPE_STRING );
// Merge Cells
$ ObjActSheet-> mergeCells ('b1: c22 ');

// Separate cells
$ ObjActSheet-> unmergeCells ('b1: c22 ');

//*************************************
// Set the cell style
//

// Set the width
$ ObjActSheet-> getColumnDimension ('B')-> setAutoSize (true );
$ ObjActSheet-> getColumnDimension ('A')-> setWidth (30 );

$ ObjStyleA5 = $ objActSheet-> getStyle ('a5 ');

// Set the numeric format of the cell content.
//
// If PHPExcel_Writer_Excel5 is used to generate the content,
// Note that the const variable defined in the PHPExcel_Style_NumberFormat class
// Other types of custom formatting methods can be used normally, but when setFormatCode
// When FORMAT_NUMBER is used, the actual effect is not set to "0 ". Yes
// Modify the getXf ($ style) method in the source code of the PHPExcel_Writer_Excel5_Format class,
// Add one before if ($ this-> _ BIFF_version = 0x0500) {(near row 363rd)
// Line of code:
// If ($ ifmt = '0') $ ifmt = 1;
//
// Set the format to PHPExcel_Style_NumberFormat: FORMAT_NUMBER to avoid large numbers.
// The data is displayed in scientific notation. The following setAutoSize method can be used to display the content of each row.
// All are displayed based on the original content.
$ ObjStyleA5-> getNumberFormat ()-> setFormatCode (PHPExcel_Style_NumberFormat: FORMAT_NUMBER );

// Set the font
$ ObjFontA5 = $ objStyleA5-> getFont ();
$ ObjFontA5-> setName ('courier new ');
$ ObjFontA5-> setSize (10 );
$ ObjFontA5-> setBold (true );
$ ObjFontA5-> setUnderline (PHPExcel_Style_Font: UNDERLINE_SINGLE );
$ ObjFontA5-> getColor ()-> setARGB ('ff9999999999 ');

// Set alignment
$ ObjAlignA5 = $ objStyleA5-> getAlignment ();
$ ObjAlignA5-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_RIGHT );
$ ObjAlignA5-> setVertical (PHPExcel_Style_Alignment: VERTICAL_CENTER );

// Set the border
$ ObjBorderA5 = $ objStyleA5-> getBorders ();
$ ObjBorderA5-> getTop ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
$ ObjBorderA5-> getTop ()-> getColor ()-> setARGB ('ffff000000'); // color
$ ObjBorderA5-> getBottom ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
$ ObjBorderA5-> getLeft ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );
$ ObjBorderA5-> getRight ()-> setBorderStyle (PHPExcel_Style_Border: BORDER_THIN );

// Set the fill color
$ ObjFillA5 = $ objStyleA5-> getFill ();
$ ObjFillA5-> setFillType (PHPExcel_Style_Fill: FILL_SOLID );
$ ObjFillA5-> getStartColor ()-> setARGB ('ffeeeeeee ');

// Copy the style information from the specified cell.
$ ObjActSheet-> duplicateStyle ($ objStyleA5, 'b1: C22 ');

//*************************************
// Add an image
$ ObjDrawing = new PHPExcel_Worksheet_Drawing ();
$ ObjDrawing-> setName ('zealimg ');
$ ObjDrawing-> setDescription ('image inserted by zeal ');
$ ObjDrawing-> setPath ('../images/201003/1269919647975424741 .jpg ');
$ ObjDrawing-> setHeight (150 );
$ ObjDrawing-> setCoordinates ('c23 ');
$ ObjDrawing-> setOffsetX (10 );
$ ObjDrawing-> setRotation (15 );
$ ObjDrawing-> getShadow ()-> setVisible (true );
$ ObjDrawing-> getShadow ()-> setDirection (36 );
$ ObjDrawing-> setWorksheet ($ objActSheet );

// Add a new worksheet
$ ObjExcel-> createSheet ();
$ ObjExcel-> getSheet (1)-> setTitle ('test 2 ');

// Protect cells
$ ObjExcel-> getSheet (1)-> getProtection ()-> setSheet (true );
$ ObjExcel-> getSheet (1)-> protectCells ('a1: c22', 'phpexcel ');
//*************************************
// Output content
//
$ OutputFileName = "output.xls ";
// To the file
$ ObjWriter-> save ($ outputFileName );
// Or
// Go to the browser
/*
Header ("Content-Type: application/force-download ");
Header ("Content-Type: application/octet-stream ");
Header ("Content-Type: application/download ");
Header ('content-Disposition: inline; filename = "'. $ outputFileName .'"');
Header ("Content-Disposition: attachment?filename= .xls ");
Header ("Content-Transfer-Encoding: binary ");
Header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT ");
Header ("Last-Modified:". gmdate ("D, d m y h: I: s"). "GMT ");
Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
Header ("Pragma: no-cache ");
$ ObjWriter-> save ('php: // output ');

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.