Popular Excel with Java object

Asked

Viewed 103 times

1

Hi, I’m having a problem to popular a table the way I need, I want to do something like this.

inserir a descrição da imagem aqui

But with my code I can only get people like this.

inserir a descrição da imagem aqui

How do I format the same way? I tried to skip the lines to do similar but I can’t.

public void exportar() throws IOException {

    ArrayList<Dados> array = listaDados;
    String fileName = "C:/novo.xlsx";
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheetEtiquetas = workbook.createSheet("Etiquetas");

    int rownum = 0;

    for (Dados dados : array) {
        Row row = sheetEtiquetas.createRow(rownum++);
        int cellnum = 0;

        Cell cellOp = row.createCell(cellnum++);
        cellOp.setCellValue("Número O.P: " + dados.getNumeroOP());
        Cell cellNomeItem = row.createCell(cellnum++);
        cellNomeItem.setCellValue("Código da Peça: " + dados.getNomeItem());

        Cell cellPedido = row.createCell(cellnum++);
        cellPedido.setCellValue("Número do Pedido: " + dados.getNumeroPedido());
        Cell cellCliente = row.createCell(cellnum++);
        cellCliente.setCellValue("Cliente: " + dados.getCliente());

        Cell cellQuantidade = row.createCell(cellnum++);
        cellQuantidade.setCellValue("Quantidade: " + dados.getQuantidade());
        rownum++;

    }

    try {
        FileOutputStream out
                = new FileOutputStream(new File(fileName));
        workbook.write(out);
        out.close();
        System.out.println("Arquivo Excel criado com sucesso!");

    } catch (FileNotFoundException e) {
        e.printStackTrace();
        System.out.println("Arquivo não encontrado!");
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Erro na edição do arquivo!");
    }

Thank you.

1 answer

1

The problem is that you are creating all the items in the same row. To skip lines, another row.

for (Dado data : array) {

    Row row = sheetEtiquetas.createRow(rownum++);
    int cellnum = 0;

    Cell cellOp = row.createCell(cellnum++);
    cellOp.setCellValue("Número O.P: " + data.getNumeroOP());
    Cell cellNomeItem = row.createCell(cellnum++);
    cellNomeItem.setCellValue("Código da Peça: " + data.getNomeItem());

    // Alinha a esquerda
    row = sheetEtiquetas.createRow(rownum++);
    cellnum = 0;

    Cell cellPedido = row.createCell(cellnum++);
    cellPedido.setCellValue("Número do Pedido: " + data.getNumeroPedido());
    Cell cellCliente = row.createCell(cellnum++);
    cellCliente.setCellValue("Cliente: " + data.getCliente());

    // Alinha a esquerda
    row = sheetEtiquetas.createRow(rownum++);
    cellnum = 0;

    Cell cellQuantidade = row.createCell(cellnum++);
    cellQuantidade.setCellValue("Quantidade: " + data.getQuantidade());
    rownum++;
}

Note that to jump the line, I created a new row, and reset the value of cellNum, so that the next inserted values are left.

resultado

  • Fantastic, it worked perfectly! Thank you very much, know how I can put the edge and set the size of Cell?

  • If it worked, mark the answer as accepted, to make life easier in future research! Regarding the border, you can check this link: https://stackoverflow.com/questions/5720049/add-borders-to-cells-in-poi-generated-excel-file

  • 1

    I’ve already scored the answer, but I don’t have enough reputation yet to show up, but I said it was accounted for! Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.