java 使用 apache poi 读取 excel - Hashmap,ArrayList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39794578/
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
Read excel using apache poi - Hashmap,ArrayList
提问by bhargav desai
Hi,
你好,
I am trying to read column values using column headers in excel (using java). As attached in the image, suppose i want to read for header "city", all subsequent cells under the column and display them . How will i do that ?
我正在尝试使用 excel 中的列标题(使用 java)读取列值。如图所示,假设我想读取标题“城市”,列下的所有后续单元格并显示它们。我将如何做到这一点?
Looking at examples over the internet, i found most of the examples iterating using an iterator over the row.
查看 Internet 上的示例,我发现大多数示例都使用迭代器在行上进行迭代。
Suppose i have a hashmap of key as headers of excel and subsequent values stored in ArrayList. Something like
假设我有一个 key 的 hashmap 作为 excel 的标题和存储在 ArrayList 中的后续值。就像是
HashMap<String, ArrayList<String>> hashmap =
new HashMap<String, ArrayList<String>>();
How should i proceed ? Can anyone help me with the code and logic?
我应该如何进行?任何人都可以帮助我解决代码和逻辑问题吗?
Please do not mark this as duplicate, i have tried searching over stackoverflow and internet, but most of them iterate using the row.
请不要将其标记为重复,我曾尝试在 stackoverflow 和 Internet 上进行搜索,但大多数都使用该行进行迭代。
回答by Chetan Jadhav CD
There is no way to directly read all the cells in a column without iterating over the rows as that is the way data is stored in an excel sheet (in rows, not in columns). A workaround could be to transpose the rows and columns like it is shown here, and then iterating over the rows to get your data in the Hashmap
as you indicated in your question.
无法在不遍历行的情况下直接读取列中的所有单元格,因为这是数据存储在 Excel 工作表中的方式(按行,而不是按列)。解决方法可能是像此处显示的那样转置行和列,然后遍历行以获取您Hashmap
在问题中指出的数据。
回答by parthi
First, you will read 1st row after you can get all column values,below i have given how to read first row
首先,您将在获得所有列值后读取第一行,下面我给出了如何读取第一行
public void readFirstRow(String filename)
{
List<String> fieldsArrayList = new ArrayList<String>();
try{
FileInputStream myInput = new FileInputStream(new File(filename));
Workbook workBook = null;
workBook = new HSSFWorkbook(myInput);
Sheet sheet = workBook.getSheetAt(0);
Row firstRow = sheet.getRow(0);
int length = firstRow.getLastCellNum();
Cell cell = null;
for( int i = 0 ; i < length ; i++ )
{
cell = firstRow.getCell(i);
fieldsArrayList.add(cell.toString());
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
Read column Values code:
读取列值代码:
//You just pass the cityPostion and filename
//您只需传递cityPostion和文件名
public void readColumnValues(int cityPosition,String fileName)
{
try
{
FileInputStream myInput = new FileInputStream(fileName);
Workbook workBook = null;
workBook = new HSSFWorkbook(myInput);
Sheet sheet = workBook.getSheetAt(0);
for ( int i = 0 ; i <= sheet.getLastRowNum() ; i++ )
{
Row row = sheet.getRow(i);
if (i > 0) //skip first row
{
Cell cityCell = row.getCell(cityPosition);
String cityNames = cityCell.toString();
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
Might be it will help to you...
可能对你有帮助...