利用Springmvc的AbstractXlsxView下载Excel文件

设计一个模型,在针对多中数据类型进行拓展

public abstract  class ExcelView extends AbstractXlsxView {


    public CellStyle cellStyle;
    /**
     * 设置样式
     */
    protected abstract void setStyle(Workbook workbook);

    /**
     * 设置row由子类实现
     */
    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 = DateUtil.getSdfTimes()+".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);
    }
}

拓展的数据类型

public class OrdersExcleView 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("订单ID");
        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);
        header.createCell(5).setCellValue("地址");
        header.getCell(5).setCellStyle(super.cellStyle);

        List<PageData> orderList= (List<PageData>) map.get("orderList");
        int rowCount = 1;
        for (PageData pageData : orderList) {
// String totalprice = pageData.getString("TOTALPRICE"); String totalprice = String.valueOf(pageData.get("TOTALPRICE")); System.out.println(totalprice); Row userRow = sheet.createRow(rowCount++); userRow.createCell(0).setCellValue(pageData.getString("ORDERSN")); userRow.createCell(1).setCellValue(totalprice); userRow.createCell(2).setCellValue(pageData.getString("CONSIGNEE")); userRow.createCell(3).setCellValue(pageData.getString("MOBILE")); userRow.createCell(4).setCellValue(pageData.getString("PROVINCENAME")); userRow.createCell(5).setCellValue(pageData.getString("ADDRESS")); } } }

下面是controller

 @RequestMapping(value = "/download",method = RequestMethod.GET)
    public ModelAndView download() throws Exception {
        PageData pageData = new PageData();
        List<PageData> list = ordersService.listAll(pageData);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("orderList",list);
//        ExcelView excelView = new ExcelView();
        ExcelView view = new OrdersExcleView();
        return new ModelAndView(view, map);
    }

 

转载于:https://www.cnblogs.com/oldzhang1222/p/10103670.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值