您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

PHP导入导出Excel方法_PHP教程

2024/4/19 4:12:10发布4次查看
原作者:冰山上的播客
看到这篇文章的时候,很是惊讶原作者的耐心,虽然我们在平时用的也有一些,但没有作者列出来的全,写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代码
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代码
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代码
// send header  
header(”pragma: public”);  
header(”expires: 0
http://www.bkjia.com/phpjc/486584.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/486584.htmltecharticle原作者:冰山上的播客 看到这篇文章的时候,很是惊讶原作者的耐心,虽然我们在平时用的也有一些,但没有作者列出来的全,写excel的时...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录