模板

下载后的效果

项目结构

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>net.sf.jxls</groupId>
<artifactId>jxls-core</artifactId>
<version>1.0.3</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

TestController

package com.example.demo;

import net.sf.jxls.transformer.XLSTransformer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*; @RestController
@RequestMapping("/excel")
public class TestController { @RequestMapping("/export")
private void export(HttpServletRequest request, HttpServletResponse response) throws Exception { //region 数据源
List<Model> resultList = new ArrayList<>(); Model model = new Model();
model.setCode("01");
model.setName("测试1");
resultList.add(model); model = new Model();
model.setCode("02");
model.setName("测试2");
resultList.add(model);
//endregion //获得模版
File directory = new File("src/main/resources/templates");
String templatePath = directory.getCanonicalPath();
String templateFile = templatePath + "\\template.xls"; //导出文件名
SimpleDateFormat simpl = new SimpleDateFormat("yyyyMMddHHmmss");
String currntTime = simpl.format(new Date());
String fileName = currntTime + "报表.xls"; //将结果放入
Map beans = new HashMap();
List values = new ArrayList(); values.add(resultList);
beans.put("values", values);
beans.put("title", "测试title"); //文件名称统一编码格式
fileName = URLEncoder.encode(fileName, "utf-8"); //生成的导出文件
File destFile = File.createTempFile(fileName, ".xls"); //transformer转到Excel
XLSTransformer transformer = new XLSTransformer(); BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//将数据添加到模版中生成新的文件
transformer.transformXLS(templateFile, beans, destFile.getAbsolutePath());
//将文件输入
InputStream inputStream = new FileInputStream(destFile);
//设置response参数,可以打开下载页面
response.reset();
//设置响应文本格式
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition",
"attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
//将文件输出到页面
ServletOutputStream out = response.getOutputStream();
bis = new BufferedInputStream(inputStream);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
//读取并写入
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//使用完成后关闭流
try {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }

model

package com.example.demo;

public class Model {

    private String code;
private String name; public String getCode() {
return code;
} public void setCode(String code) {
this.code = code;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

Demo下载

SpringBoot项目 填充Excel模板 导出 下载的更多相关文章

  1. ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 try.dot.net 的正确使用姿势 .Net NPOI 根据excel模板导出excel、直接生成excel .Net NPOI 上传excel文件、提交后台获取excel里的数据

    ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案   ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不 ...

  2. .Net NPOI 根据excel模板导出excel、直接生成excel

    一.根据Excel模板导出excel 1.导入NPOI.dll  2.DAL中添加类ExportExcel.cs using NPOI.SS.UserModel; using System; usin ...

  3. java实现excel模板导出

    一. 准备工作 1. 点击此下载相关开发工具 2. 将poi-3.8.jxls-core-1.0两个jar包放到工程中,并引用 3. 将excel模板runRecord.xls放到RunRecordB ...

  4. kettle 使用excel模板导出数据

    通过excel进行高速开发报表: 建设思路: 1.首先制订相关的execl模板. 2.通过etl工具(kettle)能够高速的 将数据库中的数据按excel模板导出成新的excel就可以. 当中ket ...

  5. Magicodes.IE之Excel模板导出教材订购表

    说明 本教程主要说明如果使用Magicodes.IE.Excel完成教材订购表的Excel模板导出. 要点 本教程使用Magicodes.IE.Excel来完成Excel模板导出 需要通过创建Dto来 ...

  6. Excel模板导出之动态导出

    说明 目前Magicodes.IE已支持Excel模板导出时使用JObject.Dictionary和ExpandoObject来进行动态导出,具体使用请看本篇教程. 本功能的想法.部分实现初步源于a ...

  7. SpringBoot图文教程10—模板导出|百万数据Excel导出|图片导出「easypoi」

    有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1「概念+ ...

  8. C#实现Excel模板导出和从Excel导入数据

    午休时间写了一个Demo关于Excel导入导出的简单练习 1.窗体 2.引用office命名空间 添加引用-程序集-扩展-Microsoft.Office.Interop.Excel 3.封装的Exc ...

  9. Net 自定义Excel模板导出数据

    转载自:http://www.cnblogs.com/jbps/p/3549671.html?utm_source=tuicool&utm_medium=referral 1 using Sy ...

  10. 6、jeecg 笔记之 自定义excel 模板导出(一)

    1.前言 jeecg 中已经自带 excel 的导出导出功能,其所使用的是 easypoi,尽管所导出的 excel 能满足大部分需求, 但总是有需要用到自定义 excel 导出模板,下文所用到的皆是 ...

随机推荐

  1. Redis+Lua实现简易的秒杀抢购

    1  商品抢购 主要逻辑是:减库存,记录抢购成功的用户 @RestController public class DemoController { @Resource private StringRe ...

  2. MySQL 幻象行

    当同一个查询在不同的时间产生不同的行集时,就会出现所谓的幻像问题.例如,如果执行了两次SELECT,但是第二次返回了第一次没有返回的行,那么该行就是一个"幻象"行. 假设在表chi ...

  3. Seata的分布式事务实现原理

    Seata分布式事务方案 简介 Seata是阿里开源的分布式事务解决方案中间件,对业务侵入小,在应用中Seata整体事务逻辑基于两阶段提交的模型,核心概念包含三个角色: TM:事务发起者.用来告诉TC ...

  4. 【OpenGL ES】FBO离屏渲染

    1 前言 ​ OpenGL 默认把 framebuffer 当作渲染目的地,它由窗口系统创建并管理.应用程序也可以创建额外非可显示的 framebuffer object(FBO),以区别窗口系统提供 ...

  5. 微信小程序云开发项目-个人待办事项-03【主页】模块开发

    上一篇: 微信小程序云开发项目-个人待办事项-02今日模块开发 https://blog.csdn.net/IndexMan/article/details/124497893 模块开发步骤 本篇介绍 ...

  6. FFmpeg开发笔记(八):ffmpeg解码音频并使用SDL同步音频播放

    若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...

  7. Counter函数

    构造一个空Counter import collections c = collections.Counter() c.update('abcdaab') print(c) # Counter({'a ...

  8. js之以面向对象的形式书写贪吃蛇

    此代码存在一定的小bug,当蛇出边界之后存在一定的小问题 分析贪吃蛇功能需求: 1.食物 (1)每次生成一个,位置随意但不可超出规定范围 (2)每次蛇吃到食物之后,前一个食物消失同时新的食物又生成 ( ...

  9. 第134篇:解决浏览器的CORS跨域问题(CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome-untrusted, https, edge.)

    好家伙,   我继续尝试着将我的飞机大战使用ES6模块化分离开来,出了点问题 1.出现问题: edge,chrome等一系列浏览器,会为了安全,禁止你跨域访问 目录如下: 主程序  index.htm ...

  10. 【Azure Key Vault】客户端获取Key Vault机密信息全部失败问题分析

    问题描述 在应用中获取存储在Azure Key Vault的机密信息,全部失败. 报错日志内容如下: [reactor-http-epoll-4] [reactor.netty.http.client ...