Posted on 2012-09-12 22:43
zolly 阅读(794)
评论(0) 编辑 收藏
read xls - before Excel 2007
try {
PRFile file =new PRFile("Excel File Name.xls");
java.io.BufferedInputStream in = new java.io.BufferedInputStream(
new PRInputStream(file));
// open HSSFWorkbook
org.apache.poi.poifs.filesystem.POIFSFileSystem fs = new org.apache.poi.poifs.filesystem.POIFSFileSystem(
in);
org.apache.poi.hssf.usermodel.HSSFWorkbook wb = new org.apache.poi.hssf.usermodel.HSSFWorkbook(
fs);
org.apache.poi.hssf.usermodel.HSSFCell cell = null;
for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
org.apache.poi.hssf.usermodel.HSSFSheet st = wb
.getSheetAt(sheetIndex);
for (int rowIndex = 0; rowIndex <= st.getLastRowNum(); rowIndex++) {
org.apache.poi.hssf.usermodel.HSSFRow row = st
.getRow(rowIndex);
if (row == null) {
continue;
}
String strJobName = "";
cell = row.getCell(0);
strJobName = cell.getStringCellValue().trim();
ClipboardProperty propJobName = myStepPage.getProperty(".Jobs(<append>).JobName");
propJobName.setValue(strJobName);
}
}
in.close();
} catch (java.io.FileNotFoundException e) {
oLog.error(e.getMessage());
} catch (java.io.IOException e) {
oLog.error(e.getMessage());
}
Excel 2007 or later
try {
PRFile file =new PRFile("Excel File Name.xlsx");
java.io.BufferedInputStream in = new java.io.BufferedInputStream(
new PRInputStream(file));
// open XSSFWorkbook
org.apache.poi.openxml4j.opc.OPCPackage docPackage = org.apache.poi.openxml4j.opc.OPCPackage
.open(in);
org.apache.poi.xssf.usermodel.XSSFWorkbook wb = new org.apache.poi.xssf.usermodel.XSSFWorkbook(
docPackage);
org.apache.poi.xssf.usermodel.XSSFCell cell = null;
for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
org.apache.poi.xssf.usermodel.XSSFSheet st = wb
.getSheetAt(sheetIndex);
for (int rowIndex = 0; rowIndex <= st.getLastRowNum(); rowIndex++) {
org.apache.poi.xssf.usermodel.XSSFRow row = st
.getRow(rowIndex);
if (row == null) {
continue;
}
String strJobName = "";
cell = row.getCell(0);
strJobName = cell.getStringCellValue().trim();
ClipboardProperty propJobName = myStepPage.getProperty(".Jobs(<append>).JobName");
propJobName.setValue(strJobName);
}
}
in.close();
} catch (java.io.FileNotFoundException e) {
oLog.error(e.getMessage());
} catch (java.io.IOException e) {
oLog.error(e.getMessage());
} catch (org.apache.poi.openxml4j.exceptions.InvalidFormatException e) {
oLog.error(e.getMessage());
}