Spring MVC AbstractXlsxView下载excl,Controller层注解坑,导出报表Excel,前端接收。

首先创建基础模板,后面模板在基础模板的基础上创建

package com.hjc.cms.service.fw;



import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.servlet.view.document.AbstractXlsxView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Map;

/**
 * 基础模板
 * @Author: Song
 * @Date: 2021/6/25 15:09
 */
public abstract class ExcelView extends AbstractXlsxView {
    public CellStyle cellStyle;

    /**
     * 设置样式
     * @param workbook
     */
    protected abstract void setStyle(Workbook workbook);

    /**
     * 设置row由子类实现
     * @param sheet
     * @param map
     */
    protected abstract void setRowStyle(Sheet sheet, Map<String, Object> map);

    @Override
    protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //自定义文件名称
        String excelName= "新建报表.xlsx";
        String Agent=request.getHeader("User-Agent");
        if (null!=Agent){
            Agent=Agent.toLowerCase();
            //针对火狐乱码的处理
            if (Agent.indexOf("firebox")!=-1){
                response.setHeader("content-disposition", String.format("attachment;filename*=utf-8'zh_cn'%s", URLEncoder.encode(excelName, "utf-8")));
            }else {
                response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(excelName, "utf-8"));
            }
        }
        response.setContentType("application/ms-excel; charset=UTF-8");
        Sheet sheet=workbook.createSheet("User Detail");
        sheet.setDefaultColumnWidth(30);
        this.setStyle(workbook);
        setRowStyle(sheet,model);
    }
}

在基础模板的基础创建属于拓展模板

package com.hjc.cms.service.fw;

import com.hjc.cms.bean.pojo.TOrder;
import lombok.ToString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.List;
import java.util.Map;

/**
 * @Author: Song
 * @Date: 2021/6/25 15:35
 */
public class OrdersExcelView extends ExcelView{
    @Override
    protected void setStyle(Workbook workbook) {

    }

    @Override
    protected void setRowStyle(Sheet sheet, Map<String, Object> map) {
        //创建行的头部
        Row header = sheet.createRow(0);
        header.createCell(0).setCellValue("订单号");
        header.getCell(0).setCellStyle(super.cellStyle);
        header.createCell(1).setCellValue("停车场");
        header.getCell(1).setCellStyle(super.cellStyle);
        header.createCell(2).setCellValue("金额(元)");
        header.getCell(2).setCellStyle(super.cellStyle);
        header.createCell(3).setCellValue("订单状态");
        header.getCell(3).setCellStyle(super.cellStyle);
        header.createCell(4).setCellValue("创建时间");
        header.getCell(4).setCellStyle(super.cellStyle);

        List<TOrder> orderList= (List<TOrder>) map.get("orderList");
        int rowCount=1;
        for (TOrder pageData : orderList){
            //String totalprice = String.valueOf(pageData.get("TOTALPRICE"));
            logger.info(pageData.toString());
            Row userRow = sheet.createRow(rowCount++);
            userRow.createCell(0).setCellValue(pageData.getOutTradeNo());
            userRow.createCell(1).setCellValue(pageData.getTPark().getParkName());
            userRow.createCell(2).setCellValue(pageData.getTotalFee());
            userRow.createCell(3).setCellValue(pageData.getPayStatus());
            userRow.createCell(4).setCellValue(pageData.getTimeStamp());
        }
    }
}

开始创建Controller,注意注解@RequestMapping。

//导出综合报表excel
    @RequestMapping("/download")
    public ModelAndView download(String sCarportNum, String sRoomNum, Boolean bAll, Boolean  bSame, Integer iOrder, String sQryEndDate){
        List<TOrder> list=orderService.jointQuery(order,startTimeStamp,outTimeStamp,selectPark());
        HashMap<String, Object> map = new HashMap<>();
        map.put("orderList",list);
        ExcelView view=new OrdersExcelView();
        return new ModelAndView(view,map);
    }

前端页面可以使用window.open访问。

$scope.exportAll = function () {
        window.open('../online/parkSpaceInfoDownload?bAll='+$scope.reportQuery.bAll+'&bSame='+$scope.reportQuery.bSame+'&iOrder='+$scope.reportQuery.iOrder);
    }

附一张流程图

Spring MVC AbstractXlsxView下载excl,Controller层注解坑,导出报表Excel,前端接收。

上一篇:CF903G Yet Another Maxflow Problem


下一篇:Tp5 Excle带图片导出