POI合并单元格设置单元格样式

设置居中

 CellStyle centerStyle = wb.createCellStyle();
 centerStyle.setAlignment(HorizontalAlignment.CENTER); // 居中
 centerStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中

设置背景颜色

 CellStyle colorStyle = wb.createCellStyle();
 colorStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); //背景颜色
 colorStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//设置实心 不然不会有颜色

设置边框

CellStyle borderStyle = wb.createCellStyle();
borderStyle.setBorderBottom(BorderStyle.THIN); //下边框
borderStyle.setBorderLeft(BorderStyle.THIN);//左边框
borderStyle.setBorderTop(BorderStyle.THIN);//上边框
borderStyle.setBorderRight(BorderStyle.THIN);//右边框

设置字体

 Font font = wb.createFont();
 font.setFontHeightInPoints((short) 10);//字体大小
 font.setColor(IndexedColors.RED.getIndex());//字体颜色
 font.setFontName("楷体");//字体
 CellStyle fontStyle = wb.createCellStyle();
 fontStyle.setFont(font);

合并单元格

第一行合并到第二行 第一列合并到第四列

 //创建一个合并单元格
  CellRangeAddress region = new CellRangeAddress(0, 1, 0, 3);
  sheet.addMergedRegion(region);

实际使用

       public void style(HttpServletResponse response) throws IOException {
           //创建工作簿
           Workbook   wb = new XSSFWorkbook();
           Sheet sheet = wb.createSheet();

           CellStyle centerStyle = wb.createCellStyle();
           centerStyle.setAlignment(HorizontalAlignment.CENTER); // 居中
           centerStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中

           CellStyle colorStyle = wb.createCellStyle();
           colorStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); //背景颜色
           colorStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//设置实心 不然不会有颜色

           CellStyle borderStyle = wb.createCellStyle();
           borderStyle.setBorderBottom(BorderStyle.THIN); //下边框
           borderStyle.setBorderLeft(BorderStyle.THIN);//左边框
           borderStyle.setBorderTop(BorderStyle.THIN);//上边框
           borderStyle.setBorderRight(BorderStyle.THIN);//右边框

           Font font = wb.createFont();
           font.setFontHeightInPoints((short) 10);//字体大小
           font.setColor(IndexedColors.RED.getIndex());//字体颜色
           font.setFontName("楷体");//字体
           CellStyle fontStyle = wb.createCellStyle();
           fontStyle.setFont(font);

           Row regionRow = sheet.createRow(0);

           Cell cell = regionRow.createCell(0);

           cell.setCellValue("合并单元格");
           cell.setCellStyle(centerStyle);

           //创建一个合并单元格
           CellRangeAddress region = new CellRangeAddress(0, 1, 0, 3);
           sheet.addMergedRegion(region);

           Row row = sheet.createRow(2);
           Cell centerCell = row.createCell(0);
           centerCell.setCellValue("居中");
           centerCell.setCellStyle(centerStyle);

           Cell colorCell = row.createCell(1);
           colorCell.setCellValue("背景颜色");
           colorCell.setCellStyle(colorStyle);

           Cell borderCell = row.createCell(2);
           borderCell.setCellValue("边框");
           borderCell.setCellStyle(borderStyle);

           Cell fontCell = row.createCell(3);
           fontCell.setCellValue("字体");
           fontCell.setCellStyle(fontStyle);
           String fileName = "我是文件名";
           //解决文件名中文乱码
           response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes(), "iso-8859-1")+".xlsx");
           //导出到浏览器
           wb.write(response.getOutputStream());
       }

运行效果

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值