PHP导出数据到表格的实例
我发现最近的项目需要导出excel表的页面非常的多,想来这个也是我们常用的功能了,现在我可以很熟练的导出了,但是记得当时自己第一次导出时还是绕了一些弯路的,那么现在我就来记录下我这次用exshop框架项目下的导出(其实在不同的框架下excel的导出原理都是差不多的)
前端
<a href="javascript:;" id="export_all" class="coolbg">导出</a> <script> //导出数据 $('#export_all').click(function(){ window.open('index.php?app=craft_order&act=export', '_blank'); }); </script>
控制器
```
// 导出数据
public function export() {
$result = $this->_oaordermodel->getallorderlistformanager($this->store_id, $ordersn=null, $buyer_id=null, $buyer_name=null, $consignee=null, $phone=null, $company_name=null, $status=null, $s_time=null, $e_time=null, $page=null, $listrows=null, $execl=true); //这个是获得数据的代码-model里
$orderlist = $result['orderlist'];
if (!empty($orderlist)) {
$j = 1;
$stmt = array();
foreach ($orderlist as $val) {
$stmt[$j]['网站id'] = $val['store_id'];
$stmt[$j]['订单信息'] = $val['order_sn'];
$stmt[$j]['商品信息'] = $val['inventory_sn_count_chinese'];
$stmt[$j]['工艺选择'] = $val['craft_count_chinese'];
$stmt[$j]['商品总数量'] = $val['real_goods_total_count'];
$stmt[$j]['提交日期'] = date("y-m-d h:i:s",$val['add_time']);
$stmt[$j]['客户名称'] = $val['company_name'];
$stmt[$j]['联系人'] = $val['consignee'];
$stmt[$j]['联系方式'] = $val['phone_mob'];
$stmt[$j]['订单完成率'] = $val['percentage_complete'];
$stmt[$j]['订单状态'] = $val['statuschinese'];
$j++;
}
$current_path = dirname(file);
$home_path = dirname($current_path);
require_once root_path . '/includes/libraries/phpexcel.php';
require_once root_path . '/includes/libraries/phpexcel/iofactory.php';
$objphpexcel = new phpexcel(); //这个方法自己下载放到公共方法里
$objphpexcel->getproperties()->setcreator("maarten balliauw")
->setlastmodifiedby("maarten balliauw")
->settitle("office 2007 xlsx test document")
->setsubject("office 2007 xlsx test document")
->setdescription("document for office 2007 xlsx, generated using php classes.")
->setkeywords("office 2007 openxml php")
->setcategory("test result file");
// 行高 for ($i = 2; $i <= count($stmt); $i++) { $objphpexcel->getactivesheet()->getrowdimension($i)->setrowheight(22); } foreach ($stmt as $fid => $fval) { if ($fid == 1) { $key = 0; foreach ($fval as $title => $first) { //如果一级标题 $objphpexcel->getactivesheet()->setcellvalue(chr($key + 65) . '1', $title); $objphpexcel->getactivesheet()->getstyle(chr($key + 65) . '1')->getfont()->setbold(true); // 加粗 $key ++; } } $cid = 0; $row_id = $fid + 1; foreach ($fval as $cval) { $objphpexcel->getactivesheet()->setcellvalue(chr($cid + 65) . (string) ($row_id), $cval); $cid++; } } $objphpexcel->setactivesheetindex(0); $objphpexcel->getactivesheet()->settitle('excel表'); $objwriter = phpexcel_iofactory::createwriter($objphpexcel, 'excel5'); //$objwriter->save('订单列表详细.xls'); //输出到浏览器 header("content-type: application/force-download"); header("content-type: application/octet-stream"); header("content-type: application/download"); header('content-disposition:inline;filename="订单列表.xls"'); header("content-transfer-encoding: binary"); header("expires: mon, 26 jul 1997 05:00:00 gmt"); header("last-modified: " . gmdate("d, d m y h:i:s") . " gmt"); header("cache-control: must-revalidate, post-check=0, pre-check=0"); header("pragma: no-cache"); $objwriter->save('php://output'); } } ```
注意:phpexcel(); //这个方法自己下载放到公共方法里
成果图
心得
有时候遇到这些问题可以多思考,多看看它的原理,原理理解了下次做其它的也是会的,但最重要的是要懂得做记录,我们的记忆并没有想象的那么好
注:文章来源雨中笑记录实习期遇到的问题与心得,转载请申明原文
上一篇: EventBus 使用/架构/源码分析
下一篇: 基本IO模型与非阻塞IO模型