首页 > 解决方案 > 需要 Java Logic 帮助以读取 Excel 文件并填充到 HashMap

问题描述

我正在使用 apache POI API 从 Java 读取 excel 数据文件,并使用 excel Headers 作为 Key 并指定 Row Data 作为 Map 的 Value 填充 HashMap 集合。所有标头始终存在,但与某些标头对应的数据可能存在也可能不存在。

下面是我的代码逻辑: 首先,我将所有标题填充到一个 ArrayList。然后我遍历指定的 excel 数据行的单元格,并将先前填充的 ArrayList 中的标题值和该行中的数据单元格值作为键值添加到 HashMap。

下面是我的代码:

ArrayList<String> headerList = new ArrayList<String>();
Map<String, String> dataMap = new LinkedHashMap<String, String>();
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

// Create object of XSSFWorkbook to get hold of excel file
FileInputStream fis = new FileInputStream(System.getProperty("user.dir") + "\\Resources\\TestData.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(fis);
try
{
int noOfSheets = workbook.getNumberOfSheets();
for(int i=0; i<noOfSheets; i++)
    {
        
        if(workbook.getSheetName(i).equalsIgnoreCase(workSheet))
        {
            //Get access to sheet
            XSSFSheet sheet = workbook.getSheetAt(i);
            
            //Get access to all rows of sheet
            Iterator<Row> rows = sheet.iterator();
            
            Row headerRow = rows.next();
            Iterator<Cell> headerCells = headerRow.cellIterator();
            while(headerCells.hasNext())
            {
                headerList.add(headerCells.next().getStringCellValue());
            }
            
            // Get access to specific row
            while(rows.hasNext())
            {
                Row dataRow = rows.next();
                
                if(dataRow.getCell(0).getStringCellValue().equalsIgnoreCase(testCase))
                {
                    int j = 0;
                    //Get access to collection of cells of the identified rows
                    Iterator<Cell> dataCells = dataRow.cellIterator();
                    
                    //loop through all the cells of the row and add cell data to arraylist.
                    while(dataCells.hasNext())
                    {
                        Cell dataCell = dataCells.next();
                                                    
                        if(dataCell.getCellType()==CellType.STRING)
                        {   
                            //arrList.add(dataCell.getStringCellValue());
                            dataMap.put(headerList.get(j), dataCell.getStringCellValue());
                        }
                        else if(dataCell.getCellType()==CellType.NUMERIC)
                        {   
                            if(DateUtil.isCellDateFormatted(dataCell))
                            {
                                //arrList.add(df.format(dataCell.getDateCellValue()));
                                dataMap.put(headerList.get(j), df.format(dataCell.getDateCellValue()));
                            }
                            else
                            {
                                //arrList.add(NumberToTextConverter.toText(dataCell.getNumericCellValue()));
                                dataMap.put(headerList.get(j), NumberToTextConverter.toText(dataCell.getNumericCellValue()));
                            }
                        }
                        else if(dataCell.getCellType()==CellType.BOOLEAN)
                        {   
                            //arrList.add(Boolean.toString(dataCell.getBooleanCellValue()));
                            dataMap.put(headerList.get(j), Boolean.toString(dataCell.getBooleanCellValue()));
                        }
                        else
                            dataMap.put(headerList.get(j), null);
                        j++;
                    }
                }
            }
        }
    }

如果任何单元格中都没有数据,那么我不希望在 Map 中添加相应的标题。但是当我遍历数据单元格(dataCells.hasNext())时,迭代器不会为那个空白单元格返回 null,而是完全跳过空白单元格。所以添加了所有标题,但没有添加那些没有数据的单元格,因此 Header-Data 键值不匹配。

示例:如果第 5 列的数据单元格为空白,则第 5 列标题的“值”与数据列 6 的值映射为 HashMap 中的“键值”。我想要的是当第 5 列数据为空白时,应跳过将第 5 列标题添加到地图中。如何解决此逻辑不匹配问题? Excel 文件截图 调试场景截图

标签: javaapache-poi

解决方案


推荐阅读