Springboot集成jxls实现导出excel功能

添加的maven依赖:

十多年的内乡网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销的优势是能够根据用户设备显示端的尺寸不同,自动调整内乡建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联公司从事“内乡网站设计”,“内乡网站推广”以来,每个客户项目都认真落实执行。

创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站建设、成都网站制作、和布克赛尔蒙古网络推广、成都小程序开发、和布克赛尔蒙古网络营销、和布克赛尔蒙古企业策划、和布克赛尔蒙古品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供和布克赛尔蒙古建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

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循环进行值得填充

文章标题:Springboot集成jxls实现导出excel功能
路径分享:https://www.cdcxhl.com/article44/iijpee.html

成都网站建设公司_创新互联,为您提供软件开发营销型网站建设网页设计公司动态网站关键词优化服务器托管

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都定制网站建设