Using PHPExcel in PHP to export Excel example _ PHP Tutorial

Source: Internet
Author: User
Tags export class php excel
Use PHPExcel to export an Excel example in PHP. One of the simplest ways to export excel files in php is to export csv files. however, to export an excel file, you can use the PHPExcel plug-in. PHPExcel is a powerful way to export excel files in php. The simplest way is to export csv files. however, to export excel files, you can use the PHPExcel plug-in.

PHPExcel is a powerful PHP Excel class library, but it is a bit complicated to simply export data using PHP to Excel. on google code, there is a PHP Excel export class, it can be called easily.

The code is as follows:

// Load library
Require 'php-excel. class. php ';
// Create a simple 2-dimen1_array
$ Data = array (
1 => array ('name', 'surname '),
Array ('Schwarz', 'Oliver '),
Array ('test', 'Peter ')
);
// Generate file (constructor parameters are optional)
$ Xls = new Excel_XML ('utf-8', false, 'My Test Sheet ');
$ Xls-> addArray ($ data );
$ Xls-> generateXML ('My-test ');
?>

Example 2

The code is as follows:

/**
* Generate an excel file in PHPEXCEL
* @ Author: firmy
* @ Desc supports generating excel files for any row and column data without adding cell styles and alignment.
*/

Require_once 'Library/PHPExcel. php ';
Require_once 'Library/PHPExcel/Reader/excel2007.php ';
Require_once 'Library/PHPExcel/Reader/excel5.php ';
Include_once 'Library/PHPExcel/IOFactory. php ';

$ FileName = "test_excel ";
$ HeadArr = array ("first column", "Second Column", "third column ");
$ Data = array (1, 2), array (1, 3), array (5, 7 ));
GetExcel ($ fileName, $ headArr, $ data );


Function getExcel ($ fileName, $ headArr, $ data ){
If (empty ($ data) |! Is_array ($ data )){
Die ("data must be a array ");
}
If (empty ($ fileName )){
Exit;
}
$ Date = date ("Y_m_d", time ());
$ FileName. = "_00000000date0000.xlsx ";

// Create a new PHP Excel Object
$ ObjPHPExcel = new PHPExcel ();
$ ObjProps = $ objPHPExcel-> getProperties ();

// Set the header
$ Key = ord ("");
Foreach ($ headArr as $ v ){
$ Colum = chr ($ key );
$ ObjPHPExcel-> setActiveSheetIndex (0)-> setCellValue ($ colum. '1', $ v );
$ Key + = 1;
}

$ Column = 2;
$ ObjActSheet = $ objPHPExcel-> getActiveSheet ();
Foreach ($ data as $ key => $ rows) {// write row
$ Span = ord ("");
Foreach ($ rows as $ keyName => $ value) {// write a column
$ J = chr ($ span );
$ ObjActSheet-> setCellValue ($ j. $ column, $ value );
$ Span ++;
}
$ Column ++;
}

$ FileName = iconv ("UTF-8", "gb2312", $ fileName );
// Rename the table
$ ObjPHPExcel-> getActiveSheet ()-> setTitle ('simple ');
// Set the activity ticket index to the first table, so open it in Excel. this is the first table.
$ ObjPHPExcel-> setActiveSheetIndex (0 );
// Redirect the output to a client web browser (Excel2007)
Header ('content-Type: application/vnd. openxmlformats-officedocument.spreadsheetml.sheet ');
Header ("Content-Disposition: attachment; filename =" $ fileName "");
Header ('cache-Control: max-age = 0 ');
$ ObjWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel2007 ');
If (! Empty ($ _ GET ['Excel ']) {
$ ObjWriter-> save ('php: // output'); // download the file in a browser
} Else {
$ ObjWriter-> save ($ fileName); // Run in script mode and save it in the current directory
}
Exit;

}

Phpexcel class: http://php-excel.googlecode.com/files/php-excel-v1.1-20090910.zip

Bytes. PHPExcel is very powerful...

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.