/**
* 下载模板
* @param tplName
* @param returnName
* @param response
* @param request
* @throws Exception
*/
@RequestMapping(value = "/downloadTpl")
public void downloadTpl(String tplName,String returnName, HttpServletResponse response,
HttpServletRequest request) throws Exception {
try {
//获取模板存放的路径
String path=request.getSession().getServletContext().getRealPath("/")+"/WEB-INF/ExcelTemplate/";
InputStream is=new FileInputStream(new File(path + tplName));
HSSFWorkbook wb=new HSSFWorkbook(is);
//下载
DownloadUtil dUtil=new DownloadUtil();
ByteArrayOutputStream os=new ByteArrayOutputStream();
wb.write(os);
dUtil.download(os, response, returnName);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
throw new Exception();
} }

(1)其中,需要配置的pom.xml文件

(2)excel文件

(3)downloadutil工具类文件

package com.test.util;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; public class DownloadUtil { /**
* @param filePath 要下载的文件路径
* @param returnName 返回的文件名
* @param response HttpServletResponse
* @param delFlag 是否删除文件
*/
protected void download(String filePath, String returnName, HttpServletResponse response, boolean delFlag){
this.prototypeDownload(new File(filePath), returnName, response, delFlag);
} /**
* @param file 要下载的文件
* @param returnName 返回的文件名
* @param response HttpServletResponse
* @param delFlag 是否删除文件
*/
protected void download(File file,String returnName,HttpServletResponse response,boolean delFlag){
this.prototypeDownload(file, returnName, response, delFlag);
} /**
* @param file 要下载的文件
* @param returnName 返回的文件名
* @param response HttpServletResponse
* @param delFlag 是否删除文件
*/
public void prototypeDownload(File file,String returnName,HttpServletResponse response,boolean delFlag){
// 下载文件
FileInputStream inputStream = null;
ServletOutputStream outputStream = null;
try {
if(!file.exists()) {return;}
response.reset();
//设置响应类型 PDF文件为"application/pdf",WORD文件为:"application/msword", EXCEL文件为:"application/vnd.ms-excel"。
response.setContentType("application/octet-stream;charset=utf-8"); //设置响应的文件名称,并转换成中文编码
//returnName = URLEncoder.encode(returnName,"UTF-8");
returnName = response.encodeURL(new String(returnName.getBytes(),"iso8859-1")); //保存的文件名,必须和页面编码一致,否则乱码 //attachment作为附件下载;inline客户端机器有安装匹配程序,则直接打开;注意改变配置,清除缓存,否则可能不能看到效果
response.addHeader("Content-Disposition", "attachment;filename="+returnName); //将文件读入响应流
inputStream = new FileInputStream(file);
outputStream = response.getOutputStream();
int length = ;
int readLength=;
byte buf[] = new byte[];
readLength = inputStream.read(buf, , length);
while (readLength != -) {
outputStream.write(buf, , readLength);
readLength = inputStream.read(buf, , length);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
//删除原文件 if(delFlag) {
file.delete();
}
}
} /**
* by tony 2013-10-17
* @param byteArrayOutputStream 将文件内容写入ByteArrayOutputStream
* @param response HttpServletResponse 写入response
* @param returnName 返回的文件名
*/
public void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName) throws IOException {
response.setContentType("application/octet-stream;charset=utf-8");
returnName = response.encodeURL(new String(returnName.getBytes(),"iso8859-1")); //保存的文件名,必须和页面编码一致,否则乱码
response.addHeader("Content-Disposition", "attachment;filename=" + returnName);
response.setContentLength(byteArrayOutputStream.size()); ServletOutputStream outputstream = response.getOutputStream(); //取得输出流
byteArrayOutputStream.writeTo(outputstream); //写到输出流
byteArrayOutputStream.close(); //关闭
outputstream.flush(); //刷数据
}
}

(4)jsp页面的调用代码

 //模板下载
$("#downloadFlowOrderTpl").click(function(){
var tplName = "测试资金流水单模板.xls";
var returnName = "资金流水单模板.xls";
window.location.href = "${pageContext.request.contextPath}/uploadCtrl/downloadTpl?tplName="+tplName+"&returnName="+returnName;
});

poi下载excel模板的更多相关文章

  1. java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)

    使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...

  2. 下载excel模板,导入数据时需要用到

    页面代码: <form id="form1" enctype="multipart/form-data"> <div style=" ...

  3. vue Excel导入,下载Excel模板,导出Excel

    vue  Excel导入,下载Excel模板,导出Excel vue  Excel导入,下载Excel模板 <template> <div style="display: ...

  4. poi读取excel模板,填充内容并导出,支持导出2007支持公式自动计算

    /** * 版权所有(C) 2016 * @author www.xiongge.club * @date 2016-12-7 上午10:03:29 */ package xlsx; /** * @C ...

  5. java 下载Excel模板

    前端: JSP: <div id="insertBtn" class="MyCssBtn leftBtn" onclick="download( ...

  6. POI根据EXCEL模板,修改内容导出新EXCEL (只支持HSSF)

    package excelPoiTest; import java.io.File; import java.io.FileInputStream; import java.io.FileOutput ...

  7. poi读取Excel模板并修改模板内容与动态的增加行

    有时候我们可能遇到相当复杂的excel,比如表头的合并等操作,一种简单的方式就是直接代码合并(浪费时间),另一种就是写好模板,动态的向模板中增加行和修改指定单元格数据. 1.一个简单的根据模板shee ...

  8. JAVA POI替换EXCEL模板中自定义标签(XLSX版本)满足替换多个SHEET中自定义标签

    个人说明:为了简单实现导出数据较少的EXCEL(根据自定义书签模板) 一.替换Excel表格标签方法```/** * 替换Excel模板文件内容 * @param map * 需要替换的标签建筑队形式 ...

  9. download下载excel模板的代码

    <%-- 直接在JSP页面中进行文件下载的代码(改 Servlet 或者 JavaBean 的话自己改吧), 支持中文附件名(做了转内码处理). 事实上只要向 out 输出字节就被认为是附件内容 ...

随机推荐

  1. [C#] 改进SqliteHelper, 减少拼接SQL语句

    说明:开始几个是基本的方法,后面稍微封装了下 public class SqliteHelper { //连接字符串 private static readonly string str = Conf ...

  2. 牛客国庆集训派对Day3 B Tree(树形dp + 组合计数)

    传送门:https://www.nowcoder.com/acm/contest/203/B 思路及参考:https://blog.csdn.net/u013534123/article/detail ...

  3. poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)

    Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...

  4. Quartz.Net使用教程

    在项目的开发过程中,难免会遇见后需要后台处理的任务,例如定时发送邮件通知.后台处理耗时的数据处理等,这个时候你就需要Quartz.Net了. Quartz.Net是纯净的,它是一个.Net程序集,是非 ...

  5. Java EE—最轻量级的企业框架?

    确保高效发展进程的建议 很久以前,J2EE,特别是应用程序服务器被认为过于臃肿和"重量级".对于开发人员来说,使用此技术开发应用程序会非常繁琐且令人沮丧.但是,由于 J2EE 框架 ...

  6. 使用maven的profile构建不同环境配置

    基本概念说明(resources.filter和profile): 1.profiles定义了各个环境的变量id 2.filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profil ...

  7. Dockfile 生成docker镜像文件大小的比较

    下面就是我针对docker file同一个文件,按照layer层的个数的多少,分别构建了两个镜像的jenkins-master.两者大小相差300MB. <1> layer层数太多,没有将 ...

  8. 5.cookie每个参数的意义和作用以及工作原理?

    cookie主要参数有: (1)expires 过期时间 (2)path cookie存放路径 (3)domain 域名 同域名下可访问 (4)Set-Cookie: name cookie名称

  9. Hive bucket表

    Hive 桶 对于每一个表(table)或者分区, Hive可以进一步组织成桶,也就是说桶是更为细粒度的数据范围划分.Hive也是 针对某一列进行桶的组织.Hive采用对列值哈希,然后除以桶的个数求余 ...

  10. Hive介绍和安装部署

        搭建环境 部署节点操作系统为CentOS,防火墙和SElinux禁用,创建了一个shiyanlou用户并在系统根目录下创建/app目录,用于存放 Hadoop等组件运行包.因为该目录用于安装h ...