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

PHP导入导出Excel步骤

2025/1/21 4:25:51发布33次查看
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代码
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\n”; $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\n”; $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\n”; $objwriter = new phpexcel_writer_excel2007($objphpexcel); $objwriter->save(str_replace(’.php’, ‘.xlsx’, __file__)); // echo done echo date(’h:i:s’) . ” done writing file.\r\n”;
2、使用pear的spreadsheet_excel_writer类
下载地址: http://pear.php.net/package/spreadsheet_excel_writer
此类依赖于ole,下载地址:http://pear.php.net/package/ole
需要注意的是导出的excel文件格式比较老,修改后保存会提示是否转换成更新的格式。
不过可以设定格式,很强大。
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修改后保存,还不会弹出提示,推荐用这种方法。
缺点是无格式。

不过笔者在64位linux系统中使用时失败了,断句符号全部变成了乱码。
5、使用制表符、换行符的方法  
制表符”\t”用户分割同一行中的列,换行符”\t\n”可以开启下一行。  

6、使用com
如果你的php可以开启com模块,就可以用它来导出excel文件
application->value}\n” ; print “loaded version: {$excel_app->application->version}\n”; $workbook = $excel_app->workbooks->open(”$filename”) or die(”did not open $filename $workbook”); $worksheet = $workbook->worksheets($sheet1); $worksheet->activate; $excel_cell = $worksheet->range(”c4″); $excel_cell->activate; $excel_result = $excel_cell->value; print “$excel_result\n”; $worksheet = $workbook->worksheets($sheet2); $worksheet->activate; $excel_cell = $worksheet->range(”c4″); $excel_cell->activate; $excel_result = $excel_cell->value; print “$excel_result\n”; #to close all instances of excel: $workbook->close; unset($worksheet); unset($workbook); $excel_app->workbooks->close(); $excel_app->quit(); unset($excel_app); ?>
一个更好的例子: http://blog.chinaunix.net/u/16928/showart_387171.html
一、php导入excel
1:还是用phpexcel,官方网站: http://www.codeplex.com/phpexcel。
2:使用php-excelreader,下载地址: http://sourceforge.net/projects/phpexcelreader
举例:
setoutputencoding(’utf8′); $data->read(’ jxlrwtest.xls’); error_reporting(e_all ^ e_notice); for ($i = 1; $i sheets[0]['numrows']; $i++) { for ($j = 1; $j sheets[0]['numcols']; $j++) { echo “\”.$data->sheets[0]['cells'][$i][$j].”\”,”; } echo “\n”; } ?>
该用户其它信息

VIP推荐

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