博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHPEXCEL 小记
阅读量:5313 次
发布时间:2019-06-14

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

  • 首先是使用PHP Reader 读取Excle内容:
1 require("http://www.cnblogs.com/PHPExcel/Classes/PHPExcel.php"); 2 $file = "D:\\datas.xlsx"; 3 if(!file_exists($file)){ 4     die("no file found in {
$file}"); 5 } 6 $datasReader = PHPExcel_IOFactory::load($file); 7 $sheets = $datasReader->getAllSheets(); 8 //如果有多个工作簿 9 $countSheets = count($sheets);10 $sheetsinfo = array();11 $sheetData = array();12 if($countSheets==1){13 $sheet = $sheets[0]; 14 $sheetsinfo["rows"] = $sheet->getHighestRow();15 $sheetsinfo["column"] = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn());16 for($row=1;$row<=$sheetsinfo["rows"];$row++){17 for($column=0;$column<$sheetsinfo["column"];$column++){18 $sheetData[$column][$row] = $sheet->getCellByColumnAndRow($column, $row)->getValue();19 }20 }21 }else{ 22 foreach ($sheets as $key => $sheet) 23 {24 $sheetsinfo[$key]["rows"] = $sheet->getHighestRow();25 $sheetsinfo[$key]["column"] = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn()); 26 for($row=1;$row<=$sheetsinfo[$key]["rows"];$row++){27 for($column=0;$column<$sheetsinfo[$key]["column"];$column++){28 $sheetData[$key][$column][$row] = $sheet->getCellByColumnAndRow($column, $row)->getValue();29 }30 }31 }32 }33 echo "
";34 print_r($sheetData);35 echo "
";

注:使用PHP 读取excel文件内容,一般都是处理整理好格式的csv或者excel,也可以读取xml文件

  • PHPExcel生成Exceel
$sql = sprintf("select * from table where op_id=%d",  intval($this->params['id']));        $query = $this->_db->query($sql);        require_once './PHPExcel_1.7.4/Classes/PHPExcel.php';        $objPHPExcel = new PHPExcel();        $objPHPExcel->setActiveSheetIndex(0);        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);        $objPHPExcel->getActiveSheet()->setCellValue('A1', "{
$this->_packInfos['o_id']}"); $objPHPExcel->getActiveSheet()->setCellValue('B1', "Volume weight (kg)"); $objPHPExcel->getActiveSheet()->setCellValue('D1', "Actual weight (kg)"); $objPHPExcel->getActiveSheet()->setCellValue('A2', "Box No."); $objPHPExcel->getActiveSheet()->setCellValue('B2', "Products"); $objPHPExcel->getActiveSheet()->setCellValue('C2', "Shipping Box"); $objPHPExcel->getActiveSheet()->setCellValue('D2', "System"); $objPHPExcel->getActiveSheet()->setCellValue('E2', "Input"); $objActSheet = $objPHPExcel->getActiveSheet(); $objActSheet->mergeCells("B1:C1"); $objActSheet->mergeCells("D1:E1"); $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A2'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $objPHPExcel->getActiveSheet()->getStyle('B2'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('C2'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('D2'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('E2'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); if($this->_db->num_rows($query)>0) { $i=3; while ($row = $this->_db->fetch_assoc($query)) { $objPHPExcel->getActiveSheet()->setCellValue('A'.($i),"BOX ".$row['box_num']); $objPHPExcel->getActiveSheet()->setCellValue('B'.($i),sprintf("%.2f",$row['volume_weight'])); $objPHPExcel->getActiveSheet()->setCellValue('C'.($i),sprintf("%.2f",$row['box_weight'])); $objPHPExcel->getActiveSheet()->setCellValue('D'.($i),sprintf("%.2f",$row['system_weight'])); $objPHPExcel->getActiveSheet()->setCellValue('E'.($i),sprintf("%.2f",$row['real_weight'])); $objPHPExcel->getActiveSheet()->getStyle('A'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $objPHPExcel->getActiveSheet()->getStyle('B'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); $objPHPExcel->getActiveSheet()->getStyle('C'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); $objPHPExcel->getActiveSheet()->getStyle('D'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); $objPHPExcel->getActiveSheet()->getStyle('E'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); $i++; } } $fileName="exportBox.xls"; $filePath = dirname(dirname("__FILE__"))."/template/".$fileName; $path = "./template/".$fileName; $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); if(file_exists($path)){ chmod($path, 0777); unlink($path); $objWriter->save($path); header('application/vnd.ms-excel'); header('Content-Disposition: attachment;filename=weight-'.$this->_packInfos["o_id"].".xlsx"); readfile($filePath); die(); } else { $objWriter->save($path); header('application/vnd.ms-excel'); header('Content-Disposition: attachment;filename=weight-'.$this->_packInfos["o_id"].".xlsx"); readfile($filePath); die(); }

注:上面的php生成excel的方式是直接使用A标签形式的,如果使用ajax,可以不使用header,直接echo $path,前台window.location.href=返回来的path就可以了。

转载于:https://www.cnblogs.com/akulubala/archive/2013/01/05/2846170.html

你可能感兴趣的文章
在线集成开发环境
查看>>
.net 冒泡排序示例
查看>>
Uva(10330)
查看>>
vlan学习
查看>>
R-Sys.time计算程序运行时间
查看>>
基础数据类型汇总补充;集合set ;深浅copy
查看>>
Java类模板
查看>>
【转贴】SAP HANA内存数据库详解
查看>>
二分查找BinarySearch(Java)
查看>>
两种应该掌握的排序方法--------1.shell Sort
查看>>
最大子段和
查看>>
vuejs动态组件给子组件传递数据
查看>>
javascript constrator and prototype
查看>>
杭电2065(递推)红色病毒
查看>>
No Language-Support in system setting ,Ubuntu
查看>>
spring 实现测试解耦
查看>>
系统资源不足,无法完成API(收集)
查看>>
美化滚动条样式
查看>>
WM_CLOSE,WM_DESTROY,WM_QUIT区别
查看>>
Python学习笔记第二十一周
查看>>