如何将excel中的值分组到HashMap

时间:2013-05-09 09:33:21

标签: java excel hashmap apache-poi

我有以下擅长:

Method Name       Status Code
getLoggedinUserDetails  400
createRequisition   400
excelMDM            400

我可以使用以下代码打印excel中的值:

package com.poc.excelfun;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

public class ReadExcelData {
    public static void main(String[] args) {
        try {

            FileInputStream file = new FileInputStream(new File("attachment_status.xls"));

            //Get the workbook instance for XLS file 
            HSSFWorkbook workbook = new HSSFWorkbook(file);

            //Get first sheet from the workbook
            HSSFSheet sheet = workbook.getSheetAt(0);

            //Iterate through each rows from first sheet
            Iterator<Row> rowIterator = sheet.iterator();
            while(rowIterator.hasNext()) {
                Row row = rowIterator.next();

                //For each row, iterate through each columns
                Iterator<Cell> cellIterator = row.cellIterator();
                while(cellIterator.hasNext()) {

                    Cell cell = cellIterator.next();

                    switch(cell.getCellType()) {
                        case Cell.CELL_TYPE_BOOLEAN:
                             System.out.print(cell.getBooleanCellValue() + "\t\t");
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                           System.out.print((int)cell.getNumericCellValue()  + "\t\t");
                            break;
                        case Cell.CELL_TYPE_STRING:
                             System.out.print(cell.getStringCellValue() + "\t\t");
                            break;
                    }
                }
                System.out.println("");
            }
            file.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

我想在Hashmap

中添加除标题Method Name Status Code和值之外的值Key - > getLoggedinUserDetails and value -> 400

怎么做。

请帮我找到这个。

1 个答案:

答案 0 :(得分:2)

试试这段代码:

Iterator<Cell> cellIterator = row.cellIterator();

String key = null
int value = Integer.MIN_VALUE; // use a default value that will never occur in the sheet

HashMap<String, Integer> map = new HashMap<String, Integer>();

while(cellIterator.hasNext()) 
{
    Cell cell = cellIterator.next();

    switch(cell.getCellType()) 
    {
        case Cell.CELL_TYPE_NUMERIC:
            value = cell.getNumericCellValue(); break;
        case Cell.CELL_TYPE_STRING:
             key = cell.getStringCellValue(); break;
    }

    if(key != null && value != Integer.MIN_VALUE)
    {
        map.put(key, value);
        key = null;
        value = Integer.MIN_VALUE;
    }
}

不是最干净的解决方案,但应该使用数据格式的迭代器。

如果你的电子表格框架允许通过索引查找特定单元格,你可以通过替换while循环来简化它,并且kust直接从单元格中检索值。