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 '
' . $cell->getValue() . ' | ';18 19 }20 if( $i == 0 ){21 echo '
';22 }23 $i++;24 ?>25 26
29
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