Read Excel file e importar para jTable

5 respostas
C

Gelera, tenho um arquivo em excel com 7 colunas e 31 linhas que são os dias do mês, achei um Api com um código que lista essas informações em System.out.println, ó que preciso listar essas informações de forma organizada em uma jtable ou List:

segue meu código:
package excelReader;

import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

/**
 *
 * @author Caio Fernando
 */
public class ReadExcel {

    private String inputFile;

    public void setInputFile(String inputFile) {
            this.inputFile = inputFile;
    }

    public void read() throws IOException  {
            File inputWorkbook = new File(inputFile);
            Workbook w;
            try {
                    w = Workbook.getWorkbook(inputWorkbook);
                    // Get the first sheet
                    Sheet sheet = w.getSheet(0);
                    // Loop over first 10 column and lines


                    for (int j = 0; j < sheet.getColumns(); j++) {
                            for (int i = 0; i < sheet.getRows(); i++) {
                                    Cell cell = sheet.getCell(j, i);
                                    CellType type = cell.getType();
                                    if (cell.getType() == CellType.LABEL) {
                                            System.out.println("I got a label "
                                                            + cell.getContents());
                                    }

                                    if (cell.getType() == CellType.NUMBER) {
                                            System.out.println("I got a number "
                                                            + cell.getContents());
                                    }

                            }
                    }
            } catch (BiffException e) {
                    e.printStackTrace();
            }
}

}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            ReadExcel test = new ReadExcel();
            test.setInputFile("c:/temp/lars.xls");
            test.read();
            // TODO add your handling code here:
        } catch (IOException ex) {
            Logger.getLogger(SinformandoView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

alguém já fez isso?

5 Respostas

J

Simples, tu ja tem a logica para ler da planilha no método read, altera ele ou cria um novo baseado nele, e ao invés de fazer o sysout, popula a tabela que tu quer.

I

Crie um objeto com as informações lidas do arquivo excel. Após faça com que sua JTable tenha um model com este objeto.

C

Então esse lance de ‘Crie um objeto com as informações lidas do arquivo excel’, que não sei fazer.

você poderia me ajudar,?

I
Como assim, não sabe?

Você tem conhecimento sobre O.O ?

Caso tenha, a maneira seria esta:

MeuObjetoExcel objeto = new MeuObjetoExcel();

objeto.setDado1(dado1);

objeto.setDado2(dado2);

.

.

.
C

Igor, dei uma procurada no google e me parece que existe um forma mais “fácil” de se fazer isso.

http://www.zfqjava.com/article/How-to-import-excel-into-JTabel.html

Vou ver se condigo colocar em prática

Criado 19 de julho de 2011
Ultima resposta 19 de jul. de 2011
Respostas 5
Participantes 3