Change Column by Row when typing Excel with Java

0

I try to change a column to put it as the subject of the rest of the columns, this is the code I have so far, which generates excel.

  while (res.next()) {
                Row filaDatos = sheet.createRow(numfilaDatos);
                for (int a = 0; a < numCol; a++) {
                    Cell celdaDatos = filaDatos.createCell(a);
                    celdaDatos.setCellStyle(datosEstilo);
                    if (a == 2) {
                        celdaDatos.setCellValue(res.getInt(a + 1));
                    } else {
                        celdaDatos.setCellValue(res.getString(a + 1));
                    }
                }
                numfilaDatos++;
            }
            if (option == JFileChooser.APPROVE_OPTION) {
                if (fileChooser.getSelectedFile() != null) {
                    FileOutputStream out = new FileOutputStream(fileChooser.getSelectedFile() + ".xls");
                    book.write(out);
                }
            }
    
asked by Summer 29.08.2018 в 21:27
source

1 answer

1

You can populate the excel so that it already comes in the format you need. So you avoid having to change columas later:

For example:

XSSFSheet spreadsheet = workbook.createSheet("EntregaRecepcion");

XSSFRow row = spreadsheet.createRow(1);
XSSFCell cell;
cell = row.createCell(1);
cell.setCellValue("Columna1");
cell = row.createCell(2);
cell.setCellValue("Columna2");



ps = con.prepareStatement("EXEC PA_ENTREGA " + quincena.getSelectedItem() + "," + ano.getSelectedItem());

res = ps.executeQuery();

int i = 2;
while(res.next()) {
    row = spreadsheet.createRow(i);
    cell = row.createCell(1);
    cell.setCellValue(res.getInt("datoColuma1"));
    cell = row.createCell(2);
    cell.setCellValue(res.getString("datoColuma2"));
    i++;
 }
    
answered by 30.08.2018 / 08:52
source