How to use JProgressBar with reads of files in excel

0

I have a method that receives a file of type Excel, and a Jtable. This method reads the file and throws the data contained within it into the jTable.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class ControleImportPlanilha {

public void importarPlanilha(File arquivo, JTable tabela){

    try {
        //Recebe o arquivo da planilha 
        XSSFWorkbook book = (XSSFWorkbook) WorkbookFactory.create(new FileInputStream(arquivo));
        //Pega a primeira folha da planilha
        Sheet folha = book.getSheetAt(0);
        //percorre as linhas da planilha
        Iterator linhaItetator = folha.rowIterator();
        //prepara o indice das linha para percorrer as celulas
        int indiceLinha = -1;

        DefaultTableModel modeloTable = new DefaultTableModel();

        tabela.setModel(modeloTable);


        while(linhaItetator.hasNext()){

            indiceLinha++;

            Row linha = (Row) linhaItetator.next();

            Iterator colunaIterator = linha.cellIterator();

            int qtdColunas = linha.getLastCellNum();

            Object[] listaColuna = new Object[qtdColunas];

            int indiceColuna = -1;

            while(colunaIterator.hasNext()){

                indiceColuna++;

                Cell celula = (Cell) colunaIterator.next();

                if(indiceLinha == 0){
                    modeloTable.addColumn(celula.getStringCellValue());
                }else{
                    if(celula != null){

                        switch(celula.getCellType()){

                            case Cell.CELL_TYPE_NUMERIC:
                                listaColuna[indiceColuna]=(int)Math.round(celula.getNumericCellValue());
                              //  System.out.print(listaColuna[indiceColuna]=(int)Math.round(celula.getNumericCellValue()));
                                break;

                            case Cell.CELL_TYPE_STRING:
                                listaColuna[indiceColuna]=celula.getStringCellValue();
                             //   System.out.print(listaColuna[indiceColuna]=celula.getStringCellValue());
                                break;

                            case Cell.CELL_TYPE_BOOLEAN:
                                listaColuna[indiceColuna]=celula.getBooleanCellValue();
                           //     System.out.println(listaColuna[indiceColuna]=celula.getBooleanCellValue());
                                break;

                            case Cell.CELL_TYPE_FORMULA:
                                listaColuna[indiceColuna]=celula.getCellFormula();
                              //  System.out.println(listaColuna[indiceColuna]=celula.getCellFormula());
                                break;

                            case Cell.CELL_TYPE_BLANK:    
                                listaColuna[indiceColuna]=celula.getStringCellValue();
                             //   System.out.print(listaColuna[indiceColuna]=celula.getStringCellValue());
                                break;

                            case Cell.CELL_TYPE_ERROR:
                                listaColuna[indiceColuna]=celula.getErrorCellValue();
                             //   System.out.println(listaColuna[indiceColuna]=celula.getErrorCellValue());

                            default:
                                listaColuna[indiceColuna]=celula.getDateCellValue();
                            //    System.out.print(listaColuna[indiceColuna]=celula.getDateCellValue());
                                break;
                        }
                    }
                }

            }
            if(indiceLinha !=0){
             modeloTable.addRow(listaColuna);
            }
            System.out.println("\n");
        }


    } catch (FileNotFoundException ex) {
        Logger.getLogger(ControleImportPlanilha.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ControleImportPlanilha.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidFormatException ex) {
        Logger.getLogger(ControleImportPlanilha.class.getName()).log(Level.SEVERE, null, ex);
    } catch (EncryptedDocumentException ex) {
        Logger.getLogger(ControleImportPlanilha.class.getName()).log(Level.SEVERE, null, ex);
    }

}

}

It depends on the size of the Excel file, the process may take a while and the idea was to add a JProgressBar, so the user does not feel that the software is stuck.

I call this method from a JButton that captures the path of the selected file and calls the method to read

jButtonSelecionarArquivoActionPerformed(java.awt.event.ActionEvent evt) {                                                         
   ControleSelecaoArquivoExcel select = new ControleSelecaoArquivoExcel();

   File[] caminho = select.seleciona();

   ControleImportPlanilha importar = new ControleImportPlanilha();

   importar.importarPlanilha(caminho[0], jTableResultadoPlanilha);

}

Can anyone help me do such a feat?

    
asked by anonymous 10.03.2016 / 18:24

0 answers