When i use below method this will print the arraylist in the excelsheet. When i call this method in a loop, second iteration prints in the same sheet. I want to print next iteration in different sheet, possibly create a new sheet and write there. What changes do i have to make to make it print the iteration in different sheet of same excel workbook.
for (int indexSelect=1;indexSelect>=10; indexSelect++){
excelWrite(Prints, indexSelect)
}
public static void excelWrite(ArrayList<Object> Prints, int indexSelect) {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Achievers"+indexSelect);
System.out.println("Getting sheet at ..:" +indexSelect);
//Sheet sheet = workbook.getSheetAt(indexSelect);
int rowCount = 0;
// for (Object Names : Prints) {
// org.apache.poi.ss.usermodel.Row row = sheet.createRow(++rowCount);
//org.apache.poi.ss.usermodel.Row row = sheet.createRow(rowCount);
int columnCount = indexSelect;
for (Object field :Prints) {
org.apache.poi.ss.usermodel.Row row = sheet.createRow(++rowCount);
Cell cell = row.createCell(columnCount);
if (field instanceof String) {
cell.setCellValue((String) field);
} else if (field instanceof Integer) {
cell.setCellValue((Integer) field);
}
}
// }
System.out.println("Current indexSelect is: "+indexSelect);
try {
FileOutputStream outputStream = new FileOutputStream("C:\\Softwares\\DataSource\\AchieversDataCopy.xlsx") ;
workbook.write(outputStream);
workbook.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}