首页 > 代码库 > jxls使用模版导出Excel
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> |