POI 单元格垂直居中,相同内容的单元格合并

本文介绍了一种使用Java API处理Excel文件的方法,包括如何创建样式、读取单元格值及合并重复数据行。通过遍历Excel表格中的每一行并对比特定列的内容,如果相邻行的指定列内容相同,则删除重复项;反之则合并该行数据,实现数据的高效整理。
XSSFCellStyle cellStyle = wb.createCellStyle(); 
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 居中
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//垂直


			int currnetRow = 5;//开始查找的行
			for (int p = 5; p < totalRow; p++) {//totalRow 总行数
				XSSFCell currentCell = sheet.getRow(p).getCell(0);
				String current = getStringCellValue(currentCell);
				log.info(" current = "+current);
				XSSFCell nextCell = null;
				String next = "";
				if(p < totalRow+1){
					XSSFRow nowRow = sheet.getRow(p+1);
					if(nowRow != null){
						nextCell = nowRow.getCell(0);
						next = getStringCellValue(nextCell);
					}else{
						next = "";
					}
					
				}else{
					next = "";
				}
				log.info(" next = "+next);
				if(current.equals(next)){//比对是否相同
					currentCell.setCellValue("");
					continue;
				}
				else{
					sheet.addMergedRegion(new CellRangeAddress(currnetRow, p, 0, 0));//合并单元格
					XSSFCell nowCell = sheet.getRow(currnetRow).getCell(0);
					nowCell.setCellValue(current);
					nowCell.setCellStyle(cellStyle);
					currnetRow = p + 1;
				}
			}
private String getStringCellValue(XSSFCell cell) {
		String strCell = "";
		if (cell != null) {
			switch (cell.getCellType()) {
			case XSSFCell.CELL_TYPE_STRING:
				strCell = cell.getStringCellValue();
				break;
			case XSSFCell.CELL_TYPE_NUMERIC:
				strCell = String.valueOf(cell.getNumericCellValue());
				break;
			case XSSFCell.CELL_TYPE_BOOLEAN:
				strCell = String.valueOf(cell.getBooleanCellValue());
				break;
			case XSSFCell.CELL_TYPE_BLANK:
				strCell = "";
				break;
			default:
				strCell = "";
				break;
			}
			if (strCell.equals("") || strCell == null) {
				return "";
			}
			if (cell == null) {
				return "";
			}
		}
		return strCell;
    }



### 使用 Apache POI 合并 Excel 单元格时实现内容居中 当使用 Apache POI 库来操作 Excel 文件时,可以通过设置单元格样式的属性来确保合并后的单元格内文字能够正确居中显示。 创建一个新的 `XSSFWorkbook` 或者 `HSSFWorkbook` 对象用于表示整个工作簿。对于每一个需要应用特殊式的工作表(即 Sheet),先获取对应的 `Sheet` 实例对象。为了定义一个区域范围内的多个单元格作为单个合并单元,可以调用 `addMergedRegion()` 方法,传入指定行列坐标的 `CellRangeAddress` 参数[^1]。 为了让合并后的单元格内部的内容保持居中对齐方式,在设置了合并区域之后还需要进一步配置该区域内所有涉及的单元格样式: ```java // 创建新的工作薄实例 (这里以 XSSFWorkbook 为例) Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Example"); // 定义要被合并单元格地址区间 int firstRow = 0; int lastRow = 0; int firstCol = 0; int lastCol = 5; // 将上述区间的单元格合并成一块大单元格 sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol)); // 设置单元格 CellStyle style = workbook.createCellStyle(); // 居中对齐 style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); // 获取第一个单元格设定其值以及应用之前定义好的样式 Row row = sheet.getRow(firstRow); // 如果不存在则会自动新建一行 if(row == null){ row = sheet.createRow(firstRow); } Cell cell = row.getCell(firstCol); // 如果不存在则会自动新建设备 if(cell == null){ cell = row.createCell(firstCol); } cell.setCellValue("这是居中的文本"); cell.setCellStyle(style); ``` 通过以上代码片段可以看出,除了基本的合并操作外,还额外指定了横向和纵向两个方向上的居中对齐模式给目标单元格关联的样式对象 `style` 。这一步骤非常重要,因为默认情况下即使进行了合并处理也不一定会让其中的文字自然地处于中心位置[^2]。 最后不要忘记保存文件到磁盘或者其他输出流当中去完成最终的结果输出过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值