Why not centered (CellRangeAddress), with regard to export excel merged cells

First, recently made a excel export demand, stands to reason is very simple, but found written method of Public buddy child does not get merged cells, but not too familiar with his kind of writing, so simple studied under, and discussion, at least thinking 10 million, grew more and more clear .
1, prior to writing simple look, do not know from which copy of the Internet, ha ha. The idea is probably: defines three arrays (derived excel entity field names, header names, setting the width of each column), to obtain these parameters by reflection.
Here Insert Picture Description
2, then look at the specific package ExeclUtils tools, is not reflected in this repeat, focus on that HSSFCellStyle pattern is provided , which has two centering (centered vertically and centered horizontally).

// 设置水平垂直居中方式
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
...
// 中间省略
// 单元格创建、设置内容和样式(真正生效一定是样式被设置到单元格上)
HSSFCell cell = row1.createCell(j);
cell.setCellValue(value[j]);
cell.setCellStyle(style );
// 按理说这2个设置后,合并单元格就可以居中了,但是却遗漏了一个点,就是单元格合并是动态的,需要明确单元格合并数,不然会出现,单元格是合并了,但是单元格是上水平居中。

3, centered merge cells, if the cells are dynamic, so you have to explicitly give CellRangeAddress merge several odd row number.

// 合并单元格,四个参数分别是行号的起止、列号的起止,比如下面这个,列号相同,都是变量k,说明只是单元格合并行号
CellRangeAddress range = new CellRangeAddress(i - temp, i - 1, k, k);
sheet.addMergedRegion(range);

4, after all, is a public class, in order not to affect existing functionality, simply open a branch needs to write out the need to merge cells, methods not too much, but if I package, I will define a comment, and then deal with highly reflective more on import and export excel the necessary requirements.

/**
*  一个定制化合并单元格代码简单示例。1、需求只合并行号。2、什么情况相邻行能合并
*/
@SuppressWarnings({ "deprecation", "unchecked" })
private static <T> void contractExcel(List<T> data, HSSFSheet sheet1, HSSFRow row1, String[] method,
		HSSFCellStyle ALIGN_CENTER) throws NoSuchMethodException, SecurityException, IllegalAccessException,
		IllegalArgumentException, InvocationTargetException {
	List<T> list = (List<T>) data;

	for (int i = 1; i < list.size(); i++) {
		// 此处发现不同的业务合并单元格要求不一样,此处强转成不同的类,按时处理什么情况能合并单元格。
		// 上一个单元格合同号
		String oldContractId = list.get(i - 1).getContractId();
		// 当前单元格合同号
		String newContractId = list.get(i).getContractId();
		int size = list.stream().filter(m -> m.getContractId().equals(oldContractId)).collect(Collectors.toList())
				.size();
		int temp = size;
		row1 = sheet1.createRow(i);
		T t = data.get(i);
		Class<?> clas = t.getClass();
		a: for (int j = 0; j < method.length; j++) {
			Method m = clas.getMethod(method[j]);
			Object value = m.invoke(t);
			HSSFCell cell = row1.createCell(j);
			cell.setCellStyle(ALIGN_CENTER);
			if (value == null) {
				cell.setCellValue("");
			} else if (value instanceof String) {
				cell.setCellValue((String) value);
			} else if (value instanceof Long) {
				cell.setCellValue((Long) value);
			} else if (value instanceof BigDecimal) {
				cell.setCellValue(((BigDecimal) value).doubleValue());
			} else if (value instanceof Integer) {
				cell.setCellValue((Integer) value);
			} else if (value instanceof LocalDateTime) {
				cell.setCellValue(
						((LocalDateTime) value).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
			} else {
				throw new BussnisException(StateCommonCode.COMMON_SYSTEM_ERROR, "系统错误,没有相应实体的数据类型,请添加");
			}
			if (i > 1) {
				if (!oldContractId.equals(newContractId) && temp > 1) {
					for (int l = size; l > 0;) {
						if (size == 1) {
							for (int k = 0; k < 21; k++) {
								cell.setCellStyle(ALIGN_CENTER);
								// 合并单元格,只是行合并
								CellRangeAddress range = new CellRangeAddress(i - temp, i - 1, k, k);
								sheet1.addMergedRegion(range);
							}
						} else {
							size--;
						}
						continue a;
					}
				}
			}
		}
	}
}
Published 10 original articles · won praise 0 · Views 352

Guess you like

Origin blog.csdn.net/weixin_43137113/article/details/105192169