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.