PHP读写excel文件 (封装 PHPExcel )
来源:互联网 发布:什么牌子棒球帽知乎 编辑:程序博客网 时间:2024/05/18 01:05
<?php/** * XExcel - excel 文件下载处理类 * 注意: * 1,全局编码为UTF-8,否则可能出错 * 2,支持读写excel2003,2007 * 3,csv需要的时候再实现 * @author wenyouming@imixun.com * @since 2014-01-14 * @version 1.0 * */class XExcel {/** * 文件下载 * @param array $data :数组数据 * @param string $filename :文件名称 * @param boolean $is_down :是否下载 */ private static function write($data, $filename = 'export', $is_down = true, $version=2007, $is_big = false) { /* 写excel需要较大的内存开销 */ if ($is_big) { $memory = '512M'; } else { $memory = '256M'; } ini_set('memory_limit', $memory); /* 数据维度检测 */ $depth = array_depth($data); if ($depth <= 0 || $depth >= 4) { echo "depth:$depth. 超过预定范围"; return false; } /* 维度补齐到3维数组 */ if ($depth == 1) { $data = array(array($data)); } elseif($depth == 2) { $data = array($data); } //如果是非thinkPHP请用require相关文件import('@.ORG.PHPExcel');$excel = new PHPExcel();$index = 0;foreach ($data as $sheet_data) { if ($index) { $excel->createSheet(); } $excel->setActiveSheetIndex($index++); $sheet = $excel->getActiveSheet(); $i = 1; foreach ($sheet_data as $row) { $j = 'A'; foreach ($row as $v) { /* 以字符串写入 */ $sheet->setCellValueExplicit($j.$i, $v, PHPExcel_Cell_DataType::TYPE_STRING); ++$j; } ++$i; }}if ($version == 2003) { $filename .= '.xls'; $writer = new PHPExcel_Writer_Excel5($excel); } else { $filename .= '.xlsx'; $writer= new PHPExcel_Writer_Excel2007($excel); } if (!$is_down) { try { $writer->save($filename); } catch (PHPExcel_Writer_Exception $e) { echo $e->getMessage(); return false; }return true;}/* 如果是IE,进行转码,防止乱码 */if (strpos($_SERVER["HTTP_USER_AGENT"],"MSIE")) { $filename = rawurlencode($filename);}header("Pragma: public");header("Expires: 0");header("Cache-Control:must-revalidate, post-check=0, pre-check=0");header("Content-Type:application/force-download");header("Content-Type:application/vnd.ms-execl");header("Content-Type:application/octet-stream");header("Content-Type:application/download");header("Content-Disposition:attachment;filename=\"$filename\"");header("Content-Transfer-Encoding:binary");$writer->save('php://output');}/** * write_2007 - 写2007格式 * @param array $data: 数据 * @param string $filename : 文件名 * @param boolean $is_big : 是否写大文件,默认false */ static function write_2007($data, $filename='export_2007', $is_big = false) { self::write($data, $filename, false, 2007, $is_big); }/** * down_2007 - 下载2007文件格式 * @param array $data: 数据 * @param string $filename : 文件名 * @param boolean $is_big : 是否写大文件,默认false */ static function down_2007($data, $filename='export_2007', $is_big = false) { self::write($data, $filename, true, 2007, $is_big); } /** * write_2003 - 写2003格式 * @param array $data: 数据 * @param string $filename : 文件名 * @param boolean $is_big : 是否写大文件,默认false */ static function write_2003($data, $filename='export_2003', $is_big = false) { self::write($data, $filename, false, 2003, $is_big); }/** * down_2003 - 下载2003文件格式 * @param array $data: 数据 * @param string $filename : 文件名 * @param boolean $is_big : 是否写大文件,默认false */ static function down_2003($data, $filename='down_2003', $is_big = false) { self::write($data, $filename, true, 2003, $is_big); } /** * read - 读取excel文件(自动识别excel2007和excel2003) * 推荐文件只用字符串格式,或者数字,其他格式需要检查是否有误 * @param string $filename 文件名 * @param boolean $is_big 是否为大文件 * @return mixed -1,文件不存在; -2,不能识别的文件; array, 3维数组 */ static function read($filename, $is_big = false) { if (!file_exists($filename)) { return -1; } if ($is_big) { $memory = '512M'; } else { $memory = '256M'; } ini_set('memory_limit', $memory); import('@.ORG.PHPExcel'); $reader = new PHPExcel_Reader_Excel2007(); if (!$reader->canRead($filename)) { $reader = new PHPExcel_Reader_Excel5(); if (!$reader->canRead($filename)) { return -2; } } $excel = $reader->load($filename); $sheet_list = $excel->getAllSheets(); $data = array(); foreach ($sheet_list as $sheet) { $cols = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn()); $rows = $sheet->getHighestRow(); $d = array(); for($row = 1; $row <= $rows; ++$row) { for($col = 0; $col < $cols; ++$col) { $cell = $sheet->getCellByColumnAndRow($col, $row); $cellstyleformat = $cell->getStyle($cell->getCoordinate())->getNumberFormat(); //日期格式的判断 $value = $cell->getValue();//获取值 $formatcode=$cellstyleformat->getFormatCode(); if (preg_match('/^(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy]/i', $formatcode)){ $value = gmdate("Y-m-d H:i:s", PHPExcel_Shared_Date::ExcelToPHP($value)); }else{ $value = PHPExcel_Style_NumberFormat::toFormattedString($value, $formatcode); } $d[$row - 1][$col] = $value; } } $data[] = $d; } return $data; }}
0 0
- PHP读写excel文件 (封装 PHPExcel )
- php导出Excel文件简单的方法(非PHPEXCEL)
- php操作excel文件--使用phpexcel
- php操作excel文件--使用phpexcel
- PHP Yii PHPExcel导出 excel文件
- ThinkPHP使用PHPexcel导出excel文件(已封装的输出类)
- PHP与EXCEL PHPExcel
- phpexcel读写excel表格详解
- phpexcel导出excel文件
- PHPexcel 生成 excel 文件
- PHPexcel导出excel文件
- PHPExcel读取excel文件
- PHPEXCEL生成excel文件
- PHPExcel(读取Excel文件)
- phpexcel 修改excel文件
- PHPExcel Excel文件导入
- VB读写EXCEL文件(封装类)
- php excel文件导出之phpExcel扩展库
- mysql悲观锁总结和实践
- Storm - 大数据Big Data实时处理架构
- Extjs的下拉框combo传多个值到后台
- ffmpeg jpeg 转yuv422p(420p)
- Java华丽转身 —— GUI编程宝典
- PHP读写excel文件 (封装 PHPExcel )
- spring对AOP的支持
- Struts2入门初步需掌握(一)
- iOS 类别和扩展(Categories和Extensions)
- HDOJ - 1009 - FatMouse' Trade(贪心)
- 可执行文件运行的系统调用
- 【Leetcode】Balanced Binary Tree
- jquery clone 与 clone(true) 的区别
- 理解 for 循环、while 循环和 do 循环之间的区别(AS3)