POI3.8和jxl读取Excel例子

来源:互联网 发布:淘宝童装平铺拍照技巧 编辑:程序博客网 时间:2024/06/06 04:54

这几天在弄java读excel的问题,以前用的是jxl.jar来读的,后来发现无法读取2007的,于是研究了一下POI,在网上下了最新版的poi-bin-3.8-beta5-20111217.zip,同时也参考了网上其它人写的代码,在jboss7服务器下成功读出,发demo吧。

使用方法:jboss服务器或tomcat服务器,将下面的jsp文件保存到某个域名下,同时在此文件所在的目录中新建一个upload目录,然后将要读取的test.xls和test.xlsx放到upload目录下,然后运行jsp文件即可看到结果:

(1)用poi.jar来读取excel2003

必须要用到的jar文件:

poi-3.8-beta5-20111217.jar

poi-ooxml-3.8-beta5-20111217.jar

poi-ooxml-schemas-3.8-beta5-20111217.jar

xmlbeans-2.3.0.jar

dom4j-1.6.1.jar

jsr173_1.0_api.jar  (如果jdk是1.5版本则需要此jar,如果是1.6以上版本则不需要)

为了方便测试,代码为jsp,如下:

[html] view plaincopy
  1. <%@ page contentType="text/html; charset=gb2312" language="java" import="java.io.*" errorPage="" %>  
  2. <%@ page import="java.io.*,org.apache.poi.poifs.filesystem.POIFSFileSystem,org.apache.poi.hssf.record.*,org.apache.poi.hssf.model.*,org.apache.poi.hssf.usermodel.*,org.apache.poi.hssf.util.*" %>  
  3. <%request.setCharacterEncoding("gb2312");%>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  8. <title>用poi.jar来读取excel2003</title>  
  9. </head>  
  10. <body bgcolor="#F2F8FF">  
  11.   
  12. <%  
  13. try{  
  14.   String file = application.getRealPath("/") + "upload\\test.xls";  
  15.   POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));  
  16.   HSSFWorkbook xwb = new HSSFWorkbook(fs);  
  17.   //读取第一个sheet  
  18.   HSSFSheet sheet = xwb.getSheetAt(0);  
  19.   
  20.   //定义 row、cell  
  21.   HSSFRow row;  
  22.   HSSFCell cell;  
  23.   String output;  
  24.   int rowNum;  
  25.   int cellNum;  
  26.   
  27.   //循环输出表格中的内容  
  28.   output = "";  
  29.   rowNum = sheet.getPhysicalNumberOfRows();  
  30.   
  31.   for (int i = 0; i < rowNum; i++)  
  32.   {  
  33.     row = sheet.getRow(i);  
  34.     cellNum = row.getPhysicalNumberOfCells();  
  35.     for (int j = 0; j < cellNum; j++)  
  36.     {  
  37.       cell = row.getCell(j);  
  38.       output = output + cell.getStringCellValue() + "   ";  
  39.     }  
  40.     output = output + "<br />";  
  41.   }  
  42.   
  43.   out.print(output);  
  44.   
  45. }catch(Exception e){  
  46.   out.print(e.toString());  
  47. }  
  48.   
  49. %>  
  50.   
  51. </body>  
  52. </html>  


(2)用poi读取excel2007的代码

所需jar文件同上

[html] view plaincopy
  1. <%@ page contentType="text/html; charset=gb2312" language="java" import="java.io.*" errorPage="" %>  
  2. <%@ page import="org.apache.poi.xssf.usermodel.XSSFRow,org.apache.poi.xssf.usermodel.XSSFSheet,org.apache.poi.xssf.usermodel.XSSFWorkbook,org.apache.poi.xssf.usermodel.XSSFCell" %>  
  3. <%request.setCharacterEncoding("gb2312");%>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  8. <title>用poi.jar来读取excel2007</title>  
  9. </head>  
  10. <body bgcolor="#F2F8FF">  
  11.   
  12. <%  
  13. try{  
  14.   String file = application.getRealPath("/") + "upload\\test.xlsx";  
  15.   XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));  
  16.   //读取第一个sheet  
  17.   XSSFSheet sheet = xwb.getSheetAt(0);  
  18.   
  19.   //定义 row、cell  
  20.   XSSFRow row;  
  21.   XSSFCell cell;  
  22.   String output;  
  23.   int rowNum;  
  24.   int cellNum;  
  25.   
  26.   //循环输出表格中的内容  
  27.   output = "";  
  28.   rowNum = sheet.getLastRowNum();  
  29.   
  30.   for (int i = 0; i <= rowNum; i++)  
  31.   {  
  32.     row = sheet.getRow(i);  
  33.     cellNum = row.getPhysicalNumberOfCells();  
  34.     for (int j = 0; j < cellNum; j++)  
  35.     {  
  36.       cell = row.getCell(j);  
  37.       output = output + cell.getStringCellValue() + "   ";  
  38.     }  
  39.     output = output + "<br />";  
  40.   }  
  41.   
  42.   out.print(output);  
  43.   
  44. }catch(Exception e){  
  45.   out.print(e.toString());  
  46. }  
  47.   
  48. %>  
  49.   
  50. </body>  
  51. </html>  


(3)用jxl.jar读取excel2003的代码

需要jxl.jar即可

[html] view plaincopy
  1. <%@ page contentType="text/html; charset=gb2312" language="java" import="java.io.*" errorPage="" %>  
  2. <%@ page import="jxl.*" %>  
  3. <%request.setCharacterEncoding("gb2312");%>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  8. <title>用jxl.jar来读取excel2003</title>  
  9. </head>  
  10. <body bgcolor="#F2F8FF">  
  11.   
  12. <%  
  13. try{  
  14.   String file = application.getRealPath("/") + "upload\\test.xls";  
  15.   Workbook workbook = Workbook.getWorkbook(new File(file));  
  16.   Sheet sheet = workbook.getSheet(0);  
  17.   Cell c1 = null;  
  18.   String s1 = "";  
  19.   
  20.   c1 = sheet.getCell(0,0);  
  21.   s1 = c1.getContents();  
  22.   
  23.   workbook.close();  
  24.   
  25.   out.print(s1);  
  26.   
  27. }catch(Exception e){  
  28.   out.print(e.toString());  
  29. }  
  30.   
  31. %>  
  32.   
  33. </body>  
  34. </html>  


 相关jar库及代码资源下载:

http://download.csdn.net/detail/uixor_/4016423