這幾天在弄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,以下:
<%@ 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文件同上
<%@ 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便可
<%@ 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庫及代碼資源下載: