: This article describes how to use PHPExcel to read ultra-large excel files. For more information about PHP tutorials, see. Original works can be reprinted. during reprinting, you must mark the original source, author information, and this statement in hyperlink form. Otherwise, legal liability will be held. Http://ustb80.blog.51cto.com/6139482/1066505
During work, part of the content in the xlsx file is often imported into the database. We usually use PHPExcel to read data.
Through the following method, we can easily read an excel table into a php array, and then we can do whatever we want:
- $ Input_file = "data.xlsx ";
- $ ObjPHPExcel = PHPExcel_IOFactory: load ($ input_file );
- $ SheetData = $ objPHPExcel-> getSheet (0)-> toArray (null, true );
If the article ends, it will be of little value.
Fortunately, data.xlsx has tens of thousands of rows, each row has many columns, each column has a long string, and some have effects such as color. when using the above method, the memory is insufficient.
Okay, we also have ini_set to increase the memory. you can also use set_time_limit to set a long timeout value, as shown below:
- Set_time_limit (90 );
- Ini_set ("memory_limit", "1024 M ");
- $ Input_file = "data.xlsx ";
- $ ObjPHPExcel = PHPExcel_IOFactory: load ($ input_file );
- $ SheetData = $ objPHPExcel-> getSheet (0)-> toArray (null, true );
But responsibly, these are not the ultimate solutions.
I have tried to set the memory to 2 GB and the timeout to 90 seconds. I still cannot read a 4000-row dirty green table. The reason is that the toArray method saves all the processing results to the array. this method is convenient for processing simple tables, but when processing large tables, it's really a cup.
Our solution is as follows:
- Require 'Lib/PHPExcel. php ';
-
- Set_time_limit (90 );
- $ Input_file = "data.xlsx ";
- $ ObjPHPExcel = PHPExcel_IOFactory: load ($ input_file );
-
- // Read rules
- $ Sheet_read_arr = array ();
- $ Sheet_read_arr ["sheet1"] = array ("A", "B", "C", "D", "F ");
- $ Sheet_read_arr ["sheet2"] = array ("A", "B", "C", "D", "F ");
-
- // Loop all pages
- Foreach ($ sheet_read_arr as $ key => $ val)
- {
- $ CurrentSheet = $ objPHPExcel-> getSheetByName ($ key); // Obtain the current page by page name
- $ Row_num = $ currentSheet-> getHighestRow (); // number of current page rows
-
- // The loop starts from the second row. the first row is usually the header.
- For ($ I = 2; $ I <= $ row_num; $ I ++)
- {
- $ Cell_values = array ();
- Foreach ($ val as $ cell_val)
- {
- $ Address = $ cell_val. $ I; // cell coordinate
-
- // Read the cell content
- $ Cell_values [] = $ currentSheet-> getCell ($ address)-> getFormattedValue ();
- }
-
- // Check the data
- Print_r ($ cell_values );
- }
- }
The above method is complicated. to read all the cells, use the following method:
- Require 'Lib/PHPExcel. php ';
-
- Set_time_limit (90 );
- $ Input_file = "data.xlsx ";
- $ ObjPHPExcel = PHPExcel_IOFactory: load ($ input_file );
-
- $ Sheet_count = $ objPHPExcel-> getSheetCount ();
- For ($ s = 0; $ s <$ sheet_count; $ s ++)
- {
- $ CurrentSheet = $ objPHPExcel-> getSheet ($ s); // current page
- $ Row_num = $ currentSheet-> getHighestRow (); // number of current page rows
- $ Col_max = $ currentSheet-> getHighestColumn (); // maximum column number on the current page
-
- // The loop starts from the second row. the first row is usually the header.
- For ($ I = 2; $ I <= $ row_num; $ I ++)
- {
- $ Cell_values = array ();
- For ($ j = 'a'; $ j <$ col_max; $ j ++)
- {
- $ Address = $ j. $ I; // cell coordinate
- $ Cell_values [] = $ currentSheet-> getCell ($ address)-> getFormattedValue ();
- }
-
- // Check the data
- Print_r ($ cell_values );
- }
- }
We can change the print_r area above into a group spelling SQL statement and write it into a file, and then import it using mysql. of course, we can also directly connect to the database to insert records into the table. this is free.
Using this method, you can easily import tens of thousands of rows of records into the table, and hope to help you.
This article is from the "Fanxing technology blog" blog, please be sure to keep this source http://ustb80.blog.51cto.com/6139482/1066505
The above describes how to use PHPExcel to read ultra-large excel files, including some content, and hope to be helpful to friends who are interested in PHP tutorials.