Hutool工具类excel导出
1-导入依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.1.0</version>
</dependency>
2-excel导出数据实体类
/**
* @ClassName: ExcelData
* @Description:
* @Author:
* @Date:
**/
@Data
public class ExcelData {
@ApiModelProperty("姓名")
private String personName;
@ApiModelProperty("性别")
private String gender;
@ApiModelProperty("年龄")
private String age;
@ApiModelProperty("身份证号")
private String certificateNumber;
@ApiModelProperty("身高")
private Integer height;
@ApiModelProperty("体重")
private Double weight;
public void setGender(String gender) {
if(gender.equals("0")){
this.gender="男";
}else {
this.gender="女";
}
}
}
@ApiModelProperty注解对应标题名称
3-创建Excel工具类
@Slf4j
public class ExportExcelUtil {
public static void export(List<ExcelData> data, String path, String fileName, ExcelData excelData) throws IOException {
log.info("开始导出");
ExcelWriter writer = ExcelUtil.getWriter();
List<Map> fieldAnnotation = getFieldAnnotation(excelData);
for(Map<String,String> map:fieldAnnotation){
for(String key:map.keySet()){
String value = map.get(key);
writer.addHeaderAlias(key,value);
}
}
//第1列40px宽
//writer.setColumnWidth(0, 40);
//第2列15px 宽
//writer.setColumnWidth(1, 15);
//对齐方式,水平左对齐,垂直中间对齐
writer.getStyleSet().setAlign(HorizontalAlignment.LEFT, VerticalAlignment.CENTER);
//标题样式
CellStyle headCellStyle = writer.getHeadCellStyle();
//设置背景色
headCellStyle.setFillForegroundColor(IndexedColors.WHITE1.getIndex());
//必须设置 否则背景色不生效
headCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//创建标题字体
Font headFont = writer.createFont();
headFont.setFontName("宋体");
//大小
headFont.setFontHeightInPoints((short) 11);
//加粗
headFont.setBold(true);
headCellStyle.setFont(headFont);
writer.setColumnWidth(-1, 30);
writer.setRowHeight(-1,20);
//保存路径
FileOutputStream output = new FileOutputStream(path+fileName+".xls");
// 一次性写出内容
writer.write(data);
//自适应列宽
int columnCount = writer.getColumnCount();
for (int i = 0; i < columnCount; ++i) {
double width = SheetUtil.getColumnWidth(writer.getSheet(), i, false);
if (width != -1.0D) {
width *= 256.0D;
//此处可以适当调整,调整列空白处宽度
width += 220D;
writer.setColumnWidth(i, Math.toIntExact(Math.round(width / 256D)));
}
}
writer.flush(output,true);
// 关闭writer,释放内存
writer.close();
}
public static List<Map> getFieldAnnotation(Object object) {
Field[] fields = object.getClass().getDeclaredFields();
Map<String, String> resultMap = new HashMap();
List<Map> list=new ArrayList<>();
for (Field field : fields) {
// 是否引用ApiModelProperty注解
boolean bool = field.isAnnotationPresent(ApiModelProperty.class);
if (bool) {
resultMap = new HashMap();
String value = field.getAnnotation(ApiModelProperty.class).value();
resultMap.put(field.getName(), value);
list.add(resultMap);
}
}
return list;
}
}
4-测试
public static void main(String[] args) {
Date date=new Date();
SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
String fileName = "测试"+format.format(date);
try{
//路径前端传入
String path="D:\\文档\\导出\\";
ExcelData excel=new ExcelData();
excel.setPersonName("111");
excel.setGender("女");
excel.setHeight(178);
excel.setWeight(158.52);
excel.setCertificateNumber("320814145718185169");
//数据库获取
List<ExcelData> data=new ArrayList<>();
data.add(excel);
excel.setPersonName("222");
excel.setGender("男");
data.add(excel);
ExcelData test=new ExcelData();
ExportExcelUtil.export(data, path,fileName,test);
System.out.println("数据导出成功!");
}catch (Exception e){
e.printStackTrace();
System.out.println("数据导出失败!");
}
}
5-部署
lunix中部署注意事项
查看安装字体
fc-list :lang=zh
没有则安装相对应字体
docker部署注意事项
1-在windows的字体文件复制到lunix中
电脑字体默认在“C:\Windows\Fonts”文件夹下。
2-修改DockerFile配置文件
我个人就是将字体文件放在font文件夹中,跟DockerFile文件同一路径