السلام عليكم
أحاول أن أكتب كود لاستطيع ان أقرأ ملف اكسل وقد استطعت ذلك ولا مشكلة وذلك بواسطة مكتبة apache poi
الآن ما أود فعله هو أن أقوم باسناد قيم العامود الثاني باكمله hi.xls وتخزينه في Arraylist لاستخدامها في قواعد البيانات
المشكلة التي تواجهني هو كيف أستطيع فعلا أن أحدد أن القيم المراد تخزينها في الـ Arraylist هي الموجودة في العامود الثاني لم استطيع ان اعرف كيف أحدد له ان يخزن فقط قيم العامود الثاني في الـ Arraylist
عدا ذلك أود بعد أن اخزن القيم المطلوبة في الـ Arraylist أن أقوم بعرضها للتأكد وهذا ما لم أنجح به أيضا حيث احصل على رسالة خطأ تقول
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)
برجاء المساعدة واعتقد انه هناك فقط اضافة جد قليلة فقط
الكود كاملاً
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();
}
}
}أرجو المساعدة