vector_ever
Mitglied
Hallo
ich versuchte Java code, um excel datei zu lesen, und so weit alles ok.
jetzt was ich möchte, dass die Zweite Spalte in Arraylist speichern um in Datenbank wie list[0], list[1] auf zurufen.
Mein Problem, ich weiß nicht genau wie ich genau die zweite Spalte bestimmen (wie kann man konkrete Spalte auswählen).
Außerdem möchte ich dazu die gespeicherte Daten in ArrayList in schleife passen und anzeigen
aber ich bekomme immer ein fehler
Die vollständige Code:
was soll ich jetzt tun?
ich versuchte Java code, um excel datei zu lesen, und so weit alles ok.
jetzt was ich möchte, dass die Zweite Spalte in Arraylist speichern um in Datenbank wie list[0], list[1] auf zurufen.
Mein Problem, ich weiß nicht genau wie ich genau die zweite Spalte bestimmen (wie kann man konkrete Spalte auswählen).
Außerdem möchte ich dazu die gespeicherte Daten in ArrayList in schleife passen und anzeigen
Code:
for (int i = 0; i < col.size(); i++){
String item = (String) col.get(i);
System.out.println("coloum " + i + " : " + item);
}
aber ich bekomme immer ein fehler
Code:
Exception in thread "main" java.lang.ClassCastException: org.apache.poi.hssf.usermodel.HSSFRichTextString cannot be cast to java.lang.String
at Reader.main(Reader.java:97)
Die vollständige Code:
Code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
public class Reader {
public static void main(String[] args) {
ArrayList col = new ArrayList();
try {
FileInputStream file = new FileInputStream(new File("d:\\hi.xls"));
//Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(file);
//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
//display from the third row until 5th
if(row.getRowNum()>2 && (row.getRowNum()<5)){
{
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
//Getting the cell contents
Cell cell = cellIterator.next();
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_FORMULA:
System.out.println(cell.getCellFormula());
break;
/** case Cell.CELL_TYPE_BLANK:
System.out.println("BLANK");
break;
**/
}
//add the values of the cell to the Arraylist
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
{
System.out.print(cell.getNumericCellValue());
col.add(cell.getNumericCellValue());
}
else if (cell.getCellType() == Cell.CELL_TYPE_STRING)
{
System.out.print(cell.getRichStringCellValue());
col.add(cell.getRichStringCellValue());
}
else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN)
{
System.out.print(cell.getBooleanCellValue());
col.add(cell.getBooleanCellValue());
}
}
}
}
System.out.println("");
}
file.close();
//print the value of the cells which is stored in the the Arraylist
System.out.println("");
for (int i = 0; i < col.size(); i++){
String item = (String) col.get(i);
System.out.println("coloum " + i + " : " + item);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}