向 Excel表中导出 jsp页面显示数据
发布时间:2023-05-24 13:43:08 所属栏目:教程 来源:
导读:excel报表的方法,一个过於简单,一个只能用於window平台(因为使用jdbc-odbc bridge),且无法使用到excel内部的各种公式或是方法,因此,今天介绍一个apache出的元件叫poi,它可以在unix或window平台处理word或excel
excel报表的方法,一个过於简单,一个只能用於window平台(因为使用jdbc-odbc bridge),且无法使用到excel内部的各种公式或是方法,因此,今天介绍一个apache出的元件叫poi,它可以在unix或window平台处理word或excel档案,而不需要依靠window的com,并且可设定储存格格式、列印格式等等;今天我来介绍其中有关资料读取、新增、修改及删除的功能,若各位网友研究好其他的功能,麻烦email给我([email protected]),分享给大家! 一、需要用的档案:jakarta-poi-1.8.0-dev-20020917.jar 几乎每天都有1.8.0的最新版(但非正式版),正式的版本是1.5.0 http://jakarta.apache.org/builds/jakarta-poi/nightly/ 将档案复制到classpath所指到的地方 二、有兴趣的朋友可以参考 http://jakarta.apache.org/poi/ 三、先建立一个叫做book1.xls的excel档,内容如下 ---------------------------------- 项目 单价 数量 合计 cpu 7000 5 35000 硬碟 2500 2 5000 记忆体 1600 3 4800 ---------------------------------- 其中合计的栏位是设定公式,单价*数量 四、资料读取範例 <%@ page contenttype="text/html;charset=ms950" import="java.util.*,java.io.*" %> <%@ page import="org.apache.poi.poifs.filesystem.*,org.apache.poi.hssf.usermodel.*" %> <html> <head> <meta http-equiv="content-type" content="text/html; charset=ms950"> <title>读取excel档案</title> </head> <body> <table border="1" width="100%"> <% fileinputstream finput = new fileinputstream(application.getrealpath("/")+"book1.xls" ); //设定fileinputstream读取excel档 poifsfilesystem fs = new poifsfilesystem( finput ); hssfworkbook wb = new hssfworkbook(fs); hssfsheet sheet = wb.getsheetat(0); //读取第一个工作表,宣告其为sheet finput.close(); hssfrow row=null; //宣告一列 hssfcell cell=null; //宣告一个储存格 short i=0; short y=0; //以巢状迴圈读取所有储存格资料 for (i=0;i<=sheet.getlastrownum();i++) { out.println("<tr>"); row=sheet.getrow(i); for (y=0;y<row.getlastcellnum();y++) { cell=row.getcell(y); out.print("<td>"); //判断储存格的格式 switch ( cell.getcelltype() ) { case hssfcell.cell_type_numeric: out.print(cell.getnumericcellvalue()); //getnumericcellvalue()会回传double值,若不希望出现小数点,请自行转型为int break; case hssfcell.cell_type_string: out.print( cell.getstringcellvalue()); break; case hssfcell.cell_type_formula: out.print(cell.getnumericcellvalue()); //读出公式储存格计算後的值 //若要读出公式内容,可用cell.getcellformula() break; default: out.print( "不明的格式"); break; } out.println("</td>"); } out.println("</tr>"); } %> </table> </body> </html> 五、资料新增範例 <%@ page contenttype="text/html;charset=ms950" import="java.util.*,java.io.*" %> <%@ page import="org.apache.poi.poifs.filesystem.*,org.apache.poi.hssf.usermodel.*" %> <html> <head> <meta http-equiv="content-type" content="text/html; charset=ms950"> <title>插入资料至excel档案</title> </head> <body> <% fileinputstream finput = new fileinputstream(application.getrealpath("/")+"book1.xls" ); //设定fileinputstream读取excel档 poifsfilesystem fs = new poifsfilesystem( finput ); hssfworkbook wb = new hssfworkbook(fs); hssfsheet sheet = wb.getsheetat(0); //读取第一个工作表,宣告其为sheet finput.close(); hssfrow row=null; //宣告一列 hssfcell cell=null; //宣告一个储存格 short i=4; row=sheet.createrow(i); //建立一个新的列,注意是第五列(列及储存格都是从0起算) cell=row.createcell((short)0); cell.setencoding(hssfcell.encoding_utf_16); //设定这个储存格的字串要储存双位元 cell.setcellvalue("显示卡"); cell=row.createcell((short)1); cell.setcellvalue(1700); cell=row.createcell((short)2); cell.setcellvalue(8); cell=row.createcell((short)3); //设定这个储存格为公式储存格,并输入公式 cell.setcellformula("b"+(i+1)+"*c"+(i+1)); try { fileoutputstream fout=new fileoutputstream(application.getrealpath("/")+"book1.xls"); wb.write(fout); //储存 fout.close(); out.println("储存成功<a href='book1.xls'>book1.xls</a>"); } catch(ioexception e) { out.println("产生错误,错误讯息:"+e.tostring()); } %> </body> </html> 六、资料删除、修改範例 <%@ page contenttype="text/html;charset=ms950" import="java.util.*,java.io.*" %> <%@ page import="org.apache.poi.poifs.filesystem.*,org.apache.poi.hssf.usermodel.*" %> <html> <head> <meta http-equiv="content-type" content="text/html; charset=ms950"> <title>删除、修改资料至excel档案</title> </head> <body> <% fileinputstream finput = new fileinputstream(application.getrealpath("/")+"book1.xls" ); //设定fileinputstream读取excel档 poifsfilesystem fs = new poifsfilesystem( finput ); hssfworkbook wb = new hssfworkbook(fs); hssfsheet sheet = wb.getsheetat(0); //读取第一个工作表,宣告其为sheet finput.close(); hssfrow row=null; //宣告一列 hssfcell cell=null; //宣告一个储存格 row=sheet.getrow((short)4); //取出第五列 if (row!=null) sheet.removerow(row); //先侦测第五列存不存在,若在的话将第五列删除 row=sheet.getrow((short)3); //取出第四列 cell=row.getcell((short)2); //取出第三个储存格 cell.setcellvalue(7); //设定该储存格值为7 cell=row.getcell((short)3); cell.setcellformula(cell.getcellformula()); //上两行为取出公式储存格,并重新计算(因为刚才更新过计算公式的值) //如果不做,公式计算後的值不会更新 try { fileoutputstream fout=new fileoutputstream(application.getrealpath("/")+"book1.xls"); wb.write(fout); //储存 fout.close(); out.println("储存成功<a href='book1.xls'>book1.xls</a>"); } catch(ioexception e) { out.println("产生错误,错误讯息:"+e.tostring()); } %> </body> </html> (编辑:聊城站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐