java - 使用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文件中的所有数据
- 该列表应根据 excel 中的列排列进行排序。
太感谢了!!
解决方案
推荐阅读
- svg - 如何在 SVG 中遮盖部分描边路径?
- docker - docker desktop 如何在 Windows 机器上运行 linux 容器
- angular6 - 如何检查 textarea 是否是焦点?
- php - 000webhost 没有从数据库中检索/获取数据,一些按钮和错误消息不起作用,但在 localhost 中它可以工作
- python - 是否有任何用于印地语单词搜索的 NLP 代码?
- angular - 未验证选择选项的 Angular 6 或 2+ 表单验证
- java - 如何在启动时不显示数据库中的数据?
- ssl - 使用letsencrypt SSL证书的Amazon Alexa自定义技能
- c# - 自定义用户控件在另一个项目“ClassLibrary”中被事件触发时不会显示
- dafny - 在涉及序列的 dafny 代码中缺少不变量