phpexcel是一个php类库,用来帮助我们简单、高效实现从excel读取excel的数据和导出数据到excel。也是我们日常开发中,经常会遇到的使用场景。比如有个客户信息表,要批量导出发给同事,我们就可以用phpexcel来快速实现。同样,如果我们要利用短信群发接口去群发信息,phpexcel可以快速导入客户信息,避免人工录入信息的麻烦。
phpexcel使用教程:
首先下载phpexcel
到https://github.com/phpoffice/phpexcel下载phpexcel,如果不懂得使用git,可以到这https://codeload.github.com/phpoffice/phpexcel/zip/1.8下载压缩包,懂得的自行用git下载。
下载好文件,解压可以得到如下文件:
phpexcel
好了,现在我们就可以用phpexcel愉快的读取和制作表格了!
phpexcel demo1:利用phpexcel读取excel信息:
我们在根目录创建一个名为read.php的文件来读取文档,创建一个test.xlsx的文件,里面写的信息如下:
然后在read.php写以下代码:
<?php
include ‘./classes/phpexcel/iofactory.php';
$inputfilename = ‘./test.xls';
date_default_timezone_set(‘prc');
// 读取excel文件
try {
$inputfiletype = phpexcel_iofactory::identify($inputfilename);
$objreader = phpexcel_iofactory::createreader($inputfiletype);
$objphpexcel = $objreader->load($inputfilename);
} catch(exception $e) {
die(‘加载文件发生错误:”‘.pathinfo($inputfilename,pathinfo_basename).'”: ‘.$e->getmessage());
}
// 确定要读取的sheet,什么是sheet,看excel的右下角,真的不懂去百度吧
$sheet = $objphpexcel->getsheet(0);
$highestrow = $sheet->gethighestrow();
$highestcolumn = $sheet->gethighestcolumn();
// 获取一行的数据
for ($row = 1; $row <= $highestrow; $row++){
// read a row of data into an array
$rowdata = $sheet->rangetoarray(‘a' . $row . ‘:' . $highestcolumn . $row, null, true, false);
//这里得到的rowdata都是一行的数据,得到数据后自行处理,我们这里只打出来看看效果
var_dump($rowdata);
echo “<br>”;
}
得到的数据:
phpexcel读取文件
phpexcel demo2:利用phpexcel导出信息到excel:
<?php
/** error reporting */
error_reporting(e_all);
ini_set('display_errors', true);
ini_set('display_startup_errors', true);
date_default_timezone_set('prc');
/** 引入phpexcel */
require_once dirname(__file__) . './classes/phpexcel.php';
// 创建excel文件对象
$objphpexcel = new phpexcel();
// 设置文档信息,这个文档信息windows系统可以右键文件属性查看
$objphpexcel->getproperties()->setcreator("作者简庆旺")
->setlastmodifiedby("最后更改者")
->settitle("文档标题")
->setsubject("文档主题")
->setdescription("文档的描述信息")
->setkeywords("设置文档关键词")
->setcategory("设置文档的分类");
//根据excel坐标,添加数据
$objphpexcel->setactivesheetindex(0)
->setcellvalue('a1', '你好')
->setcellvalue('b2', '世界')
->setcellvalue('c1', '你好')
->setcellvalue('d2', '世界');
// 混杂各种符号, 编码为utf-8
$objphpexcel->setactivesheetindex(0)
->setcellvalue('a4', 'miscellaneous glyphs')
->setcellvalue('a5', 'éàèùâêîôûëïüÿäöüç');
$objphpexcel->getactivesheet()->setcellvalue('a8',"你好世界");
$objphpexcel->getactivesheet()->getrowdimension(8)->setrowheight(-1);
$objphpexcel->getactivesheet()->getstyle('a8')->getalignment()->setwraptext(true);
$value = "-valuean-value bn-value c";
$objphpexcel->getactivesheet()->setcellvalue('a10', $value);
$objphpexcel->getactivesheet()->getrowdimension(10)->setrowheight(-1);
$objphpexcel->getactivesheet()->getstyle('a10')->getalignment()->setwraptext(true);
$objphpexcel->getactivesheet()->getstyle('a10')->setquoteprefix(true);
// 重命名工作sheet
$objphpexcel->getactivesheet()->settitle('第一个sheet');
// 设置第一个sheet为工作的sheet
$objphpexcel->setactivesheetindex(0);
// 保存excel 2007格式文件,保存路径为当前路径,名字为export.xlsx
$objwriter = phpexcel_iofactory::createwriter($objphpexcel, 'excel2007');
$objwriter->save( 'export.xlsx');
// 保存excel 95格式文件,,保存路径为当前路径,
$objwriter = phpexcel_iofactory::createwriter($objphpexcel, 'excel5');
$objwriter->save('export.xls');
好了,执行后我们可以得到export.xls和export.xlsx。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持硕编程!
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!