我正在尝试访问hashmap中的excel文件,但是i = 5 colNum得到8,因此输出没有按预期获得。下面是代码使用hashmap读取这个excel文件,一旦questionType匹配,我想只存储相应的标签和值,然后传递给相应的类
public class HashMapObjectTest {
static HSSFSheet sheet;
static HSSFRow row;
private static MissingCellPolicy xRow;
static DataFormatter df = new DataFormatter();
@Test
public void getMapData() throws Exception
{
File src=new File("D:\\Projects\\TestData_peerTest.xls");
FileInputStream fis=new FileInputStream(src);
HSSFWorkbook wb=new HSSFWorkbook(fis);
sheet = wb.getSheetAt(0);
DataFormatter df = new DataFormatter();
Row row;
//String value1 = null;
String value = null;
String keyQuestion;
String label = null;
Map<String,Map<String,String>> superMap = new HashMap <String,Map<String,String>> ();
Map<String,String> childMap=new HashMap<String,String>();
ArrayList <String >data=new ArrayList<String>();
for(int i=1; i<sheet.getLastRowNum();i++ )
{
row=sheet.getRow(i);
int column=0;
int colNum=row.getLastCellNum();
Cell c=row.getCell(column, MissingCellPolicy.RETURN_BLANK_AS_NULL);
//String label=df.formatCellValue(sheet.getRow(i).getCell(1));
if(c==null)
{
//label=df.formatCellValue(sheet.getRow(i).getCell(i+1));
label=df.formatCellValue(sheet.getRow(i).getCell(1));
value=df.formatCellValue(sheet.getRow(i).getCell(2));
data.add(value);
continue;
}
else {
keyQuestion= df.formatCellValue(sheet.getRow(i).getCell(0));
for (int j=1;j<colNum-1;j++)
{
label=df.formatCellValue(sheet.getRow(i).getCell(1));
value=df.formatCellValue(sheet.getRow(i).getCell(j+1));
childMap.put(label, value);
}
superMap.put(keyQuestion, childMap);
}
}
System.out.println(superMap);
/*List keys = new ArrayList(superMap.keySet());
for (int i = 0; i < keys.size(); i++) {
Object obj = keys.get(i);
// do stuff here
}
for (Entry<String, Map<String, String>> entry : superMap.entrySet()) {
String key = entry.getKey();
System.out.println(key);
Map<String, String> childkey = entry.getKey();
System.out.println(value);
//TODO: other cool stuff
}
另外我想检查hashmap(superMap)元素来检查questionType,为此请建议可能的方法。
答案 0 :(得分:1)
正如apache-poi doc说here,方法getLastRowNum(),getRow(),...... 基于0(零),所以你必须从0开始你的“for”循环,并且当你想要表格中的第“i”列时,计算“i-1”等等。
例如,Excel文档中的第5行将是sheet.getRow(5 - 1)。
答案 1 :(得分:1)
You can achieve the above scenario, by keeping the start and end Index of each question type.
Modified Code:
public class HashMapObjectTest {
static HSSFSheet sheet;
static HSSFRow row;
private static MissingCellPolicy xRow;
static DataFormatter df = new DataFormatter();
@Test
public void getMapData() throws Exception
{
File src=new File("D:\\Projects\\TestData_peerTest.xls");
FileInputStream fis=new FileInputStream(src);
HSSFWorkbook wb=new HSSFWorkbook(fis);
sheet = wb.getSheetAt(0);
Map<String,Map<String,String>> superMap = new HashMap <String,Map<String,String>> ();
//Start Index -> Question Type Start Index
int startIndex=1;
int endIndex=-1;
int lastRow=sheet.getLastRowNum();
while(startIndex<=lastRow){
for(int i=startIndex;i<lastRow;i++){
Row row=sheet.getRow(i+1);
Cell c=row.getCell(0, MissingCellPolicy.RETURN_BLANK_AS_NULL);
if(c!=null){
endIndex=i;
break;
}
else if(i==lastRow-1){
endIndex=i+1;
break;
}
}
String keyQuestion=sheet.getRow(startIndex).getCell(0).getStringCellValue();
Map<String,String> childMap=new HashMap<String,String>();
for(int j=startIndex;j<=endIndex;j++){
String label=sheet.getRow(j).getCell(1).getStringCellValue();
String value=sheet.getRow(j).getCell(2).getStringCellValue();
childMap.put(label, value);
}
System.out.println(childMap);
superMap.put(keyQuestion,childMap);
startIndex=endIndex+1;
}
System.out.println(superMap);
}