将csv导入JTable

时间:2012-05-05 21:19:02

标签: java swing jtable

我有一个csv文件,我想把它导入JTable。

是否有一个简单的示例显示如何将csv文件导入JTable?

3 个答案:

答案 0 :(得分:8)

使用OpenCSV:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv")); 
List myEntries = reader.readAll();
JTable table = new JTable(myEntries.toArray());

答案 1 :(得分:4)

最后一个答案对我没用,因为JTable想要一个Object [] []和一个String [](列名)...... 我必须做这样的事情:

import com.opencsv.CSVReader;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTable; 
import java.io.FileReader;

Object[] columnnames;
transient CSVReader CSVFileReader;
CSVFileReader = new CSVReader(new FileReader(csvFile));
List myEntries = CSVFileReader.readAll();
columnnames = (String[]) myEntries.get(0);
DefaultTableModel tableModel = new DefaultTableModel(columnnames, myEntries.size()-1); 
int rowcount = tableModel.getRowCount();
for (int x = 0; x<rowcount+1; x++)
{
    int columnnumber = 0;
    // if x = 0 this is the first row...skip it... data used for columnnames
    if (x>0)
    {
        for (String thiscellvalue : (String[])myEntries.get(x))
        {
            tableModel.setValueAt(thiscellvalue, x-1, columnnumber);
            columnnumber++;
        }
    }
}

JTable MyJTable = new JTable(tableModel);

此外,如果您想在数据中保留反斜杠字符,请将其用作构造函数:

CSVFileReader = new CSVReader(new FileReader(csvFile), ',', '"', '\0');

这设置&#34; \ 0&#34;逃避角色。我认为将逃避角色置之不理。请参阅此主题:opencsv in java ignores backslash in a field value

答案 2 :(得分:0)

如果要/需要避免使用CSVReader,则也可以使用BufferedReader。当然,它不像CSVReader那样简单。您肯定可以改进它,因为我只是Java文件管理的入门者。

这部分地取自https://www.youtube.com/watch?v=L2xczUN9aI0上的“ JAVA IO-如何将Java中的文本文件数据导入到JTable中[使用源代码]”

import java.io.*;
import java.util.*;
import javax.swing.JTable;
import javax.swing.table.*;

public class test {
    public static void main(String[] args) {
        String inputFileName;
        File inputFile;
        String firstRow;
        Vector<Vector<String>> vectorVectorStringsData = new Vector<Vector<String>>();
        Vector<String> vectorStrings = new Vector<String>();
        Vector<String> vectorColumnIdentifiers = new Vector<String>();
        String[] columnIdentifiers;
        DefaultTableModel model = new DefaultTableModel();
        JTable jTable;
        
        inputFileName = "yourFileName.csv";
        inputFile = new File(inputFileName);
        try (FileReader fr = new FileReader(inputFile);
            BufferedReader br = new BufferedReader(fr)) 
        {
            firstRow = br.readLine().trim();
            if (firstRow != null) {
                // headers:
                columnIdentifiers = firstRow.split(",");

                vectorColumnIdentifiers = new Vector<String>();
                for (int j =0; j < columnIdentifiers.length; j++) {
                    vectorColumnIdentifiers.add(columnIdentifiers[j]);
                }
            }
            // rows
            Object[] tableLines = br.lines().toArray();
            // data rows
            for (int i = 0; i < tableLines.length; i++) {
                System.out.println("4");
                String line = tableLines[i].toString().trim();
                String[] dataRow = line.split(",");
                vectorStrings = new Vector<String>();
                for (int j =0; j < dataRow.length; j++) {
                    vectorStrings.add(dataRow[j]);
                }
                vectorVectorStringsData.add(vectorStrings);
            }
            
            fr.close();
        }
        catch (IOException ioe) {
            System.out.println("error: " + ioe.getMessage());
        }

        model.setDataVector(vectorVectorStringsData, vectorColumnIdentifiers);
        jTable = new JTable(model);
    }
}