I am trying to access excel file in hashmap, but i=5 colNum getting as 8, so output is not getting as expected. Below is the code Reading this excel file with hashmap , once questionType matched i want to store only respective label and value then pass to respective class
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
}
Also i want to check hashmap (superMap) element to check questionType, for that please suggest possible approach.
As the apache-poi doc says here , the methods getLastRowNum() , getRow() , ... are 0 (zero) based, so you have to start your "for" loops from 0, and count "i-1" and so on when you want the "i"th column in the sheet.
For instance your 5th row in the Excel document will be sheet.getRow(5 - 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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.