PHP Excel processing (non-original)

Source: Internet
Author: User
Tags php excel
$ ObjPHPExcel-& gt; getActiveSheet ()-& gt; setTitle (iconv ('gbk', 'utf-8 ′, 'data report (this table is automatically generated by the program in the data center of Guangheng) '); // you can specify the default font and size.

The basic usage is divided into three parts:
I. INTRODUCTION interface

// PHPExcel_IOFactory

Require_once dirname (_ FILE _). '/Classes/PHPExcel/IOFactory. php ';

II. define EXCEL objects
Define a php excel Object and set the content displayed in the EXCEL Object

// Start with Excel //////////////////////////////////// //////////////////////////////////////// /////

// Prepare EXCEL files

// Error reporting

Error_reporting (E_ALL );

// PHPExcel

Require_once dirname (_ FILE _). '/../Classes/PHPExcel. php ';

Require_once dirname (_ FILE _). '/../Classes/PHPExcel/RichText. php ';

// Generate a new excel Object

$ ObjPHPExcel = new PHPExcel ();

// Set the excel document attributes

$ ObjPHPExcel-> getProperties ()-> setCreator ("Sun Star Data Center ")

-> SetLastModifiedBy ("Sun Star Data Center ")

-> SetTitle ("Microsoft Office Excel Document ")

-> SetSubject ("Test Data Report-From Sunstar Data Center ")

-> SetDescription ("LD Test Data Report, Generate by Sunstar Data Center ")

-> SetKeywords ("sunstar data report ")

-> SetCategory ("Test result file ");

// Start to operate the excel table

// Operate the first worksheet

$ ObjPHPExcel-> setActiveSheetIndex (0 );

// Set the name of the workbook.

$ ObjPHPExcel-> getActiveSheet ()-> setTitle (iconv ('gbk', 'utf-8', 'data report (This report is automatically generated by the Guangheng data center program) '));

// Set the default font and size

$ ObjPHPExcel-> getDefaultStyle ()-> getFont ()-> setName (iconv ('gbk', 'utf-8', ' '));

$ 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 $ m_strOutputExcelFileName directly from 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 =". $ m_strOutputExcelFileName );

Header ("Content-Transfer-Encoding: binary ");

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

}

// Output 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,

'Color' => array (

'Arg' => '123 ′,
),
),

'Alignment '=> array (

'Horizontal '=> PHPExcel_Style_Alignment: HORIZONTAL_CENTER,
),
);

// Set cell A1 to bold and centered

$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('A1')-> applyFromArray ($ styleArray1 );

Write content to a specific cell:

$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('A1', 'Hello Baba ');

Set the cell style (center ):

$ ObjPHPExcel-> getActiveSheet ()-> getStyle ('h5 ')-> getAlignment ()-> setHorizontal (PHPExcel_Style_Alignment: HORIZONTAL_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 ('../get/detail/images/logo_01.jpg ');

$ ObjDrawing-> setWidth (400 );

$ ObjDrawing-> setHeight (123 );

$ ObjDrawing-> setCoordinates ('j1 ′);

$ ObjDrawing-> setWorksheet ($ objPHPExcel-> getActiveSheet ());

Set hyperlinks in cells:
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('j9', iconv ('gbk', 'utf-8', 'Google '));
$ ObjPHPExcel-> getActiveSheet ()-> getCell ('j9')-> getHyperlink ()-> setUrl ('http: // www.g.cn /');

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.