SpringBoot项目 填充Excel模板 导出 下载
模板

下载后的效果

项目结构

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;
}
}
SpringBoot项目 填充Excel模板 导出 下载的更多相关文章
- 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的路由方案,与原来的方案在使用上差别不 ...
- .Net NPOI 根据excel模板导出excel、直接生成excel
一.根据Excel模板导出excel 1.导入NPOI.dll 2.DAL中添加类ExportExcel.cs using NPOI.SS.UserModel; using System; usin ...
- java实现excel模板导出
一. 准备工作 1. 点击此下载相关开发工具 2. 将poi-3.8.jxls-core-1.0两个jar包放到工程中,并引用 3. 将excel模板runRecord.xls放到RunRecordB ...
- kettle 使用excel模板导出数据
通过excel进行高速开发报表: 建设思路: 1.首先制订相关的execl模板. 2.通过etl工具(kettle)能够高速的 将数据库中的数据按excel模板导出成新的excel就可以. 当中ket ...
- Magicodes.IE之Excel模板导出教材订购表
说明 本教程主要说明如果使用Magicodes.IE.Excel完成教材订购表的Excel模板导出. 要点 本教程使用Magicodes.IE.Excel来完成Excel模板导出 需要通过创建Dto来 ...
- Excel模板导出之动态导出
说明 目前Magicodes.IE已支持Excel模板导出时使用JObject.Dictionary和ExpandoObject来进行动态导出,具体使用请看本篇教程. 本功能的想法.部分实现初步源于a ...
- SpringBoot图文教程10—模板导出|百万数据Excel导出|图片导出「easypoi」
有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1「概念+ ...
- C#实现Excel模板导出和从Excel导入数据
午休时间写了一个Demo关于Excel导入导出的简单练习 1.窗体 2.引用office命名空间 添加引用-程序集-扩展-Microsoft.Office.Interop.Excel 3.封装的Exc ...
- Net 自定义Excel模板导出数据
转载自:http://www.cnblogs.com/jbps/p/3549671.html?utm_source=tuicool&utm_medium=referral 1 using Sy ...
- 6、jeecg 笔记之 自定义excel 模板导出(一)
1.前言 jeecg 中已经自带 excel 的导出导出功能,其所使用的是 easypoi,尽管所导出的 excel 能满足大部分需求, 但总是有需要用到自定义 excel 导出模板,下文所用到的皆是 ...
随机推荐
- NC23501 小A的回文串
题目链接 题目 题目描述 小A非常喜欢回文串,当然我们都知道回文串这种情况是非常特殊的.所以小A只想知道给定的一个字符串的最大回文子串是多少,但是小A对这个结果并不是非常满意.现在小A可以对这个字符串 ...
- 快速傅里叶变换(FFT)和小波分析在信号处理上的应用
1前言 1.1傅里叶变换 函数f(t)为一元连续函数,其傅里叶变换定义为: F(w)的傅里叶逆变换定义为: 其中,i为虚数单位.由欧拉公式: 任意绝对可积的连续函数f(t),都可以用三角函数表示,由于 ...
- 【OpenGL ES】EGL+FBO离屏渲染
1 前言 FBO离屏渲染 中使用 GLSurfaceView 来驱动 Renderer 渲染图片,为了隐藏 GLSurfaceView,将其设置为透明的,并且宽高都设置为1.本文将使用 EGL 代 ...
- 使用html+css+js实现一个仿钉钉打卡按钮
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java设计模式-访问者模式Visitor
介绍 访问者模式(Visitor Pattern),封装一些作用于某种数据结构的各元素的操作,它可以在不改变数据结构的前 提下定义作用于这些元素的新的操作. 主要将数据结构与数据操作分离,解决 数据结 ...
- Vue实现静态列表增删查功能
知识点 1.双向数据绑定 2.自定义指令 3.自定义过滤器 4.v-for循环对象数组 5.ES6操作数组的新方法:forEach some filter findIndex 实现效果 页面源码 &l ...
- 如何在 libevent 中读取超过 4096 字节的数据
如何在 libevent 中读取超过 4096 字节的数据 bufferevent 是 libevent 中相对高层的封装,较 event 使用起来方便很多. 之前有一个需求,需要从服务端读取数据进行 ...
- 文心一言 VS 讯飞星火 VS chatgpt (201)-- 算法导论15.2 6题
六.用go语言,证明:对 n 个元素的表达式进行完全括号化,恰好需要 n-1 对括号. 文心一言,代码正常运行: 在Go语言中,证明对n个元素的表达式进行完全括号化恰好需要n-1对括号,可以通过数学归 ...
- extra用法
做子查询时,有些orm语句满足不了的时候使用 select参数 ## select age,(age > 18) as is_adult from myapp_person; Person.ob ...
- Sourcetree 如何关联自己的gitlab仓库
现在有些企业自己搭建了gitlab服务器,通过sourcetree从企业服务器拉取代码的时候会提示认证失败.今天搞了大半天才搞懂,给我自己做个笔记. 添加账户 托管服务商 选择 GitLab CE 托 ...