PHPExcel usage-PHP source code

Source: Internet
Author: User
For more information about PHPExcel usage, see. For more information about PHPExcel usage, see.

Script ec (2); script

PHPExcel: http://phpexcel.codeplex.com/

Import PHPExcel. php or other files

The Code is as follows:

Require_once 'phpexcel. php ';

/// 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 = newPHPExcel_Writer_Excel5 ($ objExcel); // used for other version formats

// Or

//// $ ObjWriter = newPHPExcel_Writer_Excel2007 ($ objExcel); // used in 2007 format

$ ObjProps = $ objExcel-> getProperties ();

// Set the Creator

$ ObjProps-> setCreator ('xulu ');

// Set the last Modifier

$ ObjProps-> setLastModifiedBy ("XuLulu ");

// Description

$ ObjProps-> setDescription ("MoBi class ");

// Set the title

$ ObjProps-> setTitle ('manager ');

// Set the question

$ ObjProps-> setSubject ("OfficeXLS Test Document, Demo ");

// Set keywords

$ ObjProps-> setKeywords ('manager ');

// Set the category

$ ObjProps-> setCategory ("Test ");

// Worksheet settings

$ ObjExcel-> setActiveSheetIndex (0 );

$ ObjActSheet = $ objExcel-> getActiveSheet ();

// Cell assignment example:

$ ObjActSheet-> setCellValue ('a1', 'id ');

$ ObjActSheet-> setCellValue ('b1 ', 'hashcode ');

$ ObjActSheet-> setCellValue ('c1', 'modelname ');

$ ObjActSheet-> setCellValue ('d1 ', 'indexname ');

$ 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

// Example of automatically setting the cell width:

$ ObjActSheet-> getColumnDimension ('A')-> setAutoSize (true );

// Example of manually setting the cell width:

// $ ObjActSheet-> getColumnDimension ('A')-> setWidth (10 );

// Exported file name

$ OutputFileName = iconv ('utf-8', 'gb2312', 'xululu _ '. time (). '.xlsx ');

// Export the file directly

$ ObjWriter-> save ($ outputFileName );

// Directly output the file to the browser

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-excel ');

Header ('content-Type: application/octet-stream ');

Header ('content-Type: application/download ');

Header ('content-Disposition: attachment; filename = '. $ outputFileName );

Header ('content-Transfer-Encoding: binary ');

$ ObjWriter-> save ('php: // output ');

Other settings:

// Explicitly specify the content type

$ ObjActSheet-> setCellValueExplicit ('a5 ', '123 ',

PHPExcel_Cell_DataType: TYPE_STRING );

// Merge Cells

$ ObjActSheet-> mergeCells ('b1: c22 ');

// Separate cells

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

// Get the cell style

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

// 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 byzeal ');

$ ObjDrawing-> setPath ('./zeali.net.logo.gif ');

$ ObjDrawing-> setHeight (36 );

$ 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 ');

PHPExcel is used in cakephp:

Create a folder Excel under Vendors/. The PHPExcel directory is as follows:

Write the following code for the method under the Controller to be called:

The Code is as follows:
App: import ('upload', 'excel ', array ('file' => 'phpexcel. php '));

You can instantiate PHPExcel.

(This is only one of the methods, and there are other methods !)

In addition, you can export an excel table in other forms, such as the fputcsv () function of php. You can also export a table in csv format.

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.