文件名称:EXCEL POI 代码和资源下载
文件大小:1.55MB
文件格式:RAR
更新时间:2011-12-11 11:03:15
POI
package com.kingtop.xmkj.www; import java.io.FileInputStream; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class OfficePoi extends HttpServlet { public void init() throws ServletException { } //Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String fileToBeRead="e://test.xls"; ReadExcel(fileToBeRead); } public void ReadExcel(String fileToBeRead) { try { HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead)); HSSFSheet sheet = workbook.getSheet("Sheet1"); int rows = sheet.getPhysicalNumberOfRows(); for (int r = 0; r < rows; r++) { HSSFRow row = sheet.getRow(r); if (row != null) { int cells = row.getPhysicalNumberOfCells(); String value = ""; System.out.println(cells); for (short c = 0; c < cells; c++) { HSSFCell cell = row.getCell(c); if (cell != null) { switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_FORMULA: break; case HSSFCell.CELL_TYPE_NUMERIC: value += (long) cell.getNumericCellValue() + "\t"; break; case HSSFCell.CELL_TYPE_STRING: value += cell.getStringCellValue() + "\t"; break; default: value += "\t"; } } } //下面可以将查找到的行内容用SQL语句INSERT到oracle System.out.println(value); // } } } catch (Exception e) { System.out.println(e); } } //Process the HTTP Post request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } //Clean up resources public void destroy() { } } 以上是代码,文件下载后解压到WEB-INF/lib下面就可以了
【文件预览】:
poi-scratchpad-3.0.1-FINAL-20070705.jar
poi-contrib-3.0.1-FINAL-20070705.jar
poi-3.0.1-FINAL-20070705.jar