Help with logic of a system that writes data to Excel columns

Asked

Viewed 70 times

1

Guys I have an application and I can pass the data to excel, the problem came up now because I have to make a loop and I’m not finding the best way to do it. I have several lots, for example, 1,2,3,4,5,6,7,8. Each lot has several customers, let’s assume that lot 1 has 2 customers, and lot 2 has 4 customers. What I want to do is basically list it like this:

Numero do lote 1:
Diego
Bruno

Numero do lote 2:
Rafael
Carlos
Ana
Tiago

I’m doing the loop like this, but it always repeats everyone on all excel cells, and it looks like this:

Numero do lote 1:
Rafael
Carlos
Ana
Tiago

Numero do lote 2:
Rafael
Carlos
Ana
Tiago

That is the code:

 int cont2 = 0;

        //Laço que pega o numero do lote e sempre o repete após os clientes
        for (int r = 0; r < listaGuia.size(); r++) {
            XSSFRow cabRowAnaliticor = sheet1.createRow((cont2 * 1) + 7);
            for (int x = 0; x <= 11; x++) {
                cabRowAnaliticor.createCell(x);
            }
            XSSFCell cellAnaR = cabRowAnaliticor.getCell(0);
            cellAnaR.setCellStyle(estiloS2);
            cellAnaR.setCellValue(listaGuia.get(r).getNumeroLote());

            //Percorre a lista e preenche as celulas
            for (int i = 0; i < listaGuia.size(); i++) {

                XSSFRow cabRowAnalitico7 = sheet1.createRow((cont2 * 1) + 8);
                for (int x = 0; x <= 11; x++) {
                    cabRowAnalitico7.createCell(x);
                }
                System.err.println("Lote no rel: " + listaGuia.get(i).getNumeroLote());

                XSSFCell cellAnaA5 = cabRowAnalitico7.getCell(0);
                cellAnaA5.setCellStyle(estiloS2);
                cellAnaA5.setCellValue(listaGuia.get(i).getNumeroGuia());

                XSSFCell cellAnaB5 = cabRowAnalitico7.getCell(1);
                cellAnaB5.setCellStyle(estiloS2);
                cellAnaB5.setCellValue(listaGuia.get(i).getNumeroCarteira());

                XSSFCell cellAnaC5 = cabRowAnalitico7.getCell(2);
                cellAnaC5.setCellStyle(estiloS2);
                cellAnaC5.setCellValue(listaGuia.get(i).getNomeBeneficiario());

                XSSFCell cellAnaD5 = cabRowAnalitico7.getCell(3);
                cellAnaD5.setCellStyle(estiloS2);
                cellAnaD5.setCellValue(listaGuia.get(i).getDataRealizacao());

                XSSFCell cellAnaE5 = cabRowAnalitico7.getCell(4);
                cellAnaE5.setCellStyle(estiloS2);
                cellAnaE5.setCellValue(listaGuia.get(i).getCodServico());

                XSSFCell cellAnaF5 = cabRowAnalitico7.getCell(5);
                cellAnaF5.setCellStyle(estiloS2);
                cellAnaF5.setCellValue("Outras Tabelas");

                XSSFCell cellAnaG5 = cabRowAnalitico7.getCell(6);
                cellAnaG5.setCellStyle(estiloS2);
                cellAnaG5.setCellValue(listaGuia.get(i).getDescServico());

                XSSFCell cellAnaH5 = cabRowAnalitico7.getCell(7);
                cellAnaH5.setCellStyle(estiloS2);
                cellAnaH5.setCellValue(listaGuia.get(i).getQtdExec());

                XSSFCell cellAnaI5 = cabRowAnalitico7.getCell(8);
                cellAnaI5.setCellStyle(estiloS1);
                cellAnaI5.setCellValue(listaGuia.get(i).getFilme());

                XSSFCell cellAnaJ5 = cabRowAnalitico7.getCell(9);
                cellAnaJ5.setCellStyle(estiloS1);
                cellAnaJ5.setCellValue(listaGuia.get(i).getValorServico());

                XSSFCell cellAnaK5 = cabRowAnalitico7.getCell(10);
                cellAnaK5.setCellStyle(estiloS1);
                cellAnaK5.setCellValue(listaGuia.get(i).getHonorario());

                if (listaGuia.get(i).getTipoGlosa() != null) {
                    XSSFCell cellAnaL5 = cabRowAnalitico7.getCell(11);
                    cellAnaL5.setCellStyle(estiloS1);
                    float xxx = 0f;
                    cellAnaL5.setCellValue(xxx);
                } else {
                    XSSFCell cellAnaL5 = cabRowAnalitico7.getCell(11);
                    cellAnaL5.setCellStyle(estiloS1);
                    cellAnaL5.setCellValue(listaGuia.get(i).getValor());
                }
                cont2++;

            }
        }
  • 1

    In which column is the batch field? Your FOR does not check if a particular customer is in a batch, then it adds all in all batches. Add an IF to know if customer X is in batch Y.

  • Diego, the problem is that you go through the same list twice. You need two lists, one with lots and the other with tabs/names.

  • I see, hence in the first FOR I use the list of lots?

  • Celso, that’s the problem I’m thinking of how I can verify this.

No answers

Browser other questions tagged

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