我创建了一个拉第一行的方法和另一个拉测试数据的方法。我无法使用如何将单元格直接添加到列表中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);
}
}
}