Easypoi导出(带图)

Easypoi导出(带图)

注意:目的是为了自己写有一个参考
Entity:

//专辑
@ExcelTarget("album")
public class Album implements Serializable {
    @Id
    @Excel(name = "ID")
    private String id;
    @Excel(name = "专辑名")
    private String title;
    @Excel(name = "内容简介")
    private String detail;
    @Excel(name = "作者")
    private String author;
    @Excel(name = "播音")
    private String broadcast;
    @Excel(name = "集数")
    private Integer count;
    @Excel(name = "发布日期",format = "yyyy-MM-dd",width = 20)
    private Date createDate;
    @Excel(name = "专辑图",type = 2,width = 20,height = 20)
    private String cover;
    @Excel(name = "星评数")
    private Integer score;
    @ExcelCollection(name = "专辑包涵")
    private List<Chapter> chapters;

}
//专辑包涵的章节
public class Chapter implements Serializable {
    @Id
    @Excel(name = "ID")
    private String id;
    @ExcelIgnore
    private String albumId;
    @ExcelIgnore
    private String audio;
    @Excel(name = "标题")
    private String title;
    @Excel(name = "大小")
    private String size;
    @Excel(name = "创建日期",format = "yyyy-MM-dd")
    private Date createDate;
    @Excel(name = "时长")
    private String duration;

}

Service

//展示所有专辑
List<Album> showAllAlbum(HttpServletRequest request)
//展示章节
List<Chapter> showAllChapter(String AlbumId);

ServiceImpl

//专辑Impl
public List<Album> showAllAlbum(HttpServletRequest request) {
        List<Album> albums = albumMapper.selectAll();
        for (Album album : albums) {
            String realPath = request.getSession().getServletContext().getRealPath(album.getCover());
            album.setCover(realPath);
            //根据条件查询      本次是通过albumId查询
            Example example = new Example(Chapter.class);
            example.createCriteria().andEqualTo("albumId",album.getId());
            List<Chapter> chapters = chapterMapper.selectByExample(example);
            album.setChapters(chapters);
        }
        return albums;
    }
//章节Impl
 public List<Chapter> showAllChapter(String AlbumId) {
        Example example = new Example(Chapter.class);
        example.createCriteria().andEqualTo("albumId",AlbumId);
        List<Chapter> chapters = chapterMapper.selectByExample(example);
        return chapters;
    }

Controller

@RequestMapping("exit")
    public void exit(HttpServletResponse response, HttpServletRequest request) throws IOException {
        //设置导出的文件名
        String fileName = "专辑表.xls";
        //设置下载的响应头
        response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
        //设置下载的响应类型
        String type = request.getSession().getServletContext().getMimeType("application/vnd.ms-excel");
        response.setContentType(type);
        //查询所有的专辑
        List<Album> albums = albumService.showAllAlbum(request);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), Album.class, albums);
        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        workbook.close();
    }

Easypoi导出(带图)_第1张图片

你可能感兴趣的:(Easypoi导出(带图))