下载按钮:

<button class="layui-btn layui-btn-sm" data-type="downTemplate">模板下载</button>

对应方法:

downTemplate:function () {
window.open(ctx+"/download/template/customer");
},

java 控制层:


import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest; import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection; @Controller
@RequestMapping("/download")
public class DownloadController { private Logger logger = LoggerFactory.getLogger(getClass()); @RequestMapping(value = "/template/customer")
public ResponseEntity<byte[]> downloadTemp(HttpServletRequest request) throws IOException {
String path = request.getSession().getServletContext().getRealPath("/");
String newFileName = "客户信息模板" + ".xlsx";
String fileName = "customer.xlsx";
File file = FileUtils.getFile(path, "template", fileName);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", new String(
newFileName.getBytes("gbk"), "iso-8859-1"));
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
headers, HttpStatus.OK);
} }

customer.xlsx 是放在webapp/template目录下。

00006-java 下载一个excel模板(文件),前端layui按钮的更多相关文章

  1. 只含有一个Excel模板的工程发布问题

    遇到这样一个问题,某个项目不是dynamic web project,也不是java工程,里面只有一个Excel模板,这样的话,不能打成war包和jar包,不能通过eclipse发布至Tomcat,但 ...

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

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

  3. Java实现一个简单的文件上传案例

    Java实现一个简单的文件上传案例 实现流程: 1.客户端从硬盘读取文件数据到程序中 2.客户端输出流,写出文件到服务端 3.服务端输出流,读取文件数据到服务端中 4.输出流,写出文件数据到服务器硬盘 ...

  4. java实现赋值excel模板,并在新文件中写入数据,并且下载

    /** * 生成excel并下载 */ public void exportExcel(){ File newFile = createNewFile(); //File newFile = new ...

  5. Chimm.Excel —— 使用Java 操作 excel 模板文件生成 excel 文档

    Chimm.Excel -- 设置模板,填充数据,就完事儿了~ _____ _ _ _____ _ / __ \ | (_) | ___| | | | / \/ |__ _ _ __ ___ _ __ ...

  6. java读写Excel模板文件,应用于负载均衡多个服务器

    首先,需要大家明白一点,对于多服务器就不能用导出文件用a标签访问链接方式去导出excel文件了,原因相信大家也明白,可能也做过尝试. 现在开始第一步:get请求,productPath 为你的项目路径 ...

  7. java poi导入EXCEL xls文件代码

    /** * */ package com.bn.car.common.report.excel; import java.io.FileInputStream; import java.io.IOEx ...

  8. java后台读取excel模板数据

    /** * 读取EXCEL模板数据 * * @param excelFilePath excel文件路径 * @param dataRowNum 开始读取数据的行数 * @param keyRowNu ...

  9. C#_服务器EXCEL模板文件导出

    A-1:EXCEL模板导出 非常简单,将EXCEL模板上传到项目中后,将其浏览URL保存下来(excelUrl),然后: window.location.href="http://local ...

随机推荐

  1. 用scrapy实现模拟登陆

    class Test1sSpider(scrapy.Spider): name = 'test1s' allowed_domains = ['yaozh.com'] start_urls = ['ht ...

  2. ARP/RARP报文格式

    arp协议 地址解析协议ARP(Address Resolution Protocol)是用来将IP地址解析为MAC地址的协议. arp格式: 一个字节等于8位 硬件类型:指明发送方想知道的硬件接口类 ...

  3. Ubuntu syslog 太多的 named[1195]: error (network unreachable) resolving './DNSKEY/IN': 2001:7fd::1#53

    Edit file /etc/default/bind9: # run resolvconf? RESOLVCONF=yes # startup options for the server OPTI ...

  4. mysql 之 清空表中数据

    清空表的时候注意外键约束 命令版 查询数据库中所有表名select table_name from information_schema.tables where table_schema='DB_n ...

  5. bibernate中inverse和cascade用法

    一口一口吃掉Hibernate(八)--Hibernate中inverse的用法 [转自 http://blog.csdn.net/xiaoxian8023 ] 一.Inverse是hibernate ...

  6. bzoj4318 OSU!和bzoj 3450 Tyvj1952 Easy

    这俩题太像了 bzoj 3450 Tyvj1952 Easy Description 某一天WJMZBMR在打osu~~~但是他太弱逼了,有些地方完全靠运气:( 我们来简化一下这个游戏的规则 有n次点 ...

  7. SaltStack漏洞导致的挖矿排查思路

    描述 SaltStack是一套C/S架构的运维工具,服务端口默认为4505/4506,两个端口如果对外网开放危害非常大,黑客利用SaltStack的远程命令执行漏洞CVE-2020-11651可以直接 ...

  8. Shell脚本(二)数学运算

    直接上代码. #!/bin/bash no1= no2= echo "using let ..." let result=no1+no2 echo "result is: ...

  9. Datasource Server returns invalid timezone问题

    今天在学习一个项目的时候,idea中的datasource没有配置,后来发现mysql没有连接,于是下载了最新版的jdbc.jar包,连接mysql完成后,想test一下mysql connect,结 ...

  10. Scrapy - Request 中的回调函数callback不执行

    回调函数callback不执行 大概率是被过滤了 两种方法: 在 allowed_domains 中加入目标url 在 scrapy.Request() 函数中将参数 dont_filter=True ...