程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> PHP:導入導出Excel的方法

PHP:導入導出Excel的方法

編輯:PHP綜合

如何把系統中的一些數據導出成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”;

?> 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved