JAVA如何去重excel表的行数据
时间: 2024-10-15 08:15:37 浏览: 61
在Java中,处理Excel表格的行数据去重通常需要用到Apache POI或JExcelApi这样的库来读取和操作Excel文件。以下是一个使用Apache POI的例子来实现行数据去重:
```java
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Set;
import java.util.stream.Collectors;
public class ExcelDuplicatesRemoval {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream(new File("your_file.xlsx")); // 替换为你Excel的实际路径
Workbook workbook = new XSSFWorkbook(file);
Sheet sheet = workbook.getSheetAt(0); // 假设我们处理的是第一个工作表
Set<String> uniqueData = sheet.stream()
.map(row -> rowToString(row)) // 将行转换成字符串
.collect(Collectors.toSet()); // 集合存储唯一的行数据
workbook.close();
file.close();
System.out.println("Unique rows: " + uniqueData.size());
// 打印去重后的数据...
} catch (IOException e) {
e.printStackTrace();
}
}
private static String rowToString(Row row) {
StringBuilder sb = new StringBuilder();
for (Cell cell : row) {
if (cell.getCellType() == CellType.STRING) {
sb.append(cell.getStringCellValue()).append(",");
}
}
return sb.toString().trim(); // 返回一行数据的字符串表示
}
}
```
在这个例子中,首先读取Excel文件,然后遍历每一行,将其转换为字符串,并使用`HashSet`来存储唯一的数据。最后,打印出去重后的行数。
阅读全文
相关推荐


















