I try to write a file with query, where the first column of the query is the header of the rest of the columns when writing it in excel, this is the code which writes it, the problem is that it only writes the last value of the query.
Workbook LIBRO = new XSSFWorkbook();
JFileChooser fileChooser = new JFileChooser();
int option = fileChooser.showSaveDialog(null);
XSSFSheet spreadsheet = (XSSFSheet) LIBRO.createSheet("Movimientos de Nomina");
PreparedStatement ps;
ResultSet res;
XSSFRow row = spreadsheet.createRow(1);
XSSFCell cell;
ps = con.prepareStatement("EXEC PA_QUERY");
res = ps.executeQuery();
int numCol = res.getMetaData().getColumnCount();
while (res.next()) {
for (int i = 0; i <= numCol; i++) {
row = spreadsheet.createRow(i);//CREAR FILAS
for (int j = 0; j < 1; j++) {
if (i == 0) {//para la cabecera
cell = row.createCell(0);//CELDAS PARA LA CABECERA, JUENTO CON LA POSICION
cell.setCellStyle(headerStyle);
cell.setCellValue(res.getString("DATO1"));//AÑADE INFORMACION
} else {//para el contenido
cell = row.createCell(0);//SE CREA CELDA PARA RESTO DE COLUMNAS DE ResultSet, Y POSICION
cell.setCellValue(res.getString("DATO2")); //AÑADE INFORMACION DE ResultSet
cell = row.createCell(1);//SE CREA CELDA PARA RESTO DE COLUMNAS DE ResultSet, Y POSICION
cell.setCellValue(res.getInt("DATO3")); //AÑADE INFORMACION DE ResultSet
}
}
}
}
if (option == JFileChooser.APPROVE_OPTION) {
if (fileChooser.getSelectedFile() != null) {
FileOutputStream out = new FileOutputStream(fileChooser.getSelectedFile() + ".xls");
LIBRO .write(out);
}
}