I need to check if the worksheet exists. If it exists, you must type in the next existing row and do not create a new worksheet.
You are currently deleting the current spreadsheet and I always get only 1 line written in the spreadsheet.
How do I solve this?
public class ApachePOIExcelWrite {
private static final String FILE_NAME = "c:/viagem.xlsx";
XSSFWorkbook workbook = new XSSFWorkbook();
//name Sheet
XSSFSheet sheet = workbook.createSheet("Viagem");
Object[][] datatypes = {
//head colums
{
"Destino",
"Valor por pessoa",
"Aeroporto",
"Hotel",
"data Pesquisa",
"Hora Pesquisa"
},
{
destino,
pega_valor,
aeroporto.replace("GRU", "Guarulhos").replace("CGH", "Congonhas"),
hotel.replaceAll("Pontos", "Estrelas"),
data_da_pesquisa.format(d),
hora_da_pesquisa.format(d)
}
};
int rowNum = 0;
System.out.println("Creating excel");
for (Object[] datatype: datatypes) {
Row row = sheet.createRow(rowNum++);
int colNum = 0;
for (Object field: datatype) {
Cell cell = row.createCell(colNum++);
if (field instanceof String) {
cell.setCellValue((String) field);
} else if (field instanceof Integer) {
cell.setCellValue((Integer) field);
}
}
}
try {
FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
workbook.write(outputStream);
workbook.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}