我已经创建了一个拉取第一行的方法和另一个拉取测试数据的方法。我无法使用list.add()
将单元格直接添加到List,如何将它们转换为字符串并添加到列表
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);
}
}
}
答案 0 :(得分:0)
如果您的单元格包含字符串值,则可以使用..
String value = cell.getRichStringCellValue().getString();
..然后将其添加到List<String>
对象