博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring boot集成jxls实现导出excel功能
阅读量:4171 次
发布时间:2019-05-26

本文共 4274 字,大约阅读时间需要 14 分钟。

添加的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 */ 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循环进行值得填充

转载地址:http://xpbai.baihongyu.com/

你可能感兴趣的文章
PHP环境配置:Apach+Tomcat+mysql+php
查看>>
CVE-2019-0708漏洞影响面分析及采用多种规则的检测方法
查看>>
拿走不谢!固件逆向分析过程中的工具和技巧(上)
查看>>
整理网络安全措施的5个小技巧
查看>>
入侵win10(下)--渗透系统
查看>>
烦请解释一下“驱动表”的概念
查看>>
IPAide(IP助手) v1.01
查看>>
Oracle 11g RAC SCAN basics
查看>>
ASM appears to be running, but connect via sqlplus, says idle instance.??
查看>>
Oracle EBS R12 - Steps and Issues/Resolutions during R12.1.1 to R12.1.3 Upgration
查看>>
跳过17:30,跳过瑞星定时扫描
查看>>
自动订饭
查看>>
Dos下命令运行带有包名的Java类
查看>>
Tomcat6数据源配置
查看>>
xmove.pl
查看>>
Excel简单五子棋
查看>>
Java之synchronized小例
查看>>
jstl之set与out小例
查看>>
apploc.bat
查看>>
乱撞解决word只能以安全模式启动
查看>>