EasyExcel导出

Controller层 
    @ApiOperation(value = "毛坯反馈参数(越南)导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ZzcsVO",value = "查询条件",required = false,dataType = "ZzcsVO",paramType = "query")
    })
    @PostMapping("/exportYN")
    @SzLog(SzLogType.EXPORT)
    public void exportXls(HttpServletRequest request, HttpServletResponse response,@RequestBody ZzcsVO zzcsVO){
        List<MpfkcsynExportVo> resList=iMpfkcsService.exportYN(zzcsVO);//导出的数据
        String fileName ="毛坯反馈参数(越南)";
        writeExcel(request,response,resList, MpfkcsynExportVo.class,fileName);
    }
ServiceImpl
    public static <T> void writeExcel(HttpServletRequest request, HttpServletResponse response, List<T> data, Class tClass, String name){
        try {
            name = java.net.URLEncoder.encode(name, "UTF-8").replaceAll("\\+", "%20");
            String dateStr = String.valueOf(LocalDate.now().getMonthValue())+"-"+LocalDate.now().getDayOfYear();
            name = name+dateStr;
            //防止下载时中文乱码
            name = new String(name.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
            response.setCharacterEncoding("utf-8");
            //添加这个是防止前端拿不到Content-disposition
            response.setHeader("Content-Disposition", "attachment;filename=" + name+".xlsx");
            response.setHeader("Access-Control-Expose-Headers","Content-disposition");
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            WriteFont headWriteFont = new WriteFont();
            headWriteFont.setFontHeightInPoints((short) 12);
            headWriteCellStyle.setWriteFont(headWriteFont);
            //内容样式策略
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();

            contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
            contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
            contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
            contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
            contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
            //设置 自动换行
            contentWriteCellStyle.setWrapped(true);
            // 字体策略
            WriteFont contentWriteFont = new WriteFont();
            // 字体大小
            contentWriteFont.setFontHeightInPoints((short) 12);
            contentWriteCellStyle.setWriteFont(contentWriteFont);
            // 这里需要设置不关闭流
            EasyExcelFactory.write(response.getOutputStream(), tClass).registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle)).autoCloseStream(Boolean.FALSE).sheet("毛坯反馈参数(越南)").doWrite(data);
        } catch (Exception e) {
            //log.error("文件导出失败,错误信息{}",e);
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            try {
                response.getWriter();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

 javaBean

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.annotation.write.style.HeadFontStyle;
import com.alibaba.excel.enums.BooleanEnum;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import com.alibaba.excel.enums.poi.VerticalAlignmentEnum;
import lombok.Data;


@Data
@ColumnWidth(25)
@HeadFontStyle()
@ContentStyle(wrapped = BooleanEnum.TRUE,verticalAlignment = VerticalAlignmentEnum.CENTER,horizontalAlignment= HorizontalAlignmentEnum.LEFT)
public class MpfkcsynExportVo{

    @ExcelProperty("织造单编号")
    private String WTESTNO;
    @ExcelProperty("客户")
    private String CUSTNAME;
    @ExcelProperty("加工工厂")
    private String JGGC;
    @ExcelProperty("布号")
    private String CLOTHNO;
    @ExcelProperty("麻灰结构号")
    private String MHJGH;
    @ExcelProperty("工艺属性")
    private String GYSX;
    @ExcelProperty("坯布名称")
    private String CLOTHNAME;

}

 

posted @ 2023-03-23 14:09  Li_ll  Views(83)  Comments(0Edit  收藏  举报