- Excel模板的创建
- 模板支持的语法
1) 空格分割
2) 三目运算 {{test ? obj:obj2}}
3) n: 表示 这个cell是数值类型 {{n:}}
n 用来处理数字问题,防止上面有小叹号
4) le: 代表长度{{le:()}}在if/else 运用{{le:() > 8 ? obj1 : obj2}}
5) fd: 格式化时间{{fd:(obj;yyyy-MM-dd)}}
n 使用java的时间格式化,进行格式化处理
6) fn: 格式化数字{{fn:(obj;###.00)}}
n 处理小数点问题
7) 单引号表示常量值 '' 比如'1' 那么输出的就是 1
8) fe: 遍历数据,创建row
9) !fe: 遍历数据不创建row
10) $fe: 下移插入,把当前行,下面的行全部下移.size()行,然后插入
11) #fe: 行遍历
n 其他都是列遍历,这个是横向遍历
12) !if: 删除当前列{{!if:(test)}}
13) &NULL& 控制
14) ]] 换行符
n 解决多行遍历的问题,详情看下嘛的遍历标签
模板如下:
2.读取模板
/**
* 获取模板文件名称
*
* @param templatePath
* @return
* @author: wl
* @Createtime: 2017年3月24日 下午2:09:23
*/
public static String getexcelName(String templatePath) {
if (templatePath != null) {
templatePath = templatePath.substring(templatePath.lastIndexOf("/") + 1, templatePath.lastIndexOf("."));
}
return templatePath;
}
/**
* **表一、**表二模板导出excel和html(传参判断是否导出html)
*
* @param list
* 循环填充的数据
* @param titleInfo
* 单个填充的数据
* @param templatePath
* excel模板地址
* @param excelName
* 生成的excel文件名称
* @param isToHtml
* 是否生成html文件
* @return
* @author: wl
* @Createtime: 2017年3月22日 下午4:20:08
*/
public static String exportExcelCommon(List list, List listTwo, TitleInfoEntity titleInfo, String templatePath, String outPutPath, String batchId, boolean isToHtml) {
// 读取模板
TemplateExportParams params = new TemplateExportParams(templatePath);
String excelName = "";
excelName = getexcelName(templatePath, batchId);
// 设置sheet名称
params.setSheetName(excelName);
excelName += batchId;
// 创建存放数据容器
Map<String, Object> map = new HashMap<String, Object>();
// 整理 单个填充的数据 放入容器
if (titleInfo == null) {
titleInfo = new TitleInfoEntity();
}
//将对象转化称JSON字符串,Stirng null转为空字符串
String json = JsonUtil.ObjectToJsonStringNulltoChart(titleInfo);
//json字符串转化称对象
Map title = JsonUtil.jsonParseObject(json, Map.class);
map.putAll(title);
// 整理 循环填充的数据 放入容器
map.put("list", list);
if (listTwo != null)
map.put("listtow", listTwo);
// 创建Workbook
Workbook workbook = ExcelExportUtil.exportExcel(params, map);
// 读取输出文件夹
File savefile = new File(outPutPath);
if (!savefile.exists()) {
savefile.mkdirs();
}
FileOutputStream fos = null;
// 创建excel文件
try {
fos = new FileOutputStream(outPutPath + "/" + excelName + ".xls");
workbook.write(fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
workbook.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 创建html文件
if (isToHtml) {
XLSUtils.toHtml(new File(outPutPath + "/" + excelName + ".xls"), new File(outPutPath + "/" + excelName + ".html"));
}
list = null;
map = null;
listTwo = null;
System.gc();
return excelName + ".html";
}
- Excel的下载
function exportxls(){
//获取event对象
var evt = window.event || arguments.callee.caller.arguments[0];
if (document.all) {
evt.returnValue = false;
} else {
//阻止默认的事件
evt.preventDefault();
};
//获取session中的对象的属性
var id='${sessionScope.admin.userid }'
window.open('URL'+id.toString()+'.xls','newwindow');
}