easypoi导出含图片数据


实体类:

@Data
public class DeviceExcel {

    /**
     * 名称
     */
    @Excel(name="名称",width = 20)
    private String name;

    /**
     * 编号
     */
    @Excel(name="编号",width = 20)
    private String code;

    /**
     * 上传图片
     */
    @Excel(name = "上传图片", type = 2 ,height = 30,width = 20 )
    private String picture;

    /**
     * 备注
     */
    @Excel(name="备注",width = 40)
    private String memo;

}

 

 

Excel的源码:

 

导出代码:

public void exportDevice(HttpServletRequest request, HttpServletResponse response) {
        //获取要导出的数据
        List<DeviceExcel> deviceExcelList = constructDeviceExcelList(deviceList);
        //导出数据
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(),DeviceExcel.class,deviceExcelList);
        //下载数据
        ExcelPoiUtil.downLoadExcelXlsx(request,response,workbook,"文件名称");
    }
public static void downLoadExcelXlsx(HttpServletRequest request, HttpServletResponse response, Workbook workbook, String fileName){
        final String userAgent = request.getHeader("USER-AGENT");
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            //清空输出流
            response.reset();
            fileName = encoderName(userAgent, fileName);
            //这里设置一下让浏览器弹出下载提示框,而不是直接在浏览器中打开
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName  + "-" + DateTimeUtil.getCurrDateStr()+ ".xlsx");
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("UTF8");
            workbook.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServerException(ExceptionCode.FILE_FORMAT_ERROR.getExceptionCode(),"文件下载异常");
        }
    }

    private static String encoderName(String userAgent, String fileName) throws IOException{
        if("MSIE".equals(userAgent)){
            //IE浏览器
            fileName = URLEncoder.encode(fileName,"UTF8");
        }else if("Mozilla".equals(userAgent)){
            //google,火狐浏览器
            fileName = new String(fileName.getBytes(), "ISO8859-1");
        }else{
            //其他浏览器
            fileName = URLEncoder.encode(fileName,"UTF8");
        }
        return fileName;
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM