php处理excel步骤介绍
遇到问题
平时在工作中,时常会出现将数据库表导出为excel或者将excel导入数据库表的需求。这一需求早早就已经实现过了,为了方便导入导出,兄弟连www.lampbrother.net将其分装成了两个方法作为记录。
代码实现
phpexcel类库的引用
phpexcel拥有强大的excel处理能力,在packagist上已经拥有数百万次的下载量,不过实话实说,excel的处理速度仍然是非常慢,数据量较大时慎重使用。在packagist上下载或者直接用composer require phpoffice/phpexcel之后,便可以使用phpexcel了。
导出成为excel
在绝大多数情况下,导出excel其实就是将二位数组转化为表格。
use namespace phpexcel;
/**
* @param $name string 要保存的excel的名字
* @param $ret_data 转换为表格的二维数组
* @throws phpexcel_exception
* @throws phpexcel_reader_exception
*/
function exportexcel($name, $ret_data){
$objphpexcel = new phpexcel();
//设置表格
$objphpexcel->getproperties()->setcreator($name)
->setlastmodifiedby($name)
->settitle("office 2007 xlsx test document")
->setsubject("office 2007 xlsx test document")
->setdescription("test document for office 2007 xlsx, generated using php classes.")
->setkeywords("office 2007 openxml php")
->setcategory("test result file");
//填充数据
foreach ($ret_data as $key => $row) {
$num = $key + 1;
//$row = array_values($row);
$i=0;
foreach ($row as $key2 => $value2) {
$objphpexcel->setactivesheetindex(0)->setcellvalue( cell::stringfromcolumnindex($i). ($num), $value2);
$i++;
}
}
//设置表格并输出
$objphpexcel->getactivesheet()->settitle($name);
header('content-type: application/vnd.ms-excel');
header("content-disposition: attachment;filename={$name}.xls");
header('cache-control: max-age=0');
header('cache-control: max-age=1');
header('last-modified: ' . gmdate('d, d m y h:i:s') . ' gmt');
header('cache-control: cache, must-revalidate');
header('pragma: public'); // http/1.0
$objwriter = phpexcel_iofactory::createwriter($objphpexcel, 'excel5');
$objwriter->save('php://output');
exit;
}
导入excel
同理,导入excel其实就是将excel的数据转化成为二维数组,这就要求excel必须符合格式。
function getrows($inputfilename)
{
if (!file_exists($inputfilename)) {
throw new exception("file not existed");
}
$inputfiletype = phpexcel_iofactory::identify($inputfilename);
$objreader = phpexcel_iofactory::createreader($inputfiletype);
$objphpexcel = $objreader->load($inputfilename);
$objworksheet = $objphpexcel->getactivesheet();
$highestrow = $objworksheet->gethighestrow();
$highestcolumn = $objworksheet->gethighestcolumn();
$highestcolumnindex = phpexcel_cell::columnindexfromstring($highestcolumn);//总列数
$row = 1;
$curr = array();
while ($row for ($col = 0; $col $value = str_replace(array("\n", "\n\r", "\r"), "", $objworksheet->getcellbycolumnandrow($col, $row)->getvalue());
$curr[$row][] = $value;
}
$row++;
}
array_shift($curr);//第一行一般是字段名(excel中列的标题),导入时要移除
return $curr;
}
其他
导出时保存的格式是xlsx,想要改成其他格式需要传入不同的参数。
导入时如果有多个sheet时需要在上次打开时在要导入的sheet页(以保证当前sheet为activesheet)关闭,或者根据sheet名在程序中选择sheet。
0
0
相关文章
php连接mysql用pdo事务吗_php pdo连mysql事务处理【步骤】
php二维转一维怎样捕获异常_php二维降维异常处理写法【步骤】
php动态网站开发如何处理表单提交_PHP动态网站表单处理逻辑【步骤】
如何使用 PHP 正则表达式精准提取 HTML 中广告区之后的商店区块
php分割文本含换行缩进_php保留缩进分割正则处理【步骤】
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
C++ 设计模式与软件架构
本专题深入讲解 C++ 中的常见设计模式与架构优化,包括单例模式、工厂模式、观察者模式、策略模式、命令模式等,结合实际案例展示如何在 C++ 项目中应用这些模式提升代码可维护性与扩展性。通过案例分析,帮助开发者掌握 如何运用设计模式构建高质量的软件架构,提升系统的灵活性与可扩展性。
14
2026.01.30
热门下载
精品课程
相关推荐
/
热门推荐
/
最新课程
最新文章









