java 如何将excel中的值分组到HashMap
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16458640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to group the values which are in excel to a HashMap
提问by Java Questions
i have the following excel:
我有以下excel:
Method Name Status Code
getLoggedinUserDetails 400
createRequisition 400
excelMDM 400
and i am able to print the values as they are in excel using the following code :
我可以使用以下代码打印 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();
}
}
}
i want to put the values excluding title Method Name Status Code
and values only like Key - > getLoggedinUserDetails and value -> 400
in Hashmap
我想把不包括标题Method Name Status Code
和值的值只Key - > getLoggedinUserDetails and value -> 400
放在 Hashmap 中
how to do this.
这个怎么做。
Please help me to find this.
请帮我找到这个。
回答by Daniel Lerps
Try this 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.
不是最干净的解决方案,但应该与您的数据格式的迭代器一起使用。
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.
如果您的电子表格框架允许通过索引查找特定单元格,您可以通过替换 while 循环和 kust 直接从单元格中检索值来使其更容易。