0
我有一段代碼直接進入excel工作表,它基於指定的行號和列名,我的問題是如果列名爲null,那麼值將不會設置在其餘的列。 excel中沒有設置數據
練成沒有價值得到後列「d」
下面是我的代碼
public boolean setExcelData(String sheetName, String colName, int rowNum, String data) throws AutoException {
dataFile();
try {
if (rowNum <= 0)
throw new AutoException(EXCEPTIION);
int index = wb.getSheetIndex(sheetName);
int colNum = -1;
if (index == -1)
throw new AutoException(EXCEPTIION);
XSSFSheet sheet = wb.getSheetAt(index);
Row row = sheet.getRow(0);
for (int i = 0; i < row.getLastCellNum(); i++) {
System.out.println(row.getCell(i));
if (row.getCell(i) == null) {
throw new AutoException(EXCEPTIION);
} else if (row.getCell(i).getStringCellValue().trim().equals(colName)) {
colNum = i;
break;
}
}
if (colNum == -1)
throw new AutoException(EXCEPTIION);
sheet.autoSizeColumn(colNum);
row = sheet.getRow(rowNum - 1);
if (row == null)
row = sheet.createRow(rowNum - 1);
Cell cell = row.getCell(colNum);
if (cell == null)
cell = row.createCell(colNum);
cell.setCellValue(data);
FileOutputStream fileOut = new FileOutputStream(excelFilePath);
wb.write(fileOut);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
wb.close();
in.close();
} catch (Exception e) {
}
}
return true;
}
任何一個可以幫我補充道。 由於事先