codeigniter结合phpexcel生成excel的应用
2011-09-23 15:59
330 查看
function excel() { /** * PHPExcel * * Copyright (C) 2006 - 2011 PHPExcel * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * @category PHPExcel * @package PHPExcel * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel) * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL * @version 1.7.6, 2011-02-27 */ /** Error reporting */ error_reporting(E_ALL); date_default_timezone_set('Europe/London'); /** PHPExcel */ //require_once '../Classes/PHPExcel.php'; $this->load->library('PHPExcel'); // Create new PHPExcel object $objPHPExcel = new PHPExcel(); // Set properties $objPHPExcel->getProperties()->setCreator("Maarten Balliauw") ->setLastModifiedBy("Maarten Balliauw") ->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"); // Add some data $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'Hello') ->setCellValue('B2', 'world!') ->setCellValue('C1', 'Hello') ->setCellValue('D2', 'world!'); // Miscellaneous glyphs, UTF-8 $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', iconv('utf-8','gbk','中文Hello'))// 新加代码 ->setCellValue('A4', iconv('utf-8','gbk','Miscellaneous glyphs')) ->setCellValue('A5', iconv('utf-8','gbk','nnnddd')); // Rename sheet $objPHPExcel->getActiveSheet()->setTitle('Simple'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); header("Content-Type:text/html;charset=utf-8"); //---------------------------新加的 // Redirect output to a client’s web browser (Excel5) header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="01simple.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); /* header("Content-type: text/csv");//重要 header('Content-Disposition: attachment;filename="01simple.xls"'); header('Cache-Control: must-revalidate, post-check=0,pre-check=0'); header('Expires:0'); header('Pragma:public'); $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5'); $objWriter->save('php://output'); */ exit; } function excel2(){ error_reporting(E_ALL); date_default_timezone_set('Europe/London'); /** PHPExcel */ //require_once '../Classes/PHPExcel.php'; $this->load->library('PHPExcel'); // Create new PHPExcel object //echo date('H:i:s') . " Create new PHPExcel object\n"; $objPHPExcel = new PHPExcel(); // Set properties //echo date('H:i:s') . " Set properties\n"; $objPHPExcel->getProperties()->setCreator("Maarten Balliauw") ->setLastModifiedBy("Maarten Balliauw") ->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"); // Add some data //echo date('H:i:s') . " Add some data\n"; $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', '发文编号') ->setCellValue('B1', '发文标题') ->setCellValue('C1', '发布渠道') ->setCellValue('D1', '发布人员') ->setCellValue('e1', '发布时间'); // Miscellaneous glyphs, UTF-8 $this->db->order_by("bianhao", "desc"); $query = $this->db->get('fawen'); $i=2; foreach ($query->result() as $row) : // for($i=2;$i<=10;$i++){ $objPHPExcel->getActiveSheet()->setCellValue('a'.$i, $row->bianhao); $objPHPExcel->getActiveSheet()->setCellValue('b'.$i, $row->title); $objPHPExcel->getActiveSheet()->setCellValue('c'.$i, $row->qudao); $objPHPExcel->getActiveSheet()->setCellValue('d'.$i, $row->faburen); $objPHPExcel->getActiveSheet()->setCellValue('e'.$i, $row->time); // } $i=$i+1; endforeach ; // Rename sheet //echo date('H:i:s') . " Rename sheet\n"; $objPHPExcel->getActiveSheet()->setTitle('通讯录'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); // Save Excel 2007 file //echo date('H:i:s') . " Write to Excel2007 format\n"; $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $fawei11=iconv('utf-8','gbk','发文编号.xls'); $objWriter->save($fawei11); //$objWriter->save(str_replace('.php', '.xlsx', __FILE__)); //$objWriter->save(str_replace('.php', '.xlsx', '发文')); // Echo memory peak usage //echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB\r\n"; // Echo done //echo date('H:i:s') . " Done writing file.\r\n"; $this->load->view('gongwen/excel'); }
相关文章推荐
- phpexcel应用 、生成excel文档
- python结合Arcpy处理EXCEL数据生成多边形
- 关于C#操作EXCEL,生成图表的全面应用之一(利用Microsoft.Office.Interop.Excel)【转载】
- 使用PHPExcel生成和读取Excel文件
- Struts结合数据实时生成Excel文件并直接下载
- PHPEXCEL生成excel文件
- phpexcel导出数据库数据生成excel文件
- ASP与Excel结合生成数据表和Chart图的代码
- GAN︱生成模型学习笔记(运行机制、NLP结合难点、应用案例、相关Paper)
- Excel 应用06_自动生成timesheet_VBA
- spring里结合POI生成EXCEL
- 流形式下载文件(结合POI生成excel使用)
- spring里结合POI生成EXCEL
- 关于C#操作EXCEL,生成图表的全面应用之二(利用Microsoft.Office.Interop.OWC11)
- 解决PHPexcel读取excel时间字段等生成科学计数法的问题
- [转] C#操作EXCEL,生成图表的全面应用
- PHPExcel:用于Excel等文档生成的PHP开源类库
- Excel超链接的应用-在Excel里输入身份证号,自动生成查找身份证信息的链接(可批量)
- 使用PHPExcel生成和读取Excel文件
- ASP.NET中应用Excel:(6)在服务器端生成HTML表格