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 ...
随机推荐
- 46 Simple Python Exercises-Higher order functions and list comprehensions
26. Using the higher order function reduce(), write a function max_in_list() that takes a list of nu ...
- 数据库sql语句limit区别
注意:并非所有的数据库系统都支持 SELECT TOP 语句. MySQL 支持 LIMIT 语句来选取指定的条数数据, Oracle 可以使用 ROWNUM 来选取. SQL Server / MS ...
- luogu 1608 路径统计--最短路计数
https://www.luogu.org/problemnew/show/P1608 题意https://www.cnblogs.com/rmy020718/p/9440588.html相似,建议还 ...
- 5.1 qbxt 一测 T2
求和[问题描述] 组合数 C(n,m)是从 n 个物品中取 m 个的方案数. C(n,m)=(n!)/(m!(n-m)!) 斐波那契数列 F 满足,F[0]=F[1]=1,n≥2 时 F[n]=F[n ...
- 全新Ubentu系统没有make,gcc命令解决办法
一定要记得先update sudo apt-get update 然后输入下述命令即可 sudo apt-get install make sudo apt-get install gcc
- MySQL丨02丨忘记root用户密码怎么办?
软件:Mysql 版本:8.0.13 1. 先暂停mysql的服务,方法是在cmd里输入如下代码: net stop mysql 2. 在安装文件夹下创建一个文件:mysql-ini.txt (我的安 ...
- ThinkPHP foreach标签
$optionvalue = array( 'MSGTYPE_TEXT'=>'文本消息', 'MSGTYPE_EVENT_SCAN'=>'扫描事件', 'MSGTYPE_EVENT_sub ...
- NLog在asp.net core中的应用
Asp.net core中,自带的Log是在当selfhost运行时,在控制台中输出,不便于查阅,如果用一个log架框,把日志持久化,便于查询. NLog是一个免费的日志记录框架,专门为.net平台下 ...
- NBUT 1651 - Red packet (求运气王的钱数)(二分法)
Description New Year is coming! Our big boss Wine93 will distribute some “Red Package”, just like Al ...
- 前端传list,springmvc接收list的方法
handler: function() { var baseCustomerForm = me.getAddBaseCustomerForm().getForm(); var linkStore = ...