PHP reads Excel files

Source: Internet
Author: User
PHP reads the content of an Excel file. The project needs to read the content of an Excel file. There are two main options: PHPExcelReader and PHPExcel.

 

PHPExcelReader is lightweight and only supports reading from Excel. It is actually a Reader. However, it is a pity that Excel format (.xlsx) is not supported ).

 

PHPExcel is powerful. It can output data in the memory into an Excel file and perform various operations on Excel. The following describes how to use PHPExcel to read Excel format (.xlsx) files.

 

Download PHPExcel and save it to your class file directory. Then, use the following code to open an Excel 2007 (xlsx) file:

 

Require_once '/libs/PHPExcel-1.8.0/Classes/PHPExcel. php'; // modify to your own directory echo'

TEST PHPExcel 1.8.0: read xlsx file

'; $ ObjReader = PHPExcel_IOFactory: createReaderForFile ($ filename); $ objPHPExcel = $ objReader-> load ($ filename); $ objPHPExcel-> setActiveSheetIndex (1 ); $ date = $ objPHPExcel-> getActiveSheet ()-> getCell ('a16')-> getValue ();

  

Output the $ date variable to view the content in the file. PHPExcel uses the PHPExcel_IOFactory class to automatically match the uploaded file type. Of course, we can also customize the file type to be parsed. Then, load the PHP file to the objPHPExcel object through the load method. If the Excel file has multiple sheets, you can use setActiveSheetIndex to set the current active Sheet. I still don't know how to get the current Sheet using the Sheet name. If you know it, you can stand for me.

 

Note that, for the date format in Excel, whether the data read by PHPExcel is of the date type needs to be converted using the following method.

 

Echo date ("Y-m-d H: I: s", PHPExcel_Shared_Date: ExcelToPHP ($ date ));

 

The following code shows how to traverse and display the Excel content:

 1 
 
 
  
   2 
  getActiveSheet(); 4      $i = 0; 5      foreach($objWorksheet->getRowIterator() as $row){ 6      ?> 7           
  
  
     8 getCellIterator();10 $cellIterator->setIterateOnlyExistingCells(false);11 12 if( $i == 0 ){13 echo ' 
   
    ';14 }15 foreach($cellIterator as $cell){16 17 echo ' 
   ';18 19 }20 if( $i == 0 ){21 echo ' 
   ';22                     }23                $i++;24           ?>25           26      
  29 
 
 
' . $cell->getValue() . '

 

 

References:

1. Official PHPExcel website

2. Use PHPExcel to determine and format the date in Excel

3. PHPExcel Chinese help (knowledge point)

4. Use PHPExcel to import and export Excel files

5. PHPExcel common function Guide

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.