如何在Java中的JTable中填充一些Excel行? [英] How to fill some rows of Excel in a JTable on Java?

查看:151
本文介绍了如何在Java中的JTable中填充一些Excel行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Excel文件导入数据到Java JTable 。我可以导入所有的工作表,但是我只想导入一些行。这是我用来导入工作表的代码:

  private void remplirtable(JTable table,int numfeuil,String s) {

工作簿wb;
文件fichier =新文件;
try {
//créationde la table et importation du fichier
wb = Workbook.getWorkbook(fichier);
Sheet sheet = wb.getSheet(numfeuil);
String [] columnsnames = new String [sheet.getColumns()];
DefaultTableModel model = new DefaultTableModel();
// Remplir la tabledésignée
table.setModel(model);
int colonnes = sheet.getColumns();
int lignes = sheet.getRows();
对象数据[] = new Object [colonnes]; (int i = 0; i< lignes; i ++){
for(int j = 0; j< colonnes; j ++){
if(i == 0)
{
// Component c = super.prepareRenderer(renderer,j,i);
model.addColumn(sheet.getCell(j,i).getContents());

}
//System.out.println(sheet.getCell(j,i).getContents());
if(i> = 1)
data [j] = sheet.getCell(j,i).getContents();

} model.addRow(data);
} model.removeRow(0);
} catch(BiffException | IOException e){
}

}

为了只导入行(3,4,5),我使用了这个代码。 A 是我们存储行号的数组:

  private void RemplirPostes(JTable table,int numfeuil,String s,int [] A){
Workbook wb;
文件fichier =新文件;
try {
//créationde la table et importation du fichier
wb = Workbook.getWorkbook(fichier);
Sheet sheet = wb.getSheet(numfeuil);
DefaultTableModel model = new DefaultTableModel();
// Remplir la tabledésignée
table.setModel(model);
int colonnes = sheet.getColumns();
对象数据[] = new Object [colonnes]; (int k = 0; k
(int j = 0; j< colnes; j ++){
if(A [k] == 0)
{
// Component c = super.prepareRenderer(renderer,j,i);
model.addColumn(sheet.getCell(j,A [k])。getContents());

}
//System.out.println(sheet.getCell(j,i).getContents());
if(A [k]> = 1)

data [j] = sheet.getCell(j,A [k])。getContents();

} model.addRow(data);
}
model.removeRow(0);

} catch(BiffException | IOException e){
}

}

所有这些都不起作用。

解决方案

从这个完整的示例,以下代码创建一个表,其中指定的数量为 COLUMNS code> MIN_ROW 至 MAX_ROW ,包括$。

  private static final int COLUMNS = 4; 
private static final int MIN_ROW = 3;
private static final int MAX_ROW = 5;
...
DataFormatter format = new DataFormatter();
DefaultTableModel model = new DefaultTableModel(0,COLUMNS);
for(Row row:sheet){
if(row.getRowNum()> = MIN_ROW&& row.getRowNum()< = MAX_ROW){
Vector rowData = new向量(); (单元格:行)
{
rowData.add(format.formatCellValue(cell));
}
model.addRow(rowData);
}
}
...
JFrame f = new JFrame(TableTest);
f.add(new JTable(model));

附录:在目前的做法上,您可能希望适应不连续的行。它可能有助于用 List< Integer> 替换您的数组。然后,您可以使用 contains()代替上述谓词:

  if(a.contains(row.getRowNum()){...} 

附录:评估公式您可以将 FormulaEvaluator 传递给 formatCellValue()



  FormulaEvaluator eval = book.getCreationHelper()。createFormulaEvaluator(); 
...
rowData.add(format.formatCellValue(cell,eval));


I'm working on importing data from an Excel file into a Java JTable. I can import all the sheet, but I want to import only some rows. This is the code I'm using to import the sheet:

 private void remplirtable(JTable table, int numfeuil,String s) {

        Workbook wb;
        File fichier = new File(s);
        try {
            //création de la table et importation du fichier
                    wb = Workbook.getWorkbook(fichier);
            Sheet sheet = wb.getSheet(numfeuil);
            String[] columnsnames=new String[sheet.getColumns()];
            DefaultTableModel model = new DefaultTableModel();
            //Remplir la table désignée
                        table.setModel(model);
            int colonnes = sheet.getColumns();
            int lignes = sheet.getRows();
            Object data[] = new Object[colonnes];
            for (int i=0; i<lignes; i++){
                for (int j=0; j<colonnes; j++){
                if (i==0)
                    {
                        //Component c=super.prepareRenderer(renderer, j, i);
                        model.addColumn(sheet.getCell(j,i).getContents());

                    }
                    //System.out.println(sheet.getCell(j,i).getContents());
                    if(i>=1)
                        data[j]=sheet.getCell(j,i).getContents();

                }model.addRow(data);
            }model.removeRow(0);
        } catch (BiffException | IOException e) {
        }

    }

In order to import only the rows (3,4,5), I used this code. A is the array in which we stock the rows numbers:

private void RemplirPostes(JTable table, int numfeuil,String s,int[] A) {
        Workbook wb;
        File fichier = new File(s);
        try {
            //création de la table et importation du fichier
                    wb = Workbook.getWorkbook(fichier);
            Sheet sheet = wb.getSheet(numfeuil);
            DefaultTableModel model = new DefaultTableModel();
            //Remplir la table désignée
                        table.setModel(model);
            int colonnes = sheet.getColumns();
                Object data[] = new Object[colonnes];

                        for(int k=0;k<A.length;k++)
                            {
                              for (int j=0; j<colonnes; j++){
                              if (A[k]==0)
                    {
                        //Component c=super.prepareRenderer(renderer, j, i);
                model.addColumn(sheet.getCell(j,A[k]).getContents());

                    }
                    //System.out.println(sheet.getCell(j,i).getContents());
                    if(A[k]>=1)

                        data[j]=sheet.getCell(j,A[k]).getContents();

                }model.addRow(data);
                                }
            model.removeRow(0); 

                } catch (BiffException | IOException e) {
        }

    }

And all this doesn't work.

解决方案

Starting from this complete example, the following code creates a table having the indicated number of COLUMNS from MIN_ROW to MAX_ROW, inclusive.

private static final int COLUMNS = 4;
private static final int MIN_ROW = 3;
private static final int MAX_ROW = 5;
…
DataFormatter format = new DataFormatter();
DefaultTableModel model = new DefaultTableModel(0, COLUMNS);
for (Row row : sheet) {
    if (row.getRowNum() >= MIN_ROW && row.getRowNum() <= MAX_ROW) {
        Vector rowData = new Vector();
        for (Cell cell : row) {
            rowData.add(format.formatCellValue(cell));
        }
        model.addRow(rowData);
    }
}
…
JFrame f = new JFrame("TableTest");
f.add(new JTable(model));

Addendum: Looking closer at your current approach, you may want to accommodate non-contiguous rows. It may help to replace your array with a List<Integer>. You can then use contains() in place of the predicate above:

if (a.contains(row.getRowNum()) {…}

Addendum: To evaluate formulae, you can pass a FormulaEvaluator to formatCellValue().

FormulaEvaluator eval = book.getCreationHelper().createFormulaEvaluator();
…
rowData.add(format.formatCellValue(cell, eval));

这篇关于如何在Java中的JTable中填充一些Excel行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆