Php generates and reads excel files

Source: Internet
Author: User
Report generation and reading are often used in the website management background. CSV and Excel are common report formats. This article describes how to generate and Read excel files in php, you can find out what you need. Tables are usually generated on websites. CSV and Excel are commonly used Report formats. CSV is relatively simple. if you have any questions, I will release some CSV instances one after another, this section describes 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.

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:

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:

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:

 * @ Version 1.01 */error_reporting (E_ALL); date_default_timezone_set ('Asia/Shanghai');/** PHPExcel_IOFactory */require_once '.. /Classes/PHPExcel/IOFactory. php'; // Check prerequisitesif (! File_exists ("31excel5.xls") {exit ("not found 31excel5.xls. n ") ;}$ reader = PHPExcel_IOFactory: createReader ('excel5'); // Set to Excel5 (Excel97-2003 Workbook) $ PHPExcel = $ reader-> load ("31excel5.xls"); // load an excel file $ sheet = $ PHPExcel-> getSheet (0); // read first ?? Worksheet $ highestRow = $ sheet-> getHighestRow (); // get the total number of rows $ highestColumm = $ sheet-> getHighestColumn (); // obtain 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 rows for ($ column = 'a'; $ column <= $ highestColumm; $ column ++) {// The number of columns starts with column A $ dataset [] = $ sheet-> getCell ($ column. $ row)-> getValue (); echo $ column. $ row. ":". $ sheet-> getCell ($ column. $ row)-> getValue ()."
";}}?>

I hope this article will be helpful to you. php will introduce you to generating and reading excel files. We hope you will continue to follow our website! If you want to learn php, you can continue to pay attention to this site.

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.