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());
       }

运行效果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_29917503/article/details/131482423