本文介绍了如何组这是在Excel中一个HashMap的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Excel的:

i have the following excel:

Method Name       Status Code
getLoggedinUserDetails  400
createRequisition   400
excelMDM            400

和我能够使用以下code,因为它们是打印Excel中的值:

and i am able to print the values as they are in excel using the following code :

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

}

我想提出不包括题目方法名称状态code 的价值观和价值只有像键 - &GT; getLoggedinUserDetails和价值 - &GT; 400的Hashmap

i want to put the values excluding title Method Name Status Code and values only like Key - > getLoggedinUserDetails and value -> 400 in Hashmap

如何做到这一点。

请帮我找到这个。

推荐答案

试试这个code:

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

不干净的解决方案,但应与迭代器为您的数据格式工作。

Not the cleanest solution but should work with the iterator for your data format.

如果你输入S preadsheet框架允许通过更换,而环和kust直接从细胞中检索值查找通过指数特异性细胞可以使其更容易。

If your Spreadsheet framework allows to look up specific cells via index you can make it easier by replacing the while-loop and kust directly retrieve the values from the cells.

这篇关于如何组这是在Excel中一个HashMap的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 14:16