Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

608
Visualizações
How to save from JTable to CSV or Excel?

Is there a way to save JTable data to excel? I would like to save the data that I input from the program, from the table and then to a CSV file.

I would like to have it so that there is a button that will then save the inputted data from the GUI into the table and then to the CSV file.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

This may help you:-

Method to write to a csv file.

public static void exportToCSV(JTable table,
        String path) {

    try {

        TableModel model = table.getModel();
        FileWriter csv = new FileWriter(new File(path));

        for (int i = 0; i < model.getColumnCount(); i++) {
            csv.write(model.getColumnName(i) + ",");
        }

        csv.write("\n");

        for (int i = 0; i < model.getRowCount(); i++) {
            for (int j = 0; j < model.getColumnCount(); j++) {
                csv.write(model.getValueAt(i, j).toString() + ",");
            }
            csv.write("\n");
        }

        csv.close();
    } catch (IOException e) {
       System.out.println("Error "+e);
    }
}

For reading and showing it to a JTable you can use OpenCSV.

CSVReader reader = new CSVReader(new FileReader("file.csv")); 
List list = reader.readAll();
JTable table = new JTable(list.toArray());
over 4 years ago · Santiago Trujillo Relatório

0

There is no 'magic function' of a JTable that I know of that will do this. JTable is a UI component, and what you're asking for is a data function, not a user interface function. And it doesn't have much to do with eclipse, which is just the IDE one uses to write the code.

I think what you may be looking for is the model on which the JTable is based. The first solution that occurs to me is to alter that model code to have a method that will write out the data in the format you want; then a(nother) button on your UI would invoke this method on the model to write out the data when the user chooses. There are likely other ways you might get this done, but that seems the most straightforward and easiest.

over 4 years ago · Santiago Trujillo Relatório

0

You can try this code:

public static boolean convertToCSV(JTable table,
                                   String path) {
try {
    TableModel model = table.getModel();
    FileWriter csv = new FileWriter(new File(path));

    for (int i = 0; i < model.getColumnCount(); i++) {
        csv.write(model.getColumnName(i) + ",");
    }
    csv.write("\n");

    for (int i = 0; i < model.getRowCount(); i++) {
        for (int j = 0; j < model.getColumnCount(); j++) {
            csv.write(model.getValueAt(i, j).toString() + ",");
        }
        csv.write("\n");
    }
    csv.close();
    return true;
} catch (IOException e) {
    e.printStackTrace();
}
return false;
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda