博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php 数据导出到excel 2种带有合并单元格的导出
阅读量:5296 次
发布时间:2019-06-14

本文共 8100 字,大约阅读时间需要 27 分钟。

具体业务层面 可能会有所不同。以下两种方式涉及的合并单元格地方有所不同,不过基本思路是一致的。

第一种是非插件版本。可能更容易理解点,基本思路就是 组装table 然后 读取 输出到excel上。缺点是要设置样式不太好设置。

第二种是利用插件  PHPExcel   有点是可以对输出格式做各种设置。缺点是初次接触这个插件的同学,并且对表格合并不熟悉的同学,可能要花点时间理解

另外注意excel对数字过长会处理成你不想要的数据,记得对该数据格式化成字符串 貌似就可以解决。以前遇到过

 

/**     *     * execl数据导出     */    function exportOrderExcel($title, $cellName, $data) {        $htmlinfo = "
"; $htmlinfo .= "
"; foreach($cellName as $v){ $htmlinfo .= "
"; } $htmlinfo .= "
"; foreach ($data as $k => $v) { foreach ($v['sku'] as $k1 => $v1) { $htmlinfo .= "
"; if ($k1 == 0) { $htmlinfo .= "
"; } $htmlinfo .= "
"; if ($k1 == 0) { switch ($v['o_fromtype']) { case 1: $o_fromtype = "微信小程序"; break; case 2: $o_fromtype = "支付宝小程序"; break; case 3: $o_fromtype = "APP"; break; default : $o_fromtype = "其他1"; } $htmlinfo .= "
"; } $htmlinfo .= "
"; } } $htmlinfo .= "
".$v."
" . date('Y/m/d', strtotime($v['o_addtime'])) . " " . date('H:i:s', strtotime($v['o_addtime'])) . " " . $v['d_prov'] . $v['d_city'] . " " . $v['g_name'] . " " . $v['epid'] . " " . $v['did'] . " " . $v['deviceid'] . " " . $v1['cm_gid'] . " " . $v1['sku_title'] . " " . $v1['skuid'] . " " . $v1['sku_num'] . " " . sprintf("%.2f", ($v1['sku_activity_price'] * $v1['sku_num'])) . " " . $o_fromtype . "
"; unset($orders); $filetime = date("YmdHis"); $path = $_SERVER['DOCUMENT_ROOT'] . "/Uploads/"; $fp = @fopen($path . ".html", "wb"); @fwrite($fp, $htmlinfo); unset($htmlinfo); @fclose($fp); rename($path . ".html", $path . ".xls"); $file = fopen($path . ".xls", "r"); header("Content-Type: application/octet-stream"); header("Accept-Ranges: bytes"); header("Accept-Length: " . filesize($path . ".xls")); header("Content-Disposition: attachment; filename=销售明细_" . $filetime . ".xls"); echo fread($file, filesize($path . ".xls")); fclose($file); @unlink($path . ".xls"); exit; } /** * * execl数据导出 */ function exportOrderExcel2($title, $cellName, $data) { //引入核心文件 vendor("PHPExcel.PHPExcel"); $objPHPExcel = new \PHPExcel(); //定义配置 $topNumber = 2;//表头有几行占用 $xlsTitle = iconv('utf-8', 'gb2312', $title);//文件名称 $fileName = $title.date('_YmdHis');//文件名称 $cellKey = array( 'A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z', 'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM', 'AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ' ); $objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);//所有单元格(列)默认宽度 //垂直居中 $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //处理表头标题 $objPHPExcel->getActiveSheet()->mergeCells('A1:'.$cellKey[count($cellName)-1].'1');//合并单元格(如果要拆分单元格是需要先合并再拆分的,否则程序会报错) $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1',$title); $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18); $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //处理表头 foreach ($cellName as $k=>$v) { $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellKey[$k].$topNumber, $v);//设置表头数据// $objPHPExcel->getActiveSheet()->freezePane($cellKey[$k].($topNumber+1));//冻结窗口// $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k].$topNumber)->getFont()->setBold(true);//设置是否加粗// $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k].$topNumber)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中// if($v[3] > 0)//大于0表示需要设置宽度// {// $objPHPExcel->getActiveSheet()->getColumnDimension($cellKey[$k])->setWidth($v[3]);//设置列宽度// } } //处理数据 $start = $topNumber+1; $j = $topNumber+1; foreach ($data as $k=>$v) { // $j = $start; foreach ($v['sku'] as $k1=>$v1) { if($k1==0){ //计算初步当前单元格标识,以及 需要合并的单元格标识 A B C D E F G M $end = $start+count($v['sku'])-1; $objPHPExcel->getActiveSheet()->mergeCells("A".$start.':'."A".$end); $objPHPExcel->getActiveSheet()->mergeCells("B".$start.':'."B".$end); $objPHPExcel->getActiveSheet()->mergeCells("C".$start.':'."C".$end); $objPHPExcel->getActiveSheet()->mergeCells("D".$start.':'."D".$end); $objPHPExcel->getActiveSheet()->mergeCells("E".$start.':'."E".$end); $objPHPExcel->getActiveSheet()->mergeCells("F".$start.':'."F".$end); $objPHPExcel->getActiveSheet()->mergeCells("G".$start.':'."G".$end); $objPHPExcel->getActiveSheet()->mergeCells("M".$start.':'."M".$end); $objPHPExcel->getActiveSheet()->setCellValue("A".$start, $v['o_addtime']); $objPHPExcel->getActiveSheet()->setCellValue("B".$start, $v['d_prov'] . $v['d_city']); $objPHPExcel->getActiveSheet()->setCellValue("C".$start, $v['g_name']); $objPHPExcel->getActiveSheet()->setCellValue("D".$start, $v['epid']); $objPHPExcel->getActiveSheet()->setCellValue("E".$start, $v['did']); $objPHPExcel->getActiveSheet()->setCellValue("F".$start, $v['deviceid']); $objPHPExcel->getActiveSheet()->setCellValue("G".$start, $v['o_addtime']); $objPHPExcel->getActiveSheet()->setCellValue("M".$start, $v['o_fromtype']); } $objPHPExcel->getActiveSheet()->setCellValue("H".$j, $v1['cm_gid']); $objPHPExcel->getActiveSheet()->setCellValue("I".$j, $v1['sku_title']); $objPHPExcel->getActiveSheet()->setCellValue("J".$j, $v1['skuid']); $objPHPExcel->getActiveSheet()->setCellValue("K".$j, $v1['sku_num']); $objPHPExcel->getActiveSheet()->setCellValue("L".$j, sprintf("%.2f",($v1['sku_activity_price']*$v1['sku_num']))); $j++; } $start += count($v['sku']); } //导出execl ob_end_clean();//防止乱码 header('pragma:public'); header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"'); header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印 $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); exit; }

 

转载于:https://www.cnblogs.com/phpjinggege/p/9016162.html

你可能感兴趣的文章
#ifndef #define #endif
查看>>
css背景样式
查看>>
JavaScript介绍
查看>>
正则表达式
查看>>
开源网络漏洞扫描软件
查看>>
yum 命令跳过特定(指定)软件包升级方法
查看>>
创新课程管理系统数据库设计心得
查看>>
Hallo wolrd!
查看>>
16下学期进度条2
查看>>
Could not resolve view with name '***' in servlet with name 'dispatcher'
查看>>
Chapter 3 Phenomenon——12
查看>>
C语言中求最大最小值的库函数
查看>>
js学习(精华帖)
查看>>
和小哥哥一起刷洛谷(1)
查看>>
jquery对id中含有特殊字符的转义处理
查看>>
获取元素样式信息于三中获取方式的区别
查看>>
遇麻烦,Win7+Ubuntu12.10+Archlinux12.10 +grub
查看>>
SqlBulkCopy大批量导入数据
查看>>
HTTP协议 (四) 缓存
查看>>
python学习之random
查看>>