PHP:导入导出Excel的方法

网络整理 - 07-29

如何把系统中的一些数据导出成Excel,修改后再导回系统,下面进行一些总结。

基本上导出的文件分为两种:

1:类Excel格式,这个其实不是传统意义上的Excel文件,只是因为Excel的兼容能力强,能够正确打开而已。修改这种文件后再保存,通常会提示你是否要转换成Excel文件。

优点:简单。

缺点:难以生成格式,如果用来导入需要自己分别编写相应的程序。

2:Excel格式,与类Excel相对应,这种方法生成的文件更接近于真正的Excel格式。

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

$yourStr = mb_convert_encoding;

下面详细列举几种方法。

一、PHP导出Excel

1:第一推荐无比风骚的PHPExcel,官方网站:

导入导出都成,可以导出office2007格式,同时兼容2003。

下载下来的包中有文档和例子,大家可以自行研究。

抄段例子出来:

PHP代码

<?php

/**

* PHPExcel

* Copyright20062007 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, orany 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 Copyright20062007 PHPExcel

* @license LGPL

* @version 1.5.0, 2007-10-23

*/

/** Error reporting */

error_reporting;

/** Include path **/

set_include_path . PATH_SEPARATOR . ‘../Classes/’);

/** PHPExcel */

include ‘PHPExcel.php’;

/** PHPExcel_Writer_Excel2007 */

include ‘PHPExcel/Writer/Excel2007.php’;

// Create new PHPExcel object

echo date . ” Create new PHPExcel object“n”;

$objPHPExcel = new PHPExcel;

// Set properties

echo date . ” Set properties“n”;

$objPHPExcel-getProperties-setCreator;

$objPHPExcel-getProperties-setLastModifiedBy;

$objPHPExcel-getProperties-setTitle;

$objPHPExcel-getProperties-setSubject;

$objPHPExcel-getProperties-setDescrīption;

$objPHPExcel-getProperties-setKeywords;

$objPHPExcel-getProperties-setCategory;

// Add some data

echo date . ” Add some data“n”;

$objPHPExcel-setActiveSheetIndex;

$objPHPExcel-getActiveSheet-setCellValue;

$objPHPExcel-getActiveSheet-setCellValue;

$objPHPExcel-getActiveSheet-setCellValue;

$objPHPExcel-getActiveSheet-setCellValue;

// Rename sheet

echo date . ” Rename sheet“n”;

$objPHPExcel-getActiveSheet-setTitle;

// Set active sheet index to the first sheet, so Excel opens this as the first sheet

$objPHPExcel-setActiveSheetIndex;

// Save Excel 2007 file

echo date . ” Write to Excel2007 format“n”;

$objWriter = new PHPExcel_Writer_Excel2007;

$objWriter-save);

// Echo done

echo date . ” Done writing file.“r“n”;

2、使用pear的Spreadsheet_Excel_Writer类

此类依赖于OLE,

需要注意的是导出的Excel文件格式比较老,修改后保存会提示是否转换成更新的格式。

不过可以设定格式,很强大。

PHP代码

?php

require_once ‘Spreadsheet/Excel/Writer.php’;

// Creating a workbook

$workbook = new Spreadsheet_Excel_Writer;

// sending HTTP headers

$workbook-send;

// Creating a worksheet

$worksheet =& $workbook-addWorksheet;

// The actual data

$worksheet-write;

$worksheet-write;

$worksheet-write;

$worksheet-write;

$worksheet-write;

$worksheet-write;

$worksheet-write;

$worksheet-write;

// Let’s send the file

$workbook-close;

3:利用smarty,生成符合Excel规范的XML或HTML文件

支持格式,非常完美的导出方案。不过导出来的的本质上还是XML文件,如果用来导入就需要另外处理了。

详细内容请见rardge大侠的文章:

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

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

缺点是无格式。

PHP代码

?php

// Send Header

header;

header;

header;

header;

header;

header;;

header;

header;

// XLS Data Cell

xlsBOF;

xlsWriteLabel;

xlsWriteLabel;

xlsWriteLabel;

xlsEOF;

function xlsBOF

function xlsEOF

function xlsWriteNumber

function xlsWriteLabel

不过在64位linux系统中使用时失败了,断句符号全部变成了乱码。

5、使用制表符、换行符的方法

制表符”“t”用户分割同一行中的列,换行符”“t“n”可以开启下一行。

?php

header;

header;

header;

header;

/*first line*/

echo “hello”.”“t”;

echo “world”.”“t”;

echo ““t“n”;

/*start of second line*/

echo “this is second line”.”“t”;

echo “Hi,pretty girl”.”“t”;

echo ““t“n”;

6、使用com

如果你的PHP可以开启com模块,就可以用它来导出Excel文件

PHP代码

<?

$filename = “c:/spreadhseet/test.xls”;

$sheet1 = 1;

$sheet2 = “sheet2″;

$excel_app = new COM or Die ;

print “Application name: “n” ;

print “Loaded version: “n”;

$Workbook = $excel_app-Workbooks-Open or Die;

$Worksheet = $Workbook-Worksheets;

$Worksheet-activate;

$excel_cell = $Worksheet-Range;

$excel_cell-activate;

$excel_result = $excel_cell-value;

print “$excel_result“n”;

$Worksheet = $Workbook-Worksheets;

$Worksheet-activate;

$excel_cell = $Worksheet-Range;

$excel_cell-activate;

$excel_result = $excel_cell-value;

print “$excel_result“n”;

#To close all instances of excel:

$Workbook-Close;

unset;

unset;

$excel_app-Workbooks-Close;

$excel_app-Quit;

unset;

?>

一个更好的例子:

一、PHP导入Excel

1:还是用PHPExcel,官方网站: 。

2:使用PHP-ExcelReader,下载地址:

举例:

PHP代码

<?php

require_once ‘Excel/reader.php’;

// ExcelFile;

$data = new Spreadsheet_Excel_Reader;

// Set output Encoding.

$data-setOutputEncoding;

$data-read;

error_reporting;

for

echo ““n”;

?>