Therefore, the first step is to extract the data from the Excel file. Here I used an open source PHP processing Excel class: phpexcel. Detailed information for this project http://phpexcel.codeplex.com /.
I currently use phpexcel1.7.3. After decompression, there is a phpexcel and phpexcel. php file.
We mainly use the PHP file. See the file directory structure.
This version is said to support excel2007, but XLSX edited by 2007 cannot be supported by this library. So I will convert it to 2003. I feel very supportive.
The following describes the specific usage:CopyCodeThe Code is as follows: require_once ('./phpexcel1.7.3/phpexcel. php ');
$ Php_excel_obj = new phpexcel ();
$ Php_reader = newphpexcel_reader_excel2007 ();
If (! $ Php_reader-> Canread ($ file_name ))
{
$ Php_reader = new phpexcel_reader_excel5 ();
If (! $ Php_reader-> Canread ($ file_name ))
{
Echo 'no excel! ';
}
}
$ Php_excel_obj = $ php_reader-> load ($ file_name );
$ Current_sheet = $ php_excel_obj-> getsheet (0 );
The main function above is to initialize the relevant Excel class and load the first sheet in Excel
$ All_column = $ current_sheet-> gethighestcolumn ();
$ All_row = $ current_sheet-> gethighestrow ();
Obtain the maximum column values (for example, 'G') and the maximum number of rows (For numeric values) of the table)
Here we will read the data in Excel in a loop:Copy codeThe Code is as follows: $ all_arr = array ();
$ C_arr = array ();
// Character table
For ($ r_ I = 1; $ r_ I <= $ all_row; $ r_ I ++)
{
$ C_arr = array ();
For ($ C_ I = 'a'; $ C_ I <= 'B'; $ C_ I ++)
{
$ ADR = $ C_ I. $ r_ I;
$ Value = $ current_sheet-> getcell ($ ADR)-> getvalue ();
If ($ C_ I = 'A' & empty ($ value ))
Break;
If (is_object ($ value ))
$ Value = $ value->__ tostring ();
$ C_arr [$ C_ I] = $ value;
}
$ C_arr & $ all_arr [] = $ c_arr;
}
The following is a brief introduction to the write operations in phpexcel. This operation is often used to import the data in the database to excel for easy display and more beautiful results.Copy codeThe Code is as follows: require_once ('./phpexcel1.7.3/phpexcel. php ');
$ Excel_obj = new phpexcel ();
$ Objwriter = newphpexcel_writer_excel5 ($ excel_obj );
$ Excel_obj-> setactivesheetindex (0 );
$ Act_sheet_obj = $ excel_obj-> getactivesheet ();
$ Act_sheet_obj-> settitle ('sheet ');
$ Act_sheet_obj-> setcellvalue ('a1', 'string content ');
$ Act_sheet_obj-> setcellvalue ('a2 ', 26 );
$ File_name = "output.xls ";
$ Objwriter-> Save ($ file_name );
The code is very simple. First, initialize the relevant Excel writing class, then write data, and finally save it as an xls file.
The output result is shown in the figure below.