POI3.8和jxl讀取Excel例子

這幾天在弄java讀excel的問題,之前用的是jxl.jar來讀的,後來發現沒法讀取2007的,因而研究了一下POI,在網上下了最新版的poi-bin-3.8-beta5-20111217.zip,同時也參考了網上其它人寫的代碼,在jboss7服務器下成功讀出,發demo吧。 html

使用方法:jboss服務器或tomcat服務器,將下面的jsp文件保存到某個域名下,同時在此文件所在的目錄中新建一個upload目錄,而後將要讀取的test.xls和test.xlsx放到upload目錄下,而後運行jsp文件便可看到結果: java

(1)用poi.jar來讀取excel2003 apache

必需要用到的jar文件: api

poi-3.8-beta5-20111217.jar tomcat

poi-ooxml-3.8-beta5-20111217.jar 服務器

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

xmlbeans-2.3.0.jar dom

dom4j-1.6.1.jar xss

jsr173_1.0_api.jar  (若是jdk是1.5版本則須要此jar,若是是1.6以上版本則不須要) jsp

爲了方便測試,代碼爲jsp,以下:

  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>  
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.io.*" errorPage="" %>
<%@ 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.*" %>
<%request.setCharacterEncoding("gb2312");%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>用poi.jar來讀取excel2003</title>
</head>
<body bgcolor="#F2F8FF">

<%
try{
  String file = application.getRealPath("/") + "upload\\test.xls";
  POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
  HSSFWorkbook xwb = new HSSFWorkbook(fs);
  //讀取第一個sheet
  HSSFSheet sheet = xwb.getSheetAt(0);

  //定義 row、cell
  HSSFRow row;
  HSSFCell cell;
  String output;
  int rowNum;
  int cellNum;

  //循環輸出表格中的內容
  output = "";
  rowNum = sheet.getPhysicalNumberOfRows();

  for (int i = 0; i < rowNum; i++)
  {
    row = sheet.getRow(i);
    cellNum = row.getPhysicalNumberOfCells();
    for (int j = 0; j < cellNum; j++)
    {
      cell = row.getCell(j);
      output = output + cell.getStringCellValue() + "   ";
    }
    output = output + "<br />";
  }

  out.print(output);

}catch(Exception e){
  out.print(e.toString());
}

%>

</body>
</html>


(2)用poi讀取excel2007的代碼

所需jar文件同上

  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>  
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.io.*" errorPage="" %>
<%@ 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" %>
<%request.setCharacterEncoding("gb2312");%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>用poi.jar來讀取excel2007</title>
</head>
<body bgcolor="#F2F8FF">

<%
try{
  String file = application.getRealPath("/") + "upload\\test.xlsx";
  XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));
  //讀取第一個sheet
  XSSFSheet sheet = xwb.getSheetAt(0);

  //定義 row、cell
  XSSFRow row;
  XSSFCell cell;
  String output;
  int rowNum;
  int cellNum;

  //循環輸出表格中的內容
  output = "";
  rowNum = sheet.getLastRowNum();

  for (int i = 0; i <= rowNum; i++)
  {
    row = sheet.getRow(i);
    cellNum = row.getPhysicalNumberOfCells();
    for (int j = 0; j < cellNum; j++)
    {
      cell = row.getCell(j);
      output = output + cell.getStringCellValue() + "   ";
    }
    output = output + "<br />";
  }

  out.print(output);

}catch(Exception e){
  out.print(e.toString());
}

%>

</body>
</html>


(3)用jxl.jar讀取excel2003的代碼

須要jxl.jar便可

  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>  
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.io.*" errorPage="" %>
<%@ page import="jxl.*" %>
<%request.setCharacterEncoding("gb2312");%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>用jxl.jar來讀取excel2003</title>
</head>
<body bgcolor="#F2F8FF">

<%
try{
  String file = application.getRealPath("/") + "upload\\test.xls";
  Workbook workbook = Workbook.getWorkbook(new File(file));
  Sheet sheet = workbook.getSheet(0);
  Cell c1 = null;
  String s1 = "";

  c1 = sheet.getCell(0,0);
  s1 = c1.getContents();

  workbook.close();

  out.print(s1);

}catch(Exception e){
  out.print(e.toString());
}

%>

</body>
</html>


 相關jar庫及代碼資源下載:

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

相關文章
相關標籤/搜索