I’m writing an application which reads data from a Microsoft Access database and writes this data into an Excel Sheet. Everything works fine but not the writing of the new data to the file. Here is the code:
private static XSSFWorkbook readFile(String filename) throws IOException {
// return new XSSFWorkbook(new FileInputStream(filename));
InputStream inp = new FileInputStream(filename);
XSSFWorkbook wb = null;
try {
wb = (XSSFWorkbook) WorkbookFactory.create(inp);
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return wb;
}
public void writeToFile(LinkedList<TimeRowWrapper> list) {
try {
// read file
XSSFWorkbook workbook = readFile(fileName);
// choose table from index
XSSFSheet sheet = workbook.getSheetAt(kapaSheetIndex);
for (Row row : sheet) {
int rowNum = row.getRowNum();
// iterate over list for row id
for (TimeRowWrapper trw : list) {
if (rowNum == trw.getRowNumber()) {
double hour = Math
.round(trw.getTotalTime() / 60.0 * 100.) / 100.;
Cell cell = row.getCell(timeCol);
cell.setCellValue(hour);
}
}
}
XSSFFormulaEvaluator.evaluateAllFormulaCells(workbook);
FileOutputStream out = new FileOutputStream(fileName);
workbook.write(out);
out.close();
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
The sourcecode I posted was correct. In the Excelfile were some Colums with the withe of 0 – so I tryed to write in a column which was not shown.