poi Excel 合并单元格、设置自定义背景色、对齐、设置边框、字体

HSSFWorkbook wb = new HSSFWorkbook();

//创建一张表
Sheet sheet = wb.createSheet("好家伙表");

//设置列宽
sheet.setColumnWidth(0,3766);
sheet.setColumnWidth(1,3766);
sheet.setColumnWidth(2,3766);
sheet.setColumnWidth(3,3766);
sheet.setColumnWidth(4,3766);
sheet.setColumnWidth(5,3766);
sheet.setColumnWidth(6,3766);
sheet.setColumnWidth(7,3766);
sheet.setColumnWidth(8,3766);
sheet.setColumnWidth(9,3766);
sheet.setColumnWidth(10,3766);
sheet.setColumnWidth(11,3766);
sheet.setColumnWidth(12,3766);

//=================================================================
//创建第一行,起始位0
Row titleRowOne = sheet.createRow(0);
//列头
titleRowOne.createCell(0).setCellValue("好家伙");
//设置样式
HSSFCellStyle style = wb.createCellStyle();
HSSFFont font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 11);//设置字体大小
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//设置字体加粗
style.setFont(font);//设置字体
style.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.index);//设置背景色
wb.getCustomPalette().setColorAtIndex(IndexedColors.LIGHT_YELLOW.index,(byte) 255,(byte) 255,(byte) 0);//改变背景色为自定义颜色---RGB(255,255,0)
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//居左
style.setBorderRight(BorderStyle.THIN);//设置单元格右边框
style.setRightBorderColor(IndexedColors.BLACK.index);//设置右边框颜色
titleRowOne.getCell(0).setCellStyle(style);

//=================================================================
//创建第二行,起始为0
Row titleRowTwo = sheet.createRow(1);

CellRangeAddress regionOne = new CellRangeAddress(1,1,0,5);//合并单元格,4个参数,分别为起始行,结束行,起始列,结束列
sheet.addMergedRegion(regionOne);
HSSFCellStyle style1 = wb.createCellStyle();
HSSFFont font1 = wb.createFont();
font1.setFontName("宋体");
font1.setFontHeightInPoints((short) 11);//设置字体大小
style1.setFont(font1);//设置字体
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);//垂直居中
style1.setAlignment(HSSFCellStyle.VERTICAL_CENTER);//水平居中
style1.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style1.setFillForegroundColor(IndexedColors.CORNFLOWER_BLUE.index);
wb.getCustomPalette().setColorAtIndex(IndexedColors.CORNFLOWER_BLUE.index,(byte) 83,(byte) 141,(byte) 213);
style1.setBorderTop(BorderStyle.THIN);
style1.setTopBorderColor(IndexedColors.BLACK.index);
style1.setBorderBottom(BorderStyle.THIN);
style1.setBottomBorderColor(IndexedColors.BLACK.index);
style1.setBorderRight(BorderStyle.THIN);
style1.setRightBorderColor(IndexedColors.BLACK.index);
style1.getFont(wb).setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
titleRowTwo.createCell(0).setCellValue("XXXX信息1");
titleRowTwo.getCell(0).setCellStyle(style1);
titleRowTwo.createCell(1).setCellStyle(style1);
titleRowTwo.createCell(2).setCellStyle(style1);
titleRowTwo.createCell(3).setCellStyle(style1);
titleRowTwo.createCell(4).setCellStyle(style1);
titleRowTwo.createCell(5).setCellStyle(style1);

CellRangeAddress regionTwo = new CellRangeAddress(1,1,6,11);//合并单元格,4个参数,分别为起始行,结束行,起始列,结束列
sheet.addMergedRegion(regionTwo);
HSSFCellStyle style2 = wb.createCellStyle();
HSSFFont font2 = wb.createFont();
font2.setFontName("宋体");
font2.setFontHeightInPoints((short) 11);//设置字体大小
style2.setFont(font2);//设置字体
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);//垂直居中
style2.setAlignment(HSSFCellStyle.VERTICAL_CENTER);//水平居中
style2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style2.setFillForegroundColor(IndexedColors.GREEN.index);
wb.getCustomPalette().setColorAtIndex(IndexedColors.GREEN.index,(byte) 146,(byte) 208,(byte) 80);
style2.setBorderTop(BorderStyle.THIN);
style2.setTopBorderColor(IndexedColors.BLACK.index);
style2.setBorderBottom(BorderStyle.THIN);
style2.setBottomBorderColor(IndexedColors.BLACK.index);
style2.setBorderRight(BorderStyle.THIN);
style2.setRightBorderColor(IndexedColors.BLACK.index);
style2.getFont(wb).setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
titleRowTwo.createCell(6).setCellValue("XXXX信息2");
titleRowTwo.getCell(6).setCellStyle(style2);
titleRowTwo.createCell(7).setCellStyle(style2);
titleRowTwo.createCell(8).setCellStyle(style2);
titleRowTwo.createCell(9).setCellStyle(style2);
titleRowTwo.createCell(10).setCellStyle(style2);
titleRowTwo.createCell(11).setCellStyle(style2);

CellRangeAddress regionThree = new CellRangeAddress(1,1,12,17);//合并单元格,4个参数,分别为起始行,结束行,起始列,结束列
sheet.addMergedRegion(regionThree);
HSSFCellStyle style3 = wb.createCellStyle();
HSSFFont font3 = wb.createFont();
font3.setFontName("宋体");
font3.setFontHeightInPoints((short) 11);//设置字体大小
style3.setFont(font3);//设置字体
style3.setAlignment(HSSFCellStyle.ALIGN_CENTER);//垂直居中
style3.setAlignment(HSSFCellStyle.VERTICAL_CENTER);//水平居中
style3.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style3.setFillForegroundColor(IndexedColors.ORANGE.index);
wb.getCustomPalette().setColorAtIndex(IndexedColors.ORANGE.index,(byte) 255,(byte) 192,(byte) 0);
style3.setBorderTop(BorderStyle.THIN);
style3.setTopBorderColor(IndexedColors.BLACK.index);
style3.setBorderBottom(BorderStyle.THIN);
style3.setBottomBorderColor(IndexedColors.BLACK.index);
style3.setBorderRight(BorderStyle.THIN);
style3.setRightBorderColor(IndexedColors.BLACK.index);
style3.getFont(wb).setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
titleRowTwo.createCell(12).setCellValue("XXXX信息3");
titleRowTwo.getCell(12).setCellStyle(style3);
titleRowTwo.createCell(13).setCellStyle(style3);
titleRowTwo.createCell(14).setCellStyle(style3);
titleRowTwo.createCell(15).setCellStyle(style3);
titleRowTwo.createCell(16).setCellStyle(style3);
titleRowTwo.createCell(17).setCellStyle(style3);

//=================================================================
//创建第三行,起始为0
Row titleRowThree = sheet.createRow(2);
HSSFCellStyle style4 = wb.createCellStyle();
HSSFFont font4 = wb.createFont();
font4.setFontName("宋体");
font4.setFontHeightInPoints((short) 11);//设置字体大小
style4.setFont(font4);//设置字体
style4.setAlignment(HSSFCellStyle.ALIGN_CENTER);//垂直居中
style4.setAlignment(HSSFCellStyle.VERTICAL_CENTER);//水平居中
style4.setBorderBottom(BorderStyle.THIN);
style4.setBottomBorderColor(IndexedColors.BLACK.index);
style4.setBorderRight(BorderStyle.THIN);
style4.setRightBorderColor(IndexedColors.BLACK.index);
titleRowThree.createCell(0).setCellValue("XXX0");
titleRowThree.getCell(0).setCellStyle(style4);
titleRowThree.createCell(1).setCellValue("XXX1");
titleRowThree.getCell(1).setCellStyle(style4);
titleRowThree.createCell(2).setCellValue("XXX2");
titleRowThree.getCell(2).setCellStyle(style4);
titleRowThree.createCell(3).setCellValue("XXX3");
titleRowThree.getCell(3).setCellStyle(style4);
titleRowThree.createCell(4).setCellValue("XXX4");
titleRowThree.getCell(4).setCellStyle(style4);
titleRowThree.createCell(5).setCellValue("XXX5");
titleRowThree.getCell(5).setCellStyle(style4);
titleRowThree.createCell(6).setCellValue("XXX6");
titleRowThree.getCell(6).setCellStyle(style4);
titleRowThree.createCell(7).setCellValue("XXX7");
titleRowThree.getCell(7).setCellStyle(style4);
titleRowThree.createCell(8).setCellValue("XXX8");
titleRowThree.getCell(8).setCellStyle(style4);
titleRowThree.createCell(9).setCellValue("XXX9");
titleRowThree.getCell(9).setCellStyle(style4);
titleRowThree.createCell(10).setCellValue("XXX10");
titleRowThree.getCell(10).setCellStyle(style4);
titleRowThree.createCell(11).setCellValue("XXX11");
titleRowThree.getCell(11).setCellStyle(style4);
titleRowThree.createCell(12).setCellValue("XXX12");
titleRowThree.getCell(12).setCellStyle(style4);
titleRowThree.createCell(13).setCellValue("XXX13");
titleRowThree.getCell(13).setCellStyle(style4);
titleRowThree.createCell(14).setCellValue("XXX14");
titleRowThree.getCell(14).setCellStyle(style4);
titleRowThree.createCell(15).setCellValue("XXX15");
titleRowThree.getCell(15).setCellStyle(style4);
titleRowThree.createCell(16).setCellValue("XXX16");
titleRowThree.getCell(16).setCellStyle(style4);
titleRowThree.createCell(17).setCellValue("XXX17");
titleRowThree.getCell(17).setCellStyle(style4);

效果图

在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值