引言
要求实现对课程数据进行 Excel 表格下载。生成课程时有三种不同的阶段,所有有三种 Excel 表格下载。使用到 XSSFWorkbook 解决。表格主要包含三部分,第一行为课程名称等信息;第二行为课程单元信息的表头;第三行及之后为主要的课程单元信息,包括单元名称、单元描述等等。
正文
- 依赖引入
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
- 请求体设置
主要有需要两个参数,课程 id 以及 课程类型。这里使用到了枚举,列出三种要下载的表格类型 - service 层具体实现
1)进行请求参数不为空校验
2)通过课程 id 获取课程信息
主要通过编写 sql 进行多表查询这里不详细展开
3)设置表格样式
课程单元信息部分列要求背景色在四个颜色中循环
XSSFWorkbook workbook = new XSSFWorkbook();
// 设置字体
XSSFFont font = workbook.createFont();
font.setFontName("Arial"); // 设置字体为Arial
font.setFontHeightInPoints((short) 10); // 设置字体大小为10
// 创建一个单元格样式并启用换行
XSSFCellStyle autoHeightStyle = workbook.createCellStyle();
// 启用自动换行
autoHeightStyle.setWrapText(true);
// 自动换行均需要顶端对齐
autoHeightStyle.setVerticalAlignment(VerticalAlignment.TOP);
autoHeightStyle.setFont(font);
// 设置垂直居中
XSSFCellStyle verticalCenterStyle = workbook.createCellStyle();
verticalCenterStyle.setWrapText(true); // 均需自动换行
verticalCenterStyle.setVerticalAlignment(VerticalAlignment.CENTER);
verticalCenterStyle.setFont(font);
// 设置字体加粗
XSSFFont boldFont = workbook.createFont();
boldFont.setBold(true);
boldFont.setFontName("Arial");
boldFont.setFontHeightInPoints((short) 10);
int rowIndex = 0;
XSSFSheet sheet;
// 表头背景色,将#FAFAFA转换为RGB
XSSFColor color = new XSSFColor(new Color(239, 239, 239), null);
XSSFColor colorOne = new XSSFColor(new Color(239, 249, 231), null);
XSSFColor colorTwo = new XSSFColor(new Color(222, 241, 245), null);
XSSFColor colorThree = new XSSFColor(new Color(244, 244, 237), null);
XSSFColor colorFour = new XSSFColor(new Color(231, 230, 251), null);
// 创建单元格样式并设置背景色
XSSFCellStyle tableHeadStyle = workbook.createCellStyle();
tableHeadStyle.setFillForegroundColor(color);
tableHeadStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// 该背景色为表头,字体加粗,水平居中,垂直居中
tableHeadStyle.setFont(boldFont);
tableHeadStyle.setVerticalAlignment(VerticalAlignment.CENTER);
tableHeadStyle.setAlignment(HorizontalAlignment.CENTER);
XSSFCellStyle styleOne = workbook.createCellStyle();
styleOne.setFillForegroundColor(colorOne);
styleOne.setFillPattern(FillPatternType.SOLID_FOREGROUND);
styleOne.setFont(boldFont); // 有背景色的加粗
styleOne.setVerticalAlignment(VerticalAlignment.CENTER); // 有背景色的垂直居中
styleOne.setAlignment(HorizontalAlignment.CENTER); // 有背景色水平居中
styleOne.setWrapText(true);
XSSFCellStyle styleTwo = workbook.createCellStyle();
styleTwo.setFillForegroundColor(colorTwo);
styleTwo.setFillPattern(FillPatternType.SOLID_FOREGROUND);
styleTwo.setFont(boldFont);
styleTwo.setVerticalAlignment(VerticalAlignment.CENTER);
styleTwo.setAlignment(HorizontalAlignment.CENTER);
styleTwo.setWrapText(true);
XSSFCellStyle styleThree = workbook.createCellStyle();
styleThree.setFillForegroundColor(colorThree);
styleThree.setFillPattern(FillPatternType.SOLID_FOREGROUND);
styleThree.setFont(boldFont);
styleThree.setVerticalAlignment(VerticalAlignment.CENTER);
styleThree.setAlignment(HorizontalAlignment.CENTER);
styleThree.setWrapText(true);
XSSFCellStyle styleFour = workbook.createCellStyle();
styleFour.setFillForegroundColor(colorFour);
styleFour.setFillPattern(FillPatternType.SOLID_FOREGROUND);
styleFour.setFont(boldFont);
styleFour.setVerticalAlignment(VerticalAlignment.CENTER);
styleFour.setAlignment(HorizontalAlignment.CENTER);
styleFour.setWrapText(true);
List<XSSFCellStyle> colorList = new ArrayList<>();
colorList.add(0, styleOne);
colorList.add(1, styleTwo);
colorList.add(2, styleThree);
colorList.add(3, styleFour);
4)根据数据依次设置每行数据
// 创建sheet
sheet = workbook.createSheet("Pacing Guide");
// 设置前两行固定
sheet.createFreezePane(0, 2);
// 设置列宽
sheet.setColumnWidth(0, 6000); // 第1列,宽度为6000
sheet.setColumnWidth(1, 8000); // 第2列,宽度为15000
.
.
.
//合并第一行第一二列单元格
sheet.addMergedRegion(new CellRangeAddress(0,0,0,1));
根据需要给对应单元格设置样式即可,这类给出一些示例
XSSFRow unitRow = sheet.createRow(rowIndex);
// 设置行高
unitRow.setHeightInPoints(100);
// 设置每个单元格内容
XSSFCell unitTitleCell = unitRow.createCell(0);
unitTitleCell.setCellValue("Unit "+ unit.getNumber() + ":" +unit.getTitle());
// 设置单元格样式
unitTitleCell.setCellStyle(colorList.get(unit.getNumber()-1 % colorList.size()));
XSSFCell descriptionCell = unitRow.createCell(1);
descriptionCell.setCellValue(unit.getDescription());
// 设置文本自动换行
descriptionCell.setCellStyle(autoHeightStyle);
XSSFCell weekCell = unitRow.createCell(2);
weekCell.setCellValue(unit.getWeeks() != null? unit.getWeeks() : 0);
// 设置背景色
weekCell.setCellStyle(colorList.get(unit.getNumber()-1 % colorList.size()));
- dao 层具体实现
为对课程表、课程单元表、单元活动表三个表的联表查询,不具体展开。
收获
学会了一种下载 Excel 的方法,加强了对复杂 SQL 语句的编写能力,了解了企业开发的代码规范重要性。