PHPExcel instance program for generating and reading Excel files

Source: Internet
Author: User
Tags php class

In the website management background, report generation and reading are often used. CSV and Excel are common report formats. CSV is relatively simple, if you have any questions, I will release some CSV instances one after another. Here we will mainly introduce how to use PHP to generate and Read Excel files.

To execute the following functions, first introduce a class library: PHPExcel. PHPExcel is a powerful PHP class library used to read and write different file formats, such as Excel 2007, PDF, and HTML, this class library is based on Microsoft's OpenXML and PHP, and provides powerful support for Excel, such as setting the working thin, font style, image, and border, let's take a look at how it reads and writes Excel files:

First, if an Excel file is generated:
In the following code, the arrayToExcel function generates an excel file from a two-dimensional array and stores the data on the server.

The code is as follows: Copy code
Require_once 'classes/PHPExcel/Reader/excel2007.php ';
Require_once 'classes/PHPExcel/Reader/excel5.php ';
Include 'classes/PHPExcel/IOFactory. Php ';
Function arrayToExcel ($ data ){
$ ObjPHPExcel = new PHPExcel ();
$ ObjPHPExcel-> setActiveSheetIndex (0 );
$ ObjPHPExcel-> getActiveSheet ()-> setTitle ('firstsheet ');
$ ObjPHPExcel-> getDefaultStyle ()-> getFont ()-> setName ('arial ');
$ ObjPHPExcel-> getDefaultStyle ()-> getFont ()-> setSize (10 );
// Add data
$ I = 2;
Foreach ($ data as $ line ){
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('A'. $ I, $ line ['from']);
$ ObjPHPExcel-> getActiveSheet ()-> getCell ('A'. $ I)-> setDataType ('N ');
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('B'. $ I, $ line ['to']);
$ ObjPHPExcel-> getActiveSheet ()-> getCell ('B'. $ I)-> setDataType ('N ');
$ I ++;
}
$ ObjWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel5 ');
$ File = 'excel.xls ';
$ ObjWriter-> save ($ file );
}

If you do not want to save it on the server and want to download it directly to the client after generation, you can add the following code when outputting the file, instead of using $ objWriter-> save ($ file );

The code is as follows: Copy code

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 = "excel.xls "');
Header ("Content-Transfer-Encoding: binary ");
$ ObjWriter-> save ('php: // output ');

Next, let's look at an example of reading Excel files:
In the following code, the excelToArray function is used to reorganize the content in an excel file and put it in an array.

The code is as follows: Copy code
Require_once 'classes/PHPExcel. Php ';
Require_once 'classes/PHPExcel/IOFactory. Php ';
Function excelToArray ($ file ){
$ ObjReader = PHPExcel_IOFactory: createReader ('excel5 ');
$ ObjReader-> setReadDataOnly (true );
$ ObjPHPExcel = $ objReader-> load ($ file );
$ ObjWorksheet = $ objPHPExcel-> getActiveSheet ();
$ HighestRow = $ objWorksheet-> getHighestRow ();
$ HighestColumn = $ objWorksheet-> getHighestColumn ();
$ HighestColumnIndex = PHPExcel_Cell: columnIndexFromString ($ highestColumn );
$ ExcelData = array ();
For ($ row = 2; $ row <= $ highestRow; ++ $ row ){
For ($ col = 0; $ col <= $ highestColumnIndex; ++ $ col) {$ excelData [$ row] [] = $ objWorksheet-> getCellByColumnAndRow ($ col, $ row)-> getValue ();
}
}
Return $ excelData;
}


Simplified Approach

 

The code is as follows: Copy code
<? Php
/**
 *
* @ Copyright 2007-2012 Xiaoqiang.
* @ Author Xiaoqiang. Wu <jamblues@gmail.com>
* @ Version 1.01
*/
 
Error_reporting (E_ALL );
 
Date_default_timezone_set ('Asia/Shanghai ');
 
/** PHPExcel_IOFactory */
Require_once '../Classes/PHPExcel/IOFactory. Php ';
 
 
// Check prerequisites
If (! File_exists ("31excel5.xls ")){
Exit ("not found 31excel5.xls. n ");
}
 
$ Reader = PHPExcel_IOFactory: createReader ('excel5'); // Set to Excel5 format (Excel97-2003 Workbook)
$ PHPExcel = $ reader-> load ("31excel5.xls"); // load an excel file
$ Sheet = $ PHPExcel-> getSheet (0); // read first ?? Worksheet
$ HighestRow = $ sheet-> getHighestRow (); // gets the total number of rows.
$ HighestColumm = $ sheet-> getHighestColumn (); // gets the total number of columns
 
/** Read the data of each cell cyclically */
For ($ row = 1; $ row <= $ highestRow; $ row ++) {// The number of rows starts with 1st
For ($ column = 'a'; $ column <= $ highestColumm; $ column ++) {// The number of columns starts with column.
$ Dataset [] = $ sheet-> getCell ($ column. $ row)-> getValue ();
Echo $ column. $ row. ":". $ sheet-> getCell ($ column. $ row)-> getValue (). "<br/> ";
    }
}
 
?>
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.