Java 在hashmap中读取excel文件时,for循环中出现错误

Java 在hashmap中读取excel文件时,for循环中出现错误,java,apache-poi,Java,Apache Poi,我试图访问hashmap中的excel文件,但I=5 colNum的值为8,所以输出并没有达到预期的值。下面是使用hashmap读取此excel文件的代码,一旦questionType匹配,我只想存储相应的标签和值,然后传递给相应的类 公共类HashMapObjectTest{ 静态HSSF片材; 排成一行的静态; 私有静态丢失单元策略xRow; 静态DataFormatter df=新DataFormatter(); @试验 public void getMapData()引发异常 { 文件

我试图访问hashmap中的excel文件,但I=5 colNum的值为8,所以输出并没有达到预期的值。下面是使用hashmap读取此excel文件的代码,一旦questionType匹配,我只想存储相应的标签和值,然后传递给相应的类

公共类HashMapObjectTest{
静态HSSF片材;
排成一行的静态;
私有静态丢失单元策略xRow;
静态DataFormatter df=新DataFormatter();
@试验
public void getMapData()引发异常
{
文件src=新文件(“D:\\Projects\\TestData\u peerTest.xls”);
FileInputStream fis=新的FileInputStream(src);
HSSF工作手册wb=新的HSSF工作手册(fis);
sheet=wb.getSheetAt(0);
DataFormatter df=新的DataFormatter();
行行;
//字符串值1=null;
字符串值=null;
字符串键问题;
字符串标签=null;
Map superMap=newhashmap();
Map childMap=newhashmap();
ArrayList数据=新的ArrayList();

对于(inti=1;i,正如ApachePOI文档所说,方法。。。 基于0(零),因此必须从0开始“for”循环,并在需要工作表中第i列时计算“i-1”等


例如,Excel文档中的第5行是sheet.getRow(5-1)。

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

修改代码:

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

    }
公共类HashMapObjectTest{
静态HSSF片材;
排成一行的静态;
私有静态丢失单元策略xRow;
静态DataFormatter df=新DataFormatter();
@试验
public void getMapData()引发异常
{
文件src=新文件(“D:\\Projects\\TestData\u peerTest.xls”);
FileInputStream fis=新的FileInputStream(src);
HSSF工作手册wb=新的HSSF工作手册(fis);
sheet=wb.getSheetAt(0);
Map superMap=newhashmap();
//开始索引->问题类型开始索引
int startIndex=1;
int-endIndex=-1;
int lastRow=sheet.getLastRowNum();

当(startIndex@Subburaj-请求endindex初始化,我尝试了,但我只为1设置了循环值,出现了一些问题。请帮助我。我犯了一个小错误。我使用了startIndex,而不是Row=sheet.getRow(startIndex+1)中的i;请在读取滑块行后检查更新的代码,startIndex和endIndex值相同,以便跳过循环。因此,仅读取空白值。我已尝试了上述更改。此外,代码仍处于运行模式,没有控制台输出,因此无法检查output@swapnilbargale:我已拆分if条件并将其设置为if else。我已拆分if条件使用更新后的代码进行测试,它对我来说工作正常,直到i=4,int colNum=row.getLastCellNum();返回3,这是正确的,但当我从5开始时,colNum从excel文件中读取的数据是错误的
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);

    }