您的位置:首页 > 编程语言 > PHP开发

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');
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: