一、引入插件
<!-- Apache POI插件 (Feb 14, 2020)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
二、创建一个工作簿
1.绘制Excel(Map<String, List> tableMap) 参数JSON结构
{
"表A": [
{
"TYPE": "VARCHAR",
"FIELDNAME": "TYPE",
"ALIASNAME": "类型"
},
{
"TYPE": "VARCHAR",
"FIELDNAME": "NAME",
"ALIASNAME": "名称"
},
{
"TYPE": "VARCHAR",
"FIELDNAME": "ALIAS",
"ALIASNAME": "别名"
}
]
}
2.实现代码
public static void 绘制Excel(Map<String, List<LayerAndFields>> tableMap) {
XSSFWorkbook workbook = new XSSFWorkbook();
for (Map.Entry<String, List<LayerAndFields>> table : tableMap.entrySet()) {
String sheetName = table.getKey();
Pattern pattern = Pattern.compile("[\\s\\\\/:\\*\\?\\\"<>\\|]");
Matcher matcher = pattern.matcher(sheetName);
XSSFSheet sheet = workbook.createSheet(matcher.replaceAll("&"));
row = sheet.createRow(0);
XSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell = row.createCell(0, CellType.STRING);
cell.setCellValue("序号");
sheet.setColumnWidth(0, (int) ((5 + 0.72) * 256));
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell = row.createCell(1, CellType.STRING);
cell.setCellValue("字段名称");
sheet.setColumnWidth(1, (int) ((25 + 0.72) * 256));
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell = row.createCell(2, CellType.STRING);
cell.setCellValue("字段别名");
sheet.setColumnWidth(2, (int) ((25 + 0.72) * 256));
cell.setCellStyle(style);
int rowNum = 0;
for (LayerAndFields fieldsInRow : table.getValue()) {
row = sheet.createRow(++rowNum);
cell = row.createCell(0, CellType.NUMERIC);
cell.setCellValue(rowNum);
cell = row.createCell(1, CellType.STRING);
cell.setCellValue(fieldsInRow.getFieldName());
cell = row.createCell(2, CellType.STRING);
cell.setCellValue(fieldsInRow.getAliasName());
}
}
try {
FileOutputStream outputStream = new FileOutputStream(new File("src/main/resources/数据清单.xlsx"));
workbook.write(outputStream);
workbook.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
原MySQL数据

最终Excel结果

三、总结
- 注意新旧版本设置格式的区别
XSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell = row.createCell(0, CellType.STRING);
cell.setCellValue("序号");
sheet.setColumnWidth(0, (int) ((5 + 0.72) * 256));
cell.setCellStyle(style);
- 除了Springboot引入poi以外,还要引入poi-ooxml
- 工作表名称不能包含 / \ : * " < > | ,必须去除windows非法字符,否则报异常
java.lang.IllegalArgumentException: Invalid char (/) found at index (2) in sheet name '非法/字符*名称示例"'
如有补充或问题欢迎评论区留言。。。
JJ❥YY