Ich versuche, eine Tabelle basierend auf einem Schlüsselwort zu filtern. Ich möchte dann die Zeilen in einem mehrdimensionalen String-Array (Zellenwerte und Zeilen) speichern. Ich kann nicht scheinen, dass jede Zelle ein separates Element ist. Alle Zellen in allen Arrays werden zu einem Array hinzugefügt:So speichern Sie HSSF-Zeilen als String Java POI
Hier ist der Code:
////Adding each filtered row to an ArrayList
FileInputStream fis = new FileInputStream(new File("myfile.xls"));
HSSFWorkbook workBook = new HSSFWorkbook (fis);
HSSFSheet sheet = workBook.getSheetAt (0);
List<HSSFRow> filteredRows = new ArrayList<HSSFRow>();
ArrayList<List<HSSFRow>> TwoD = new ArrayList<List<HSSFRow>>();
Iterator<Row> rows= sheet.rowIterator();
while (rows.hasNext()){
HSSFRow row = (HSSFRow) rows.next();
Iterator<Cell> cells = row.cellIterator();
while (cells.hasNext()){
HSSFCell cell = (HSSFCell) cells.next();
if (cell.toString().contains("GHH")) {
filteredRows.add(row);
}
}
}
/////// Now iterating through the filteredRows arrayList and turn it into multidimensional String array
ArrayList<String> in =new ArrayList<String>();
ArrayList<List<String>> out = new ArrayList<List<String>>();
for (HSSFRow n:filteredRows){
Iterator<Cell> cells = n.cellIterator();
while (cells.hasNext()){
HSSFCell cell = (HSSFCell) cells.next();
in.add(cell.toString());
}
out.add(in);
}
workBook.close();
Sollten Sie die 'in'-Variable, die in Ihrer pro-for-for-loop-Schleife erstellt werden soll, nicht verschieben? – Gagravarr
Aha. Ja. Fügen Sie als Antwort hinzu –