在hashmap中读取excel文件时for循环出错 [英] Error in for loop with while reading excel file in hashmap

查看:22
本文介绍了在hashmap中读取excel文件时for循环出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问 hashmap 中的 excel 文件,但 i=5 colNum 变为 8,因此输出未达到预期.下面是使用 hashmap 读取这个 excel 文件的代码,一旦 questionType 匹配,我只想存储各自的标签和值,然后传递给各自的类

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
    }

我还想检查 hashmap (superMap) 元素来检查 questionType,为此请提出可能的方法.

Also i want to check hashmap (superMap) element to check questionType, for that please suggest possible approach.

推荐答案

您可以通过保留每个问题类型的开始和结束索引来实现上述场景.

You can achieve the above scenario, by keeping the start and end Index of each question type.

修改后的代码:

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);

    }

这篇关于在hashmap中读取excel文件时for循环出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆