gpt4 book ai didi

java - 如何使用java从e​​xcel中获取重复记录并写入新文件

转载 作者:行者123 更新时间:2023-12-02 01:06:35 25 4
gpt4 key购买 nike

我可以使用下面的代码读取第0列的数据。我需要获取第 0 列中存在的重复值的记录

FileInputStream fis = new FileInputStream(theNewestFile);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet spreadsheet = workbook.getSheetAt(0);
XSSFRow row;
String cellValueMaybeNull;
List<Cell> cells = new ArrayList<Cell>();

for (int rowIndex = 0; rowIndex <= spreadsheet.getLastRowNum(); rowIndex++) {
row = (XSSFRow) spreadsheet.getRow(rowIndex);
if (row != null) {
int colIndex = 0;
Cell cell = row.getCell(colIndex);
if (cell != null) {
// Found column and there is value in the cell.
cellValueMaybeNull = cell.getStringCellValue();
// Do something with the cellValueMaybeNull here ...
System.out.println(cellValueMaybeNull);

}
}
}

最佳答案

声明一个 Map 并捕获所有重复项并稍后打印。如果列表大小大于 1,则为重复情况。

FileInputStream fis = new FileInputStream(theNewestFile);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet spreadsheet = workbook.getSheetAt(0);
XSSFRow row;
String cellValueMaybeNull;
List<Cell> cells = new ArrayList<Cell>();

Map<String,List<String>> duplicateCheck = new HashMap<String,List<String>>();
for (int rowIndex = 0; rowIndex <= spreadsheet.getLastRowNum(); rowIndex++) {
row = (XSSFRow) spreadsheet.getRow(rowIndex);
if (row != null) {
int colIndex = 0;
Cell cell = row.getCell(colIndex);
if (cell != null) {
// Found column and there is value in the cell.
cellValueMaybeNull = cell.getStringCellValue();
// Do something with the cellValueMaybeNull here ...
if(duplicateCheck.get(cellValueMaybeNull) != null) {
duplicateCheck.get(cellValueMaybeNull).add(cellValueMaybeNull);
}
else {
List<String> list = new ArrayList<String>();
list.add(cellValueMaybeNull);
duplicateCheck.put(cellValueMaybeNull, list);
}

}
}
}

for(List<String> duplicateValue : duplicateCheck.values()) {
if(duplicateValue.size() > 1) {
System.out.println("Duplicate values :"+duplicateValue);
}
}

关于java - 如何使用java从e​​xcel中获取重复记录并写入新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57723940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com