java按照模板导出Excel工具类

准备模板:

java按照模板导出Excel工具类_第1张图片


模板中使用jxls表达式,详细的jxls表达式使用可以到网上查找。一下给出简单的说明

${v.JIGOUID}


jxls的循环方法,使用此方法,根据规定要放在要显示数据的第一列,items进行列表循环取值

使用el表达式取值。

1.项目路径获取

public class PathUtil {


/*
* 获取classpath
*/
public static String getClasspath(){
String path = (String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))+"../../").replaceAll("file:/", "").replaceAll("%20", " ").trim();
if(path.indexOf(":") != 1){
path = File.separator + path;
}
return path;
}

}


2.读取properties文件工具类

/**
 * 文件读取工具类
 *
 */
public class ReadProperties {
//fileName:properties文件名
public static String getValueByKey(String fileName,String key){
String value = "";
Properties prop = new Properties();
if(fileName != null){
ClassLoader classLoader = ReadProperties.class.getClassLoader();// 读取属性文件
InputStream in = classLoader.getResourceAsStream(fileName+".properties");
try {
prop.load(new InputStreamReader(in,"UTF-8"));//中文乱码问题
/// 加载属性列表
            Iterator it = prop.stringPropertyNames().iterator();
            while (it.hasNext()) {
                if (it.next().equals(key)) {
                value = prop.getProperty(key);
                }
            }
            in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return value;
}

3.excel导出工具类


import java.util.HashMap;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jxls.transformer.XLSTransformer;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


public class ExcelExport {

/**

* jar包:按照import导入即可,jar下载地址:http://www.manyjar.com/,也可自行网上下载,都能找到,同时也需要引入poi           *  的相关jar包

* 功能描述:

excel文件导出;根据模板进行导出


* @param beanParams excel导出的数据
* @param response 下载时的浏览器响应
* @throws Exception 
*/
public static void excelExport(Map beanParams,HttpServletResponse response) throws Exception{
//获取请求
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
//获取参数报表唯一标识ID
String reportId = request.getParameter("reportId");
//根据reportId 获取excel.properties文件中的对应模板名称
String tempname = ReadProperties.getValueByKey("excel", reportId);
//取得文件的存储路径,得到静态文件
String temppath = PathUtil.getClasspath()+"uploadFiles//template//templatePath//";
String destPath = PathUtil.getClasspath()+"uploadFiles//template//downloadPath//";
//得到模板绝对路径
String templateFilePath = temppath+tempname;
//得到下载使用的文件名
String destname = tempname;
//得到下载文件绝对路径
String destFilePath = destPath+destname;
//创建结果集resultsMap
Map resultsMap = new HashMap();
//得到结果集
resultsMap.put("results",beanParams);
//创建jxsl对象
XLSTransformer xlsTransformer = new XLSTransformer();
//得到templateFilePath、结果集与destFilePath,并进行模版转换
xlsTransformer.transformXLS(templateFilePath, resultsMap, destFilePath);
//下载文件

FileDownload.fileDownload(response, destFilePath, destname);//方法上一篇已经给出,地址

//https://blog.csdn.net/gaohengtime/article/details/80755105

}
注:本文章仅用于学习交流使用。

}

你可能感兴趣的:(java,excel,学习分享)