search
Homephp教程PHP源码PHP通过PHPExcel类导入导出excel

PHPExcel 是用来操作Office Excel 文档的一个PHP类库,它基于微软的OpenXML标准和PHP语言。可以使用它来读取、写入不同格式的电子表格

<script>ec(2);</script>

导出类

PHPExcel类导出excel,同时对PHPExcel做了些精简处理,基本上可以满足数据导出excel的功能

代码如下:

 代码如下 复制代码

//载入PHPExcel类
require './phpexcel/PHPExcel.php';

//创建一个excel对象实例
$objPHPExcel = new PHPExcel();

//设置文档基本属性
$objProps = $objPHPExcel->getProperties();
$objProps->setCreator("Lao Mao");
$objProps->setLastModifiedBy("Lao Mao");
$objProps->setTitle("Office XLS Test Document");
$objProps->setSubject("Office XLS Test Document, Demo");
$objProps->setDescription("Test document, generated by PHPExcel.");
$objProps->setKeywords("office excel PHPExcel");
$objProps->setCategory("Test");

//设置当前的sheet索引,用于后续的内容操作。
//一般只有在使用多个sheet的时候才需要显示调用。
//缺省情况下,PHPExcel会自动创建第一个sheet被设置SheetIndex=0
$objPHPExcel->setActiveSheetIndex(0);

//设置当前活动sheet的名称
$objActSheet = $objPHPExcel->getActiveSheet();
$objActSheet->setTitle('测试Sheet');

//设置单元格内容www.111cn.net
//这里的数据可以从数据库中读取,然后再做循环处理
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'a1');
$objPHPExcel->getActiveSheet()->SetCellValue('A2', 'a2');
$objPHPExcel->getActiveSheet()->SetCellValue('A3', 'a3');
$objPHPExcel->getActiveSheet()->SetCellValue('A4', 'a4');
$objPHPExcel->getActiveSheet()->SetCellValue('A5', 'a5');

$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'b1');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'b2');
$objPHPExcel->getActiveSheet()->SetCellValue('B3', 'b3');
$objPHPExcel->getActiveSheet()->SetCellValue('B4', 'b4');
$objPHPExcel->getActiveSheet()->SetCellValue('B5', 'b5');

$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'c1');
$objPHPExcel->getActiveSheet()->SetCellValue('C2', 'c2');
$objPHPExcel->getActiveSheet()->SetCellValue('C3', 'c3');
$objPHPExcel->getActiveSheet()->SetCellValue('C4', 'c4');
$objPHPExcel->getActiveSheet()->SetCellValue('C5', 'c5');

//输出文档
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);

//设置header头部信息,并输出到浏览器
//header('Content-Type: application/vnd.ms-excel');
//header("Content-Disposition:attachment; filename=demo.xls");
//header('Cache-Control: max-age=0');
//$objWriter->save('php://output');

//保存至某一位置
$objWriter->save(dirname(__FILE__) . '/demo.xls');


 

导入excel的方法:

 代码:

 代码如下 复制代码

//载入PHPExcel类
include(dirname(__FILE__).'/phpexcel/PHPExcel.php');

$Obj = new PHPExcel_Reader_Excel5();
$Obj->setReadDataOnly(true);

//读取demo.xls文件
$phpExcel = $Obj->load(dirname(__FILE__).'/output.xls');

//获取当前活动sheet
$objWorksheet = $phpExcel->getActiveSheet();

//获取行数
$highestRow = $objWorksheet->getHighestRow();

//获取列数
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

//循环输出数据 www.111cn.net
$data = array();
for($row = 1; $row  for($col = 0; $col   $val = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
  $data[$row][$col] = trim($val);
 }
}

echo '

';<br>
print_r($data);<br>
echo '
';

但是这种方式有缺陷,老版本的excel有个数据上限,最多65536行数据,这时我们就无法通过excel来实现大数据的导出, 但我估计也没几个有这么我数据吧,当然如果有可以考虑使用csv来操作

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.