Spring boot集成jxls实现导出excel功能

发布时间:2020-06-17 20:05:59 作者:nineteens
来源:网络 阅读:1548

  添加的maven依赖:

  org.jxls

  jxls-poi

  ${jxsl.poi.version}

  org.jxls

  jxls

  ${jxls.version}

  org.jxls

  jxls-reader

  2.0.3

  导出模板类:

  package com.ismartgo.uqcode.common.utils;

  import java.io.IOException;

  import java.io.InputStream;

  import java.io.OutputStream;

  import java.util.Map;

  import org.apache.commons.logging.Log;

  import org.apache.commons.logging.LogFactory;

  import org.jxls.common.Context;

  import org.jxls.util.JxlsHelper;

  /**

  * JXSL excel模板工具方法

  *

  * JXSL detail please see http://jxls.sourceforge.net/

  * User: Foy Lian

  * Date: 2017-07-04

  * Time: 17:44

  */

  public class JxlsTemplate {

  /**

  * 模板路径

  */

  private static final String TEMPLATE_DIR = "/excel";

  protected static Log logger = LogFactory.getLog(JxlsTemplate.class);

  /**

  * 使用JxlsTemplate.class.getResourceAsStream load 模板

  *

  * @param template 模板名称,相当于TEMPLATE_DIR设置的路径

  * @param out 生成excel写入的输出流

  * @param params 交给jxls处理模板需要的参数

  * @throws IOException

  */

  public static void processTemplate(String template, OutputStream out, Map params) throws IOException {

  processTemplate(JxlsTemplate.class, template, out, params);

  }

  /**

  * 使用resourceBaseClassgetResourceAsStream load 模板

  *

  * @param resourceBaseClass class load的类

  * @param template 模板名称

  * @param out 生成excel写入的输出流

  * @param params 交给jxls处理模板需要的参数

  * @throws IOException

  */

  public static void processTemplate(Class resourceBaseClass, String template, OutputStream out, Map params) throws IOException {

  InputStream in = resourceBaseClass.getResourceAsStream(TEMPLATE_DIR + template);

  if (null == in) {

  logger.error("can't find excel template by path:" + TEMPLATE_DIR + template);

  throw new TemplateNotFoundException("找不到excel模板!,位置:" + TEMPLATE_DIR + template);

  }

  processTemplate(in, out, params);

  }

  /**

  * @param templateStream excel模板流

  * @param out 生成excel写入的输出流

  * @param context jxsl上下文

  * @throws IOException

  */

  private static void processTemplate(InputStream templateStream, OutputStream out, Context context) throws IOException {

  JxlsHelper.getInstance().processTemplate(templateStream, out, context);

  }

  /**

  * @param templateStream excel模板流

  * @param out 生成excel写入的输出流

  * @param params 交给jxls处理模板需要的参数

  * @throws IOException无锡×××医院 https://yyk.familydoctor.com.cn/20612/

  */

  public static void processTemplate(InputStream templateStream, OutputStream out, Map params) throws IOException {

  Context context = new Context();

  if (params != null) {

  for (String key : params.keySet()) {

  context.putVar(key, params.get(key));

  }

  }

  processTemplate(templateStream, out, context);

  }

  }

  Controller类导出接口方法:

  @RequestMapping(value="/export", method = RequestMethod.GET)

  public void export(HttpServletResponse response,HttpServletRequest req) {

  AuthUser user = (AuthUser) req.getSession().getAttribute("loginUser");

  UqcProduct product = new UqcProduct();

  //product.setSysTenantCode(user.getSysTenantCode());

  product.setSysTenantCode("1");

  ServletOutputStream out = null;

  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

  List list = productService.queryList(product);

  Map params = new HashMap();

  params.put("items", list);

  try {

  response.setHeader("Expires", "0");

  response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");

  response.setHeader("Content-Disposition", "attachment; filename=\"product.xls\"");

  response.setHeader("Pragma", "public");

  response.setContentType("application/x-excel;charset=UTF-8");

  out = response.getOutputStream();

  JxlsTemplate.processTemplate("/productList_export.xls", out, params);

  out.flush();

  out.close();

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  Excel模板:

  Excel模板添加的批注:上图ID批注:zsy:jx:area(lastCell="N2")

  ${item.id}的批注:zsy:jx:each(items="items" var="item" lastCell="N2")

  模板识别到Controller传入的items值进行for循环进行值得填充


推荐阅读:
  1. Spring Boot和Spring Cloud的联系
  2. 什么是Spring Boot

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring boot jxls ce

上一篇:基于python的大数据分析-数据处理(代码实战)

下一篇:eclipse安装activiti插件和基本使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》