您好,登錄后才能下訂單哦!
添加的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循環進行值得填充
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。