In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to read the record of Excel file by PHP". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to read the record of Excel file in PHP" can help you solve the problem.
Install Composer first.
Composer require phpoffice/phpspreadsheet, either with or without the version number.
May be because we are on the public network agent, always can not install, finally installed, the use of Composer always can not install PHPOffice/PHPExcel, a variety of inexplicable error prompts.
Then I downloaded JetBrains PhpStorm and installed it, and the error prompt changed a little, but it didn't work yet, and it still didn't work to open OPENSSL and download certificates.
I spent 3 hours on this, and I'm really freaking out. Think about why other people can.
Why do I have to install Composer? In fact, the installation is just to write the information of PHPOffice to the configuration file, so it is convenient for programmers to carry out code hints, error detection and later run prompts in the programming environment, so what if they don't install?
Download a paid PHPOFFICE from the Internet, unpack it to a directory, quote it, read it according to the way you read it online, and get dozens of lines of error prompts:
Deprecated: Array and string offset access syntax with curly braces is deprecated in G:\ PHP2021\ HTAPP\ phpexcel\ Classes\ PHPExcel\ Shared\ String.php on line 526
.
Deprecated: Array and string offset access syntax with curly braces is deprecated in G:\ PHP2021\ HTAPP\ phpexcel\ Classes\ PHPExcel\ Reader\ Excel5.php on line 1928
.
Deprecated: Array and string offset access syntax with curly braces is deprecated in G:\ PHP2021\ HTAPP\ phpexcel\ Classes\ PHPExcel\ Calculation.php on line 2551
.
Deprecated: Array and string offset access syntax with curly braces is deprecated in G:\ PHP2021\ HTAPP\ phpexcel\ Classes\ PHPExcel\ Worksheet\ AutoFilter.php on line 720
.
Deprecated: Array and string offset access syntax with curly braces is deprecated in G:\ PHP2021\ HTAPP\ phpexcel\ Classes\ PHPExcel\ Cell.php on line 812
This means: it is not recommended to use braced arrays and string offset access syntax, this display has dozens of lines, which is frustrating, because the class library was written by someone else, originally intended to give up, and pulled the web page to the end. it's amazing that there are data records to read, which means that as long as the error is solved.
Open the files with error prompts one by one, and change the "{}" access mode of the array to "[]". If there are too many errors, it will take half an hour to change the problem.
Got it! All the file records are displayed, and you can read either the file with the suffix xls or the file with xlsx.
Then it is written to the database.
This method is still a little troublesome, later to find other methods, at least ODBC should be possible.
Supplement
PHP can not only read the records of Excel files, but also generate Excel files.
The function arrayToExcel in the following code is to generate an excel file of a two-dimensional array of data and save it on the server.
Require_once 'Classes/PHPExcel/Reader/Excel2007.php';require_once' Classes/PHPExcel/Reader/Excel5.php';include 'Classes/PHPExcel/IOFactory.php';function arrayToExcel ($data) {$objPHPExcel = new PHPExcel (); $objPHPExcel- > setActiveSheetIndex (0); $objPHPExcel- > getActiveSheet ()-> setTitle (' firstsheet'); $objPHPExcel- > getDefaultStyle ()-> getFont ()-> setName ('Arial'); $objPHPExcel- > getDefaultStyle ()-> getFont ()-> setSize (10); / / add data$i = 2 Foreach ($data as $line) {$objPHPExcel- > getActiveSheet ()-> setCellValue ('Aspir.Secreti, $line [' From']); $objPHPExcel- > getActiveSheet ()-> getCell ('Apati.Secreti)-> setDataType (' n'); $objPHPExcel- > getActiveSheet ()-> setCellValue ('Bread.Secreti, $line [' To']); $objPHPExcel- > getActiveSheet ()-> getCell ('Bread.Secreti)-> setDataType (' n'); $iTunes;} $objWriter = PHPExcel_IOFactory::createWriter ($objPHPExcel, 'Excel5'); $file =' excel.xls';$objWriter- > save ($file);}
If you do not want to save it on the server and want to download it directly to the client after the generation, you can add the following code to the output file instead of using $objWriter- > save ($file)
The code is as follows:
Header ("Pragma: public"); header ("Expires: 0"); header ("Cache-Control:must-revalidate, post-check=0, pre-check=0"); header ("Content-Type:application/force-download"); header ("Content-Type:application/vnd.ms-execl"); header ("Content-Type:application/octet-stream"); header ("Content-Type:application/download"); header ('Content-Disposition:attachment;filename= "excel.xls'); header (" Content-Transfer-Encoding:binary ") $objWriter- > save ('php://output'); that's all for "how PHP can read the records of Excel files". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.