最初の行をプルするメソッドと、テスト データをプルする別のメソッドを作成しました。を使用してセルをリストに直接追加できません。list.add()
それらを文字列に変換してリストに追加する方法
public class read {
HSSFCell cell;
HSSFRow row;
HSSFWorkbook wbk;
HSSFSheet sheet;
List<HSSFCell> cellTempList;
Hashtable<String, String>[] data = null;
Iterator CIterator;
List<List<HSSFCell>> list;
HSSFFormulaEvaluator HFE = new HSSFFormulaEvaluator(wbk);
@SuppressWarnings("unchecked")
public String[] readFirstRow() throws IOException{
InputStream ipstream = new FileInputStream(new File("C:\readfile.xls"));
wbk = new HSSFWorkbook(ipstream);
sheet = wbk.getSheetAt(0);
row = sheet.getRow(0);
int numberofCells = row.getPhysicalNumberOfCells();
Object result;
String[] firstRow = new String[numberofCells];
for(int i = 0; i<= numberofCells; i++){
CIterator = row.cellIterator();
while(CIterator.hasNext()){
CIterator.next();
HSSFCell cellFirstRow = row.getCell(i);
switch(cellFirstRow.getCellType()){
case Cell.CELL_TYPE_BOOLEAN:
result = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
result = cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_STRING:
result = cell.getStringCellValue();
break;
}
for(int j=0; j<=numberofCells; j++){
firstRow[i] = result.toString();
}
}
return firstRow;
}
public void getNextRow(){
Object obj;
list = new ArrayList<List<HSSFCell>>();
cellTempList = new ArrayList<HSSFCell>();
HSSFRow rowNext = sheet.getRow(1);
Iterator rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
rowIterator.next();
CIterator = rowNext.cellIterator();
for(int j = 0; j<= rowNext.getPhysicalNumberOfCells();j++){
while (CIterator.hasNext()) {
CIterator.next();
//HSSFCell cell = rowNext.getCell(numberofCells);
cellTempList.add(cell);
}
list.add(cellTempList);
}
}
}