How to write and read Excel files using PHPExcel

墨辰丷
Release: 2023-03-27 16:58:01
Original
1736 people have browsed it

This article mainly introduces the relevant knowledge of using PHPExcel to write and read Excel files. Has very good reference value. Let’s take a look with the editor below

As a Java person, I only know after using PHP that writing and reading Excel files can be so simple!

Use PHP to read Excel, mainly with the help of PHPExcel plug-in.

PHPExcel download address: PHPExcel download

1. PHPExcel implementation steps for writing to excel

First, you need to introduce the class file and the PHPExcel.php file .

1. Create a new Excel table (instantiate the PHPExcel class)

2. Create a sheet (built-in table) (createSheet() method, setActiveSheet() method, getActiveSheet() method)

3. Fill in data (setCellValue() method)

4. Save the file (PHPExcel_IOFactory::createWriter() method, save method)

2. PHPExcel implements reading Take the Excel operation steps

First you need to introduce the class file and the IOFactory.php file.

1. Instantiate the Excel reading object

2. Load the Excel file (load all, select load)

3. Read the Excel file (read all, load one by one) Row reading)

Use PHPExcel to write and read Excel files code:

<?php
 $dir = dirname(__FILE__); //找出当前脚本所在路径
 /*require $dir.&#39;\lib\PHPExcel_1.8.0_doc\Classes\PHPExcel.php&#39;; //添加读取excel所需的类文件
 $objPHPExcel = new PHPExcel();   //实例化一个PHPExcel()对象
 $objSheet = $objPHPExcel->getActiveSheet(); //选取当前的sheet对象
 $objSheet->setTitle(&#39;helen&#39;);   //对当前sheet对象命名
 //常规方式:利用setCellValue()填充数据
 $objSheet->setCellValue("A1","张三")->setCellValue("B1","李四"); //利用setCellValues()填充数据
 //取巧模式:利用fromArray()填充数据
 $array = array(
 array("","B1","张三"),
 array("","B2","李四")
 );
 $objSheet->fromArray($array); //利用fromArray()直接一次性填充数据
 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,&#39;Excel2007&#39;); //设定写入excel的类型
 $objWriter->save($dir.&#39;/test.xlsx&#39;);*/ //保存文件
 //利用php读取excel数据
 require $dir.&#39;\lib\PHPExcel_1.8.0_doc\Classes\PHPExcel\IOFactory.php&#39;;
 $filename = $dir.&#39;\test.xlsx&#39;;
 $objPHPExcelReader = PHPExcel_IOFactory::load($filename); //加载excel文件
 foreach($objPHPExcelReader->getWorksheetIterator() as $sheet) //循环读取sheet
 {
 foreach($sheet->getRowIterator() as $row) //逐行处理
 {
  if($row->getRowIndex()<2) //确定从哪一行开始读取
  {
  continue;
  }
  foreach($row->getCellIterator() as $cell) //逐列读取
  {
  $data = $cell->getValue(); //获取cell中数据
  echo $data;
  }
  echo &#39;<br/>&#39;;
 }
 }
?>
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

php generates a cool four-character verification code_php tips

Use php to output different heart-shaped patterns_php skills

Briefly talk about include, include_once, require and require_once in PHP Statement_php skills

The above is the detailed content of How to write and read Excel files using PHPExcel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template