Original works, reprinting is allowed. When reprinting, please be sure to indicate the article original source , author information and this statement in the form of a hyperlink. Otherwise held liable. http://ustb80.blog.51cto.com/6139482/1066505
In my work, I often encounter the problem of importing part of the xlsx file into the database. Usually we use PHPExcel to read.
We can easily read an excel table into a php array through the following method, 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, true, true);
If the article ends here, it will not be of much value.
Unfortunate situations always exist. When data.xlsx has tens of thousands of rows, each row has many columns, each column has a long string, and some have color and other effects, use the above What often happens with methods is that they run out of memory.
Well, we still have ini_set to increase the memory, and we can also use set_time_limit to set a longer timeout, as follows:
- set_time_limit(90);
- ini_set("memory_limit", "1024M");
- $input_file = "data.xlsx";
- $objPHPExcel = PHPExcel_IOFactory::load($input_file);
- $sheetData = $objPHPExcel->getSheet(0)->toArray(null, true, true, true);
But it is very responsible to say that these are not the ultimate solution.
I once tried setting the memory to 2G and the timeout to 90 seconds, but I still couldn’t read a colorful table with 4,000 rows. The reason lies in the toArray method, which saves all the processing results into an array. This method is very convenient when processing simple tables, but it is really useless when processing large tables.
Our solution is as follows:
- require 'lib/PHPExcel.php';
- set_time_limit(90);
- $input_file = "data.xlsx";
- $objPHPExcel = PHPExcel_IOFactory::load($input_file);
- // 读取规则
- $sheet_read_arr = array();
- $sheet_read_arr["sheet1"] = array("A","B","C","D","F");
- $sheet_read_arr["sheet2"] = array("A","B","C","D","F");
- // 循环所有的页
- foreach ($sheet_read_arr as $key => $val)
- {
- $currentSheet = $objPHPExcel->getSheetByName($key);// 通过页名称取得当前页
- $row_num = $currentSheet->getHighestRow();// 当前页行数
- // 循环从第二行开始,第一行往往是表头
- for ($i = 2; $i = $row_num; $i++)
- {
- $cell_values = array();
- foreach ($val as $cell_val)
- {
- $address = $cell_val . $i;// 单元格坐标
- // 读取单元格内容
- $cell_values[] = $currentSheet->getCell($address)->getFormattedValue();
- }
- // 看看数据
- print_r($cell_values);
- }
- }
The above method is considered a more complicated situation. If you just want to read out all the cells, just 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);// 当前页
- $row_num = $currentSheet->getHighestRow();// 当前页行数
- $col_max = $currentSheet->getHighestColumn(); // 当前页最大列号
- // 循环从第二行开始,第一行往往是表头
- for($i = 2; $i = $row_num; $i++)
- {
- $cell_values = array();
- for($j = 'A'; $j $col_max; $j++)
- {
- $address = $j . $i; // 单元格坐标
- $cell_values[] = $currentSheet->getCell($address)->getFormattedValue();
- }
- // 看看数据
- print_r($cell_values);
- }
- }
We can The above print_r place is changed to combine the sql statement and write it to the file, and then use mysql to import it. Of course, you can also directly connect to the database to insert records into the table. This is optional.
Using this method, tens of thousands of rows of records can be easily imported into the table. I hope it will be helpful to everyone.
This article comes from the "Fanxing's Technology Blog" blog, please be sure to keep this source http://ustb80.blog.51cto.com/6139482/1066505
The above introduces how to use PHPExcel to read very large excel files, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment
