<!-- easypoi的jar -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
//专辑类
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@TableName("album")
@ExcelTarget(value = "album")
public class Album extends Model<Album> {
private static final long serialVersionUID = 1L;
@ExcelIgnore
private String id;
@Excel(name = "标题",needMerge = true,width = 15)
private String title;
@Excel(name = "得分",needMerge = true)
private String score;
@ExcelIgnore
private String detail;
@ExcelIgnore
private String broadcast;
@Excel(name = "封面",type = 2,needMerge = true,width = 20 , height = 20,imageType = 1)
private String cover;
@Excel(name = "作者",needMerge = true)
private String author;
@Excel(name = "集数",needMerge = true)
private Integer count;
@Excel(name = "创建日期",needMerge = true,format = "yyyy年MM月dd日",width = 15)
@TableField("create_date")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date createDate;
@TableField(exist = false)
@ExcelCollection(name = "专辑详情")
private List<Chapter> chapters;
@Override
protected Serializable pkVal() {
return this.id;
}
}
//章节类
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@TableName("chapter")
public class Chapter extends Model<Chapter> {
private static final long serialVersionUID = 1L;
@Excel(name = "编号",width = 33)
private String id;
@Excel(name = "名称")
private String title;
@ExcelIgnore
private String audio;
@TableField("album_id")
private String albumId;
@Excel(name = "大小(MB)")
private Double size;
@Excel(name = "创建日期",format = "yyyy年MM月dd日",width = 15)
@TableField("create_date")
@JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss",timezone = "GMT+8")
private Date createDate;
@Excel(name = "时长")
private String duration;
@Override
protected Serializable pkVal() {
return this.id;
}
}
<a class="btn btn-success" href="${ctx}/album/download" >导出所有专辑信息</a>
//文件导出
@RequestMapping("download")
public void download(HttpServletResponse response) throws IOException {
//先查出所有专辑
List<Album> albums = albumService.selectList(null);
for (Album album : albums) {
//根据专辑查出专辑下所有音频
Map<String, Object> map = new HashMap<>();
map.put("album_id",album.getId());
List<Chapter> chapters = chapterService.selectByMap(map);
//获取封面 重新存入 可以是绝对路径也可以是相对路径
String cover = album.getCover();
String path = "F:\\Java Project\\code3\\cmfz_ze\\src\\main\\webapp\\view\\album\\image\\"+cover;
album.setCover(path);
//获取集数 重新存入
album.setCount(chapters.size());
album.setChapters(chapters);
}
//设置响应头信息 以附件的形式下载
response.setHeader("content-disposition","attachment;fileName="+ URLEncoder.encode("专辑详情.xls","UTF-8"));
ServletOutputStream outputStream = response.getOutputStream();
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("所有专辑","音频"),Album.class, albums);
workbook.write(outputStream);
workbook.close();
}
![](https://i-blog.csdnimg.cn/blog_migrate/fb55af6d78247eb5fc75f6dd4988ddc0.png)