jxls结合spring mvc生成excel

jxls结合spring mvc生成excel

Apache jxls是生成excle的模板引擎,结合spring mvc生成excel

public class JxlsExcelView extends AbstractView {


    private String exportFileName;
    private String templateName;
    private static final String CONTENT_TYPE = "application/vnd.ms-excel";
    private static final String TPL_PATH = "template/";

    public JxlsExcelView(String exportFileName, String templateName) {
        if (exportFileName != null) {
            try {
                exportFileName = URLEncoder.encode(exportFileName, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        this.exportFileName = exportFileName;
        this.templateName = templateName;
    }

    @Override
    protected void renderMergedOutputModel(Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) throws Exception {

        Context context = new Context(map);
        response.setContentType(CONTENT_TYPE);
        response.setHeader("content-disposition", "attachment;filename=" + exportFileName + ".xls");
        InputStream is = getClass().getClassLoader().getResourceAsStream(TPL_PATH+templateName);
        JxlsHelper jxlsHelper = JxlsHelper.getInstance();
        ServletOutputStream os = response.getOutputStream();
        Transformer transformer = jxlsHelper.createTransformer(is, os);
        JexlExpressionEvaluator expressionEvaluator = (JexlExpressionEvaluator) transformer.getTransformationConfig().getExpressionEvaluator();
        Map<String, Object> func = new HashMap<>();
        func.put("utils", new JxlsUtils());
        expressionEvaluator.getJexlEngine().setFunctions(func);
        jxlsHelper.processTemplate(context, transformer);
        is.close();
    }
}

JxlsUtils工具类:

public class JxlsUtils {


    static{
        //合并单元格,自定义实现
        XlsCommentAreaBuilder.addCommandMapping("merge", MergeCommand.class);
    }

    public String dateFmt(Date date, String pattern) {
        if (date == null) {
            return "";
        }

        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}

controller调用:

@RequestMapping("/download")
public ModelAndView download(HttpServletRequest request ,HttpServletResponse response) throws IOException {
   try {
       Map<String, Object> model = new HashMap<>();
       model.put("data", result);
       return new ModelAndView(new JxlsExcelView("xxxx报表", "download_template.xlsx"), model);
   } catch (Throwable e) {
       LOGGER.error("导出报表失败: ", e);
   }
   return null;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值