首页 > 解决方案 > 使用java在excel中列出地图数据

问题描述

需要帮忙!我正在尝试编写一个可以从 excel 源返回数据的简单函数。我正在尝试从 excel 文件创建数据映射列表,但我得到的列表具有相同的值。所有的价值都是一样的。

这是我的代码:

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ExcelReaderFinal {
    public static void main(String[] args) throws IOException {
        String path = "C:\\Users\\username\\Downloads\\TestFile.xlsx";
        FileInputStream fis = new FileInputStream(path);
        Workbook workbook = new XSSFWorkbook(fis);
        Sheet sheet = workbook.getSheetAt(0);
        int lastRow = sheet.getLastRowNum()-5;
        int lastColumn = 3;

        Map<String, Object> dataMap = new HashMap<String, Object>();
        ArrayList<Map<String, Object>> dataList = new ArrayList<>();

        for(int j=0; j<=lastRow; j++){


            for(int i=0; i<=lastColumn; i++) {

                Row row = sheet.getRow(4);
                Cell keyCell = row.getCell(i);
                Row val = sheet.getRow(5+j);
                Cell valueCell = val.getCell(i);

                String value = valueCell.getStringCellValue().trim();
                String key = keyCell.getStringCellValue().trim();
                dataMap.put(key, value);

            }
            dataList.add(dataMap);
        }
        System.out.println(dataList);
    }

}

我的实际输出是这样的:

[{Negative Val=0, Account Number=121312C, Positive Val=20,000,000.00, Banko=RCBC}, {Negative Val=0, Account Number=121312C, Positive Val=20,000,000.00, Banko=RCBC}, {Negative Val=0, Account Number=121312C,正值=20,000,000.00,Banko=RCBC}]

但我的预期输出是这样的: 在此处输入图像描述

这是我的excel文件源: 在此处输入图像描述

总之:

  1. 我想以列表格式获取excel文件中的所有数据
  2. 该列表应根据 excel 中的列排列进行排序。

太感谢了!!

标签: javaexceldictionaryarraylist

解决方案


推荐阅读