我在java中使用POI 3.10读取excel(.xls)文件。
假设我的单元格值不包含/空白,它跳过该列并读取下一列值,但我想将该空白值也读为空白。
例如
header 1 header 2 header 3
mumbai madras
delhi jaipur
在标题2栏中没有值
我想mumbai null /"" madras delhi null /""斋浦尔
但我的代码给出了这个输出 - mumbai madras delhi jaipur
我的代码是
public static void main(String[] args) throws IOException {
String fname = "D:/Vijay/xls/vijay/test.xls";
InputStream fis = new FileInputStream(fname);
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet sheet = (HSSFSheet) wb.getSheetAt(0);
fis = new FileInputStream(fname);
Iterator rowIter = sheet.rowIterator();
while (rowIter.hasNext()) {
HSSFRow myRow = (HSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
while (cellIter.hasNext()) {
String cellvalue = "";
HSSFCell myCell = (HSSFCell) cellIter.next();
if (myCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
cellvalue = myCell.getStringCellValue();
} else if (myCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
cellvalue = "" + myCell.getNumericCellValue();
} else if (myCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
cellvalue = "" + myCell.getBooleanCellValue();
} else if (myCell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
cellvalue = "" + myCell.getCellFormula();
} else if (myCell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
cellvalue = "";
}
System.out.println(cellvalue);
}
}
}
提前致谢。
答案 0 :(得分:0)
谢谢大家。
这是我使用POI 3.10从java中的.xls文件读取空白和非空单元格值的工作代码
public static void main(String[] args) {
ArrayList<ArrayList<String>> Temp = new ArrayList<ArrayList<String>>();
ArrayList<String> Temp1 = new ArrayList<String>();
int row = 0;
String fname = "D:/Vijay/xls/vijay/ICICIPRUDENTIAL_D_000011161214_new.xls";
try {
InputStream fis = new FileInputStream(fname);
HSSFWorkbook workbook = new HSSFWorkbook(fis);
HSSFSheet sheet = workbook.getSheetAt(0);
FormulaEvaluator formulaEval =
workbook.getCreationHelper().createFormulaEvaluator();
int rowEnd = sheet.getLastRowNum();
int rowStart = sheet.getFirstRowNum();
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
Row r = sheet.getRow(rowNum);
int lastColumn = r.getLastCellNum();
int cols = 0;
Temp1 = new ArrayList<String>();
for (int cn = 0; cn < lastColumn; cn++) {
String cellvalue = "";
Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
if (c == null) {
cellvalue = "";
} else {
if (r.getCell(cn).getCellType() == HSSFCell.CELL_TYPE_STRING) {
cellvalue = r.getCell(cn).getStringCellValue();
} else if (r.getCell(cn).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
if (HSSFDateUtil.isCellDateFormatted(r.getCell(cn))) {
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date = (Date) formatter.parse(r.getCell(cn).getDateCellValue().toString());
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cellvalue = cal.get(Calendar.DATE) + "/"
+ (cal.get(Calendar.MONTH) + 1) + "/"
+ cal.get(Calendar.YEAR);
} else {
r.getCell(cn).setCellType(r.getCell(cn).CELL_TYPE_STRING);
cellvalue = "" + r.getCell(cn).getStringCellValue();
}
} else if (r.getCell(cn).getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
cellvalue = "" + r.getCell(cn).getBooleanCellValue();
} else if (r.getCell(cn).getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
cellvalue = ""+ formulaEval.evaluate(r.getCell(cn)).formatAsString();
}
}
Temp1.add(cols, cellvalue);
cols++;
}
if (Temp1.size() > 0) {
Temp.add(row, Temp1);
row++;
}
}
for (ArrayList al : Temp) {
System.out.println("Contents of temp " + al);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}