Use PHP to read Excel files. In this article, we will introduce in detail some examples of PHP Excel file reading code :? Phprequire_once... Excelreader. php; ExcelFile ($ filename, $ encoding); $ data we will introduce you in detail in this article
PHP code example for reading Excel files:
- <? Php
- Require_once '../Excel/reader. php ';
- // ExcelFile ($ filename, $ encoding );
- $ Data = new Spreadsheet_Excel_Reader ();
- // Set output Encoding. specify the Chinese code
- $ Data->SetOutputEncoding ('gb2312 ');
- /***
- * If you want you can change 'iconv'
To mb_convert_encoding:
- * $ Data->SetUTFEncoder ('mb ');
- *
- **/
- /***
- * By default rows & cols indeces start with 1
- * For change initial index use:
- * $ Data->SetRowColOffset (0 );
- *
- **/
- /***
- * Some function for formatting output.
- * $ Data->SetDefaultFormat ('%. 2f ');
- * SetDefaultFormat-set format
Columns with unknown formatting
- *
- * $ Data->SetColumnFormat (4, '%. 3f ');
- * SetColumnFormat-set format
Column (apply only to number fields)
- *
- **/
- // Specify the excel file to be read
- $ Data->Read ('../uploads/jsxz01.xls ');
- /*
- $ Data->Sheets [0] ['numrows ']-count rows
- $ Data->Sheets [0] ['numcols']-count columns
- $ Data->Sheets [0] ['cells '] [$ I] [$ j]-
Data from $ I-row $ j-column
- $ Data->Sheets [0] ['cellsinfo'] [$ I] [$ j]-
Extended info about cell
- $ Data->Sheets [0] ['cellsinfo'] [$ I] [$ j]
['Type'] = "date" | "number" | "unknown"
- If 'type' = "unknown"-use 'raw'
Value, because cell contain value
Format '0. 00 ';
- $ Data->Sheets [0] ['cellsinfo'] [$ I] [$ j]
['Raw'] = value if cell without format
- $ Data->Sheets [0] ['cellsinfo'] [$ I] [$ j]
['Colspan ']
- $ Data->Sheets [0] ['cellsinfo'] [$ I] [$ j]
['Rowspan ']
- */
- Error_reporting (E_ALL ^ E_NOTICE );
- // Read each unit value cyclically
- For ($ I = 1; $ I<= $ Data->Sheets [0]
['Numrows ']; $ I ++ ){
- For ($ j = 1; $ j<= $ Data->Sheets [0]
['Numcols']; $ j ++ ){
- Echo ". $ data->Sheets [0] ['cells]
[$ I] [$ j]. "", ";
- }
- Echo"
";
- }
- // Print_r ($ data );
- // Print_r ($ data->FormatRecords );
- ?>
The above is the full usage of PHP to read Excel files as a reference object.
Example of PHP code for reading Excel files :? Php require_once '../Excel/reader. php'; // ExcelFile ($ filename, $ encoding); $ data...