模板
下载后的效果
项目结构
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下载