在处理 Excel 数据时,合并单元格是一个常见的操作,它可以帮助我们更清晰地展示数据,节省空间,并且使表格看起来更加整洁。在使用 Apache POI 库进行 Java 开发时,合并单元格同样是一个实用的功能。下面,我将详细介绍如何在 Java 程序中巧妙地合并 Excel 中 POI 列相同数据,实现单元格的智能合并。
1. 准备工作
在开始之前,请确保你已经将 Apache POI 库添加到你的项目中。以下是添加 POI 库到 Maven 项目的示例:
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
</dependencies>
2. 创建 Excel 文件
首先,我们需要创建一个 Excel 文件,并添加一些数据。以下是一个简单的示例:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelMergeExample {
public static void main(String[] args) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
// 添加数据
Object[][] data = {
{"Name", "Age", "City"},
{"Alice", 25, "New York"},
{"Bob", 30, "Los Angeles"},
{"Alice", 22, "Chicago"},
{"Bob", 28, "New York"}
};
int rowNum = 0;
for (Object[] rowData : data) {
Row row = sheet.createRow(rowNum++);
int colNum = 0;
for (Object cellData : rowData) {
Cell cell = row.createCell(colNum++);
if (cellData instanceof String) {
cell.setCellValue((String) cellData);
} else if (cellData instanceof Integer) {
cell.setCellValue((Integer) cellData);
}
}
}
// 合并单元格
mergeCells(sheet, 0, 0, 2, 0);
// 写入文件
try (FileOutputStream outputStream = new FileOutputStream("example.xlsx")) {
workbook.write(outputStream);
}
workbook.close();
}
// 合并单元格的方法
public static void mergeCells(Sheet sheet, int firstRow, int firstCol, int lastRow, int lastCol) {
sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol));
Row row = sheet.getRow(firstRow);
Cell cell = row.getCell(firstCol);
cell.setCellValue("Header");
}
}
在这个例子中,我们创建了一个包含姓名、年龄和城市的 Excel 表格,并合并了第一行的前两列,作为标题。
3. 合并相同列的数据
现在,让我们扩展这个例子,实现合并相同列中相同数据的功能。以下是一个示例:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
public class ExcelMergeExample {
public static void main(String[] args) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
// 添加数据
Object[][] data = {
{"Name", "Age", "City"},
{"Alice", 25, "New York"},
{"Bob", 30, "Los Angeles"},
{"Alice", 22, "Chicago"},
{"Bob", 28, "New York"},
{"Alice", 25, "New York"}
};
int rowNum = 0;
for (Object[] rowData : data) {
Row row = sheet.createRow(rowNum++);
int colNum = 0;
for (Object cellData : rowData) {
Cell cell = row.createCell(colNum++);
if (cellData instanceof String) {
cell.setCellValue((String) cellData);
} else if (cellData instanceof Integer) {
cell.setCellValue((Integer) cellData);
}
}
}
// 合并相同列的数据
mergeCells(sheet, 1, 0, 4, 0);
// 写入文件
try (FileOutputStream outputStream = new FileOutputStream("example.xlsx")) {
workbook.write(outputStream);
}
workbook.close();
}
// 合并相同列的数据的方法
public static void mergeCells(Sheet sheet, int firstRow, int firstCol, int lastRow, int lastCol) {
Map<String, List<Integer>> dataMap = new HashMap<>();
for (int i = firstRow; i <= lastRow; i++) {
Row row = sheet.getRow(i);
if (row != null) {
Cell cell = row.getCell(firstCol);
if (cell != null && cell.getCellType() == CellType.STRING) {
String cellValue = cell.getStringCellValue();
dataMap.computeIfAbsent(cellValue, k -> new ArrayList<>()).add(i);
}
}
}
// 合并单元格
for (Map.Entry<String, List<Integer>> entry : dataMap.entrySet()) {
List<Integer> rows = entry.getValue();
if (rows.size() > 1) {
sheet.addMergedRegion(new CellRangeAddress(rows.get(0), rows.get(rows.size() - 1), firstCol, lastCol));
Row row = sheet.getRow(rows.get(0));
Cell cell = row.getCell(firstCol);
cell.setCellValue(entry.getKey());
}
}
}
}
在这个例子中,我们合并了“Name”列中相同的数据。首先,我们创建了一个 Map 来存储每个唯一值及其对应的行号。然后,我们遍历这个 Map,合并具有相同值的单元格。
4. 总结
通过以上示例,我们了解了如何在 Java 程序中使用 Apache POI 库合并 Excel 中的数据。合并相同列的数据可以帮助我们更好地组织和展示数据,提高工作效率。希望这个例子能对你有所帮助!
