I created Excel file with Apache POI.
In my database, I have a list of 400 people. I want to write their name and surname to that Excel file.
Here is my code sample:
try {
FileOutputStream fileOut = new FileOutputStream("C:/testExcelForJava/test.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet = workbook.createSheet("POI Worksheet");
// index from 0,0... cell A1 is cell(0,0)
HSSFRow row1 = worksheet.createRow((short) 0);
HSSFCell cellA1 = row1.createCell((short) 0);
cellA1.setCellValue("Ad");
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellA1.setCellStyle(cellStyle);
HSSFCell cellB1 = row1.createCell((short) 1);
cellB1.setCellValue("Soyad");
cellStyle = workbook.createCellStyle();
cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellB1.setCellStyle(cellStyle);
List<Driver> driverList = Dao.driverDao.queryForAll();
for(Driver driver :driverList){
String name = driver.getName();
String surname = driver.getSurname();
String birthDay = driver.getBirthDay();
cellA1.setCellValue(name);
cellB1.setCellValue(surname);
}
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
} catch(FileNotFoundException e){
e.printStackTrace();
log.error(e.getMessage());
} catch(IOException e){
e.printStackTrace();
log.error(e.getMessage());
}
When I look at to my Excel file, I only see the last person of list.
How can I write all my people's information?
Thanks