jxls使用模版导出Excel
/**
* 使用模版导出Excel
*/
@SuppressWarnings({ "unchecked", "deprecation" })
@Override
public String experExcel(Card card,HttpServletRequest request,HttpServletResponse response){
try {
Map<Object,Object> map=new HashMap<Object, Object>();
StringBuffer where=new StringBuffer();
StringBuffer useSql=new StringBuffer();
Users user=(Users) request.getSession().getAttribute("user");
//需要判断是否查询总帐号
useSql.append(" from UseTbl where userIdFk='"+user.getId()+"' ");
List<UseTbl> result=baseDao.qryInfo(useSql.toString(), null);//查询数据库数据
map.put("experMessageList",result);
map.put("experDate",new Date());
String basePath=request.getRealPath("/");
String temp=user.getName()+"的账单信息-"+user.getId()+".xls";
createExcel(basePath+"files/账单信息.xls", map, basePath+"file/down/",temp);//basePath+"files/账单信息.xls" 模版的路径
//temp = new String(temp.getBytes("utf-8"),"iso-8859-1");
//InputStream is=new FileInputStream(new File(basePath+"upload/experMessageDownload/专家信息.xls"));
return download(request, response, basePath+"file/down/"+temp, temp);
} catch (Exception e) {
e.printStackTrace();
return "error";
}
}
/**
* 用excel模版模版导出excel
*
* @param templeteSrc
* 模版路径(含名称)
* @param params
* 模版参数
* @param newExcelPath
* 生成excel路径
*/
public static void createExcel(String templeteSrc, Map<Object,Object> params,
String newExcelPath,String cre_FileName) {
try {
XLSTransformer transformer = new XLSTransformer();
File file=new File(newExcelPath);
if(!file.exists()){
file.mkdirs();
}
transformer.transformXLS(templeteSrc, params, newExcelPath+cre_FileName);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 下载
*
* @param request
* @param response
* @param urlandfile
* 文件路径+文件名
* @param fileName
* 文件名
* @return success 和 error
* @throws Exception
*/
public static String download(HttpServletRequest request,
HttpServletResponse response, String urlandfile, String fileName)
throws Exception {
String msg = null;
try {
response.setCharacterEncoding("UTF-8");
javax.servlet.ServletOutputStream ou = response.getOutputStream();
// 文件名
// String filename=new
// String(fileName.getBytes("ISO8859_1"),"GB2312").toString();
// 路径
java.io.File file = new java.io.File(urlandfile);
if (!file.exists()) {
System.out.println(file.getAbsolutePath() + " 文件不能存在!");
msg = "unexists";
return msg;
}
// 读取文件流
java.io.FileInputStream fileInputStream = new java.io.FileInputStream(
file);
// 下载文件
// 设置响应头和下载保存的文件名
response.setContentType("application/x-msdownload;charset=UTF-8");// 弹出下载的框
response.setContentLength((int)file.length());// 下载统计文件大小的进度
response.setHeader("Content-Disposition", "attachment; filename="
+ new String(fileName.getBytes(), "ISO-8859-1"));
// 下载框的信息
if (fileInputStream != null) {
// int filelen = fileInputStream.available();
// 文件太大时内存不能一次读出,要循环
byte a[] = new byte[1024];
int n = 0;
while (n != -1) {
n = fileInputStream.read(a);
if (n > 0) {
ou.write(a, 0, n);
}
}
fileInputStream.read(a);
}
fileInputStream.close();
ou.close();
msg = "success";
} catch (Exception e) {
e.printStackTrace();
msg = "error";
}
// 解决完成后使用一切正常,但是总抛出java.lang.IllegalStateException异常主要是流还存在
return msg;
}
=============excel模版(账单信息.xls)============================

| 名称 | 卡号 | 当期金额 | 消费日期 | 资金动向 | 消费类型 |
| <jx:forEach items="${experMessageList}" var="experMessage"> | |||||
| ${experMessage.name} | ${experMessage.cardFk} | ${experMessage.xffs} | ${experMessage.zrzh} | ${experMessage.statsu} | ${experMessage.Xflx} |
| </jx:forEach> | |||||
jxls使用模版导出Excel的更多相关文章
- [poi使用]使用excel模版导出excel
Apache POI是基于Office Open XML标准(OOXML)和Microsoft的OLE 2复合文档格式(OLE2)处理各种文件格式的开源项目.简而言之,您可以使用Java读写MS ...
- 按模版导出Excel
实现效果: excel模版: ExcelHandle.java package com.common.utils; import java.io.File; import java.io.FileIn ...
- POI实现导出Excel和模板导出Excel
一.导出过程 1.用户请求导出 2.先访问数据库,查询需要导出的结果集 3.创建导出的Excel工作簿 4.遍历结果集,写入工作簿 5.将Excel已文件下载的形式回复给请求客户端 二.具体实现(截取 ...
- Java使用POI导出excel(下)——实例与小技巧
[更新]:thinkgem的导出工具类: /** * Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesit ...
- weblogic 12c下jxls导出excel报错Could not initialize class org.apache.poi.xssf.usermodel.XSSFVMLDrawing
周一,开发反馈weblogic 12c下jxls导出excel报错,公司环境和UAT环境均报错,看日志如下: 2016-06-08 09:16:55,825 ERROR org.jxls.util.T ...
- JXLS导出Excel(模板导出)
1.导包 在pom.xml中加入依赖如下: <dependency> <groupId>org.jxls</groupId> <artifactId>j ...
- java使用jxls导出Excel
jxls是基于POI的Excel模板导出导入框架.通过使用类似于jstl的标签,有效较少导出Excel的代码量. 1.pom <!-- https://mvnrepository.com/art ...
- POI3.10 根据Excel模版导出数据测试
1:所需jar包 2:Mysql数据库表内容如下: 3:代码结构如下: (1)User.java public class User { private int id; private String ...
- 导出Excel(导出一个模版)
有时,客户需要一个标准的模板来填东西,然后在导入 这时可以弄好excel模板,供导出 /** * 导出excel模板文件 * @param request * @param response * @r ...
随机推荐
- currentStyle getComputedStyle兼容
function getStyle(obj,attr){ if(obj.currentStyle) {return obj.currentStyle[attr]} else{ return getCo ...
- spring Existing transaction found for transaction marked with propagation 'never' 解决
先在申明事务中配置了所有的事务 <!--配置事物传播策略,以及隔离级别--> <tx:advice id="txAdvice" transaction-manag ...
- xcode6的项目中虚拟键盘无法弹出
这是因为Xcode6中的模拟器键盘设置跟之前的版本不一样了.之前版本是模拟器的键盘和电脑的键盘都可以使用,但是Xcode6的模拟器键盘只能使用一种,即要么是模拟器键盘,要么是电脑键盘.快捷键切换键盘类 ...
- vue项目中常用的一些公共方法
//校验手机号码 export function isSpecialPhone(num) { return /^1[2,3,4,5,7,8]\d{9}$/.test(num) } //校验中英文姓名 ...
- MFC技术积累——基于MFC对话框类的那些事儿5
4. 菜单 4.1 弹出菜单 本节主要讲解如何在主对话框的指定区域内通过鼠标右击来弹出一个菜单选项.最终效果图如图4.1. 如图4.1鼠标只能在指定区域(图中深色区域)内右击时弹出菜单,在指定区域外点 ...
- leetcode_41. First Missing Positive_cyclic swapping
https://leetcode.com/problems/first-missing-positive/ 给定一个长度为len的无序数组nums,找到其第一个丢失的正整数. 解法: 使用cyclic ...
- swfit:运算符重载 Operator Methods
Operator Methods Classes and structures can provide their own implementations of existing operators. ...
- Mathematics-基础:斐波那契数列
f(1)=1 f(2)=1 f(n)=f(n-1)+f(n-2) n>2
- [BZOJ3207]:花神的嘲讽(分块解法)
题目传送门 题目描述:背景花神是神,一大癖好就是嘲讽大J,举例如下:“哎你傻不傻的![hqz:大笨J]”“这道题又被J屎过了!!”“J这程序怎么跑这么快!J要逆袭了!”…… 描述这一天DJ在给吾等众蒟 ...
- js获取农历
上一篇我们对upupoo网页壁纸改造时用到了农历,upupoo(网页壁纸)自主修改一:农历,这里记一下笔记: 获取当前农历的js 主js: //农历 var CalendarData=new Arra ...