java poi 导出excel 单元格样式

发布时间 2023-11-01 09:53:25作者: 唏嘘-
CellStyle cs = wb.createCellStyle();
//设置字体
Font font = wb.createFont();
font.setColor(IndexedColors.BLACK.getIndex());
font.setFontHeightInPoints((short) 12);
font.setBold(true);
//字体样式
cs.setFont(font1);
// 边框
cs.setBorderLeft(BorderStyle.THIN);
cs.setBorderRight(BorderStyle.THIN);
cs.setBorderTop(BorderStyle.THIN);
cs.setBorderBottom(BorderStyle.THIN);
//背景颜色
cs.setFillForegroundColor(IndexedColors.YELLOW1.getIndex());(颜色)
cs.setFillPattern(FillPatternType.SOLID_FOREGROUND);(必须设置填充样式)
// 水平居中
cs.setAlignment(HorizontalAlignment.CENTER);
// 垂直居中
cs.setVerticalAlignment(VerticalAlignment.CENTER);                
//设置列宽 
//第一个参数是列索引,表示要设置宽度的单元格所在的列。
//第二个参数是宽度值,单位为1/256个字符宽度。在上述代码中,设置的宽度值为20 * 256,表示该单元格的宽度为20个字符宽度。
sheet.setColumnWidth(0,(short) (37.5 * 200)); 
//设置行高
titleRow.setHeightInPoints(80);
//合并单元格 起始行, 结束行, 起始列, 结束列
CellRangeAddress region1 = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
sheet.addMergedRegion(region1);
//合并后的单元格设置边框
RegionUtil.setBorderBottom(BorderStyle.THIN, region1, sheet);
RegionUtil.setBorderRight(BorderStyle.THIN, region1, sheet);