PHP头条
热点:

PHP导入导出Excel方法


原作者:冰山上的播客
看到这篇文章的时候,很是惊讶原作者的耐心,虽然我们在平时用的也有一些,但没有作者列出来的全,写excel的时候,我用过pear的库,也用过pack压包的头,同样那些利用smarty等作的简单替换xml的也用过,csv的就更不用谈了。呵呵。(COM方式不讲了,这种可读的太多了,我也写过利用wps等进行word等的生成之类的文章 )
但是在读的时候,只用过一种,具体是什么忘了,要回去翻代码了。因为采用的是拿来主义,记不住。
原文地址:http://xinsync.xju.edu.cn/index.php/archives/3858
原文内容:

最近因项目需要,需要开发一个模块,把系统中的一些数据导出成Excel,修改后再导回系统。就趁机对这个研究了一番,下面进行一些总结。
基本上导出的文件分为两种:
1:类Excel格式,这个其实不是传统意义上的Excel文件,只是因为Excel的兼容能力强,能够正确打开而已。修改这种文件后再保存,通常会提示你是否要转换成Excel文件。
优点:简单。
缺点:难以生成格式,如果用来导入需要自己分别编写相应的程序。
2:Excel格式,与类Excel相对应,这种方法生成的文件更接近于真正的Excel格式。

如果导出中文时出现乱码,可以尝试将字符串转换成gb2312,例如下面就把$yourStr从utf-8转换成了gb2312:
$yourStr = mb_convert_encoding(”gb2312″, “UTF-8″, $yourStr);

下面详细列举几种方法。
一、PHP导出Excel

1:第一推荐无比风骚的PHPExcel,官方网站: http://www.codeplex.com/PHPExcel
导入导出都成,可以导出office2007格式,同时兼容2003。
下载下来的包中有文档和例子,大家可以自行研究。
抄段例子出来:


PHP代码
<?php  
/** 
* PHPExcel 

* Copyright (C) 2006 - 2007 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 - 2007 PHPExcel ( http://www.codeplex.com/PHPExcel) 
* @license    http://www.gnu.org/licenses/lgpl.txt    LGPL 
* @version    1.5.0, 2007-10-23 
*/  
  
/** Error reporting */  
error_reporting(E_ALL);  
  
/** Include path **/  
set_include_path(get_include_path() . PATH_SEPARATOR . ‘../Classes/’);  
  
/** PHPExcel */  
include ‘PHPExcel.php’;  
  
/** PHPExcel_Writer_Excel2007 */  
include ‘PHPExcel/Writer/Excel2007.php’;  
  
// Create new PHPExcel object  
echo date(’H:i:s’) . ” Create new PHPExcel object ”;  
$objPHPExcel = new PHPExcel();  
  
// Set properties  
echo date(’H:i:s’) . ” Set properties ”;  
$objPHPExcel->getProperties()->setCreator(”Maarten Balliauw”);  
$objPHPExcel->getProperties()->setLastModifiedBy(”Maarten Balliauw”);  
$objPHPExcel->getProperties()->setTitle(”Office 2007 XLSX Test Document”);  
$objPHPExcel->getProperties()->setSubject(”Office 2007 XLSX Test Document”);  
$objPHPExcel->getProperties()->setDescrīption(”Test document for Office 2007 XLSX, generated using PHP classes.”);  
$objPHPExcel->getProperties()->setKeywords(”office 2007 openxml php”);  
$objPHPExcel->getProperties()->setCategory(”Test result file”);  
  
// Add some data  
echo date(’H:i:s’) . ” Add some data ”;  
$objPHPExcel->setActiveSheetIndex(0);  
$objPHPExcel->getActiveSheet()->setCellValue(’A1′, ‘Hello’);  
$objPHPExcel->getActiveSheet()->setCellValue(’B2′, ‘world!’);  
$objPHPExcel->getActiveSheet()->setCellValue(’C1′, ‘Hello’);  
$objPHPExcel->getActiveSheet()->setCellValue(’D2′, ‘world!’);  
  
// Rename sheet  
echo date(’H:i:s’) . ” 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);  
  
// Save Excel 2007 file  
echo date(’H:i:s’) . ” Write to Excel2007 format ”;  
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);  
$objWriter->save(str_replace(’.php’, ‘.xlsx’, __FILE__));  
  
// Echo done  
echo date(’H:i:s’) . ” Done writing file. ”;  

 

2、使用pear的Spreadsheet_Excel_Writer类
下载地址: http://pear.php.net/package/Spreadsheet_Excel_Writer
此类依赖于OLE,下载地址:http://pear.php.net/package/OLE
需要注意的是导出的Excel文件格式比较老,修改后保存会提示是否转换成更新的格式。
不过可以设定格式,很强大。


PHP代码
<?php  
require_once ‘Spreadsheet/Excel/Writer.php’;  
  
// Creating a workbook  
$workbook = new Spreadsheet_Excel_Writer();  
  
// sending HTTP headers  
$workbook->send(’test.xls’);  
  
// Creating a worksheet  
$worksheet =& $workbook->addWorksheet(’My first worksheet’);  
  
// The actual data  
$worksheet->write(0, 0, ‘Name’);  
$worksheet->write(0, 1, ‘Age’);  
$worksheet->write(1, 0, ‘John Smith’);  
$worksheet->write(1, 1, 30);  
$worksheet->write(2, 0, ‘Johann Schmidt’);  
$worksheet->write(2, 1, 31);  
$worksheet->write(3, 0, ‘Juan Herrera’);  
$worksheet->write(3, 1, 32);  
  
// Let’s send the file  
$workbook->close();  
?>  


3:利用smarty,生成符合Excel规范的XML或HTML文件
支持格式,非常完美的导出方案。不过导出来的的本质上还是XML文件,如果用来导入就需要另外处理了。
详细内容请见rardge大侠的帖子:http://bbs.chinaunix.net/viewthread.php?tid=745757

需要注意的是如果导出的表格行数不确定时,最好在模板中把”ss:ExpandedColumnCount=”5″ ss:ExpandedRowCount=”21″”之类的东西删掉。

4、利用pack函数打印出模拟Excel格式的断句符号,这种更接近于Excel标准格式,用office2003修改后保存,还不会弹出提示,推荐用这种方法。
缺点是无格式。


PHP代码
<?php  
// Send Header  
header(”Pragma: public”);  
header(”Expires: 0

www.phpzy.comtrue/phprm/28392.htmlTechArticlePHP导入导出Excel方法 原作者:冰山上的播客 看到这篇文章的时候,很是惊讶原作者的耐心,虽然我们在平时用的也有一些,但没有作者列出来的全,写excel的时候,我用过pear的库,也用...

相关文章

    暂无相关文章

PHP之友评论

今天推荐