使用easypoi导出Excel

1.导入依赖

<!--Easy poi -->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.0.1</version>
        </dependency>

跑通例子

@GetMapping("/exportIncomeChart")
    @ApiOperation(value = "导出后台总览收入图表")
    public void exportIncomeChart(@RequestParam(required = false) Integer source, @RequestParam(required = false) String starDate, @RequestParam(required = false) String endDate, HttpServletRequest request, HttpServletResponse response) throws IOException {
    
    
        Date date1 = new Date(Long.parseLong(starDate)*1000);   //对应的就是时间戳对应的Date
        Date date2 = new Date(Long.parseLong(endDate)*1000);   //对应的就是时间戳对应的Date

        Request<ManageIncome> request2 = new Request<>();
        ManageIncome income = new ManageIncome();
        if (StringUtil.isBlank(starDate)||StringUtil.isBlank(endDate)){
    
    
            date2=new Date();
            date1=couOrderService.getPastDate(7, new Date());
        }
        income.setStarDate(date1);
        income.setEndDate(date2);
        income.setSource(source);
        request2.setData(income);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        List<ManageIncomeExport> exports = couOrderService.incomeChart(request2);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("收入明细表","明细"),
                ManageIncomeExport.class, exports);
        flushWorkBook(request,response,workbook,sdf.format(income.getEndDate())+"-"+sdf.format(income.getStarDate())+"收入明细表");
    }

    public static void flushWorkBook(HttpServletRequest request, HttpServletResponse response,
                                     Workbook wb, String excelName) {
    
    
        try {
    
    
            String attachFilename =excelName+ ".xlsx";// 文件名
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/msexcel;charset=utf-8");
            String agent = request.getHeader("USER-AGENT");//
            String encodeFileName = null;
            if (null != agent
                    && (-1 != agent.indexOf("MSIE") || -1 != agent.indexOf("Trident") || -1 != agent
                    .indexOf("Edge"))) {
    
    // ie
                encodeFileName = new String(attachFilename.getBytes("utf-8"), "ISO8859-1");// ISO8859-1
            } else {
    
    
                encodeFileName = java.net.URLEncoder.encode(attachFilename, "UTF8");
            }
            response.setHeader("Content-Disposition", "attachment;filename=\"" + encodeFileName
                    + "\"");
            wb.write(response.getOutputStream());
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            if (wb != null) {
    
    
                try {
    
    
                    wb.close();
                } catch (IOException e) {
    
    
                }
            }
        }
    }

实体类注释



/**
 * <p>
 * 
 * </p>
 *
 * @author tujr
 * @since 2020-08-26
 */
@Data
public class ManageIncomeExport {
    
    

    @ApiModelProperty(value = "线上课程拼团")
    Double onGroupCourse;
    @ApiModelProperty(value = "线下课程拼团")
    Double donGroupCourse;

    @Excel(name = "线上课程", height = 20, width = 30, isImportField = "true_st")
    @ApiModelProperty(value = "线上课程")
    Double onCourse;

    @Excel(name = "线下课程", height = 20, width = 30, isImportField = "true_st")
    @ApiModelProperty(value = "线下课程")
    Double donCourse;

    @ApiModelProperty(value = "拼团收入")
    Double groupCount;

    @Excel(name = "总收入", height = 20, width = 30, isImportField = "true_st")
    @ApiModelProperty(value = "总收入")
    Double count;

    @Excel(name = "来源", height = 20, width = 30, isImportField = "true_st",
            replace = {
    
    "手机端_0","PC端_1","全部_2"})
    @ApiModelProperty(value = "来源0手机端 1PC端 2全部")
    Integer source;

    @Excel(name = "下单时间", height = 20, width = 30, isImportField = "true_st",format="yyyy-MM-dd")
    @ApiModelProperty(value = "时间")
    Date time;

}

Guess you like

Origin blog.csdn.net/weixin_43285931/article/details/108621219