当前位置:网站首页>Excel导入导出Controller
Excel导入导出Controller
2022-07-22 07:38:00 【ᅟᅟᅟᅟᅟ】
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
/**
* @author
* @version 1.0
* @description:
* @date
*/
@RestController
@RequestMapping("excel")
public class ExcelController {
@GetMapping()
public void excel(HttpServletResponse response) throws FileNotFoundException {
FileInputStream fis = new FileInputStream("D:\\test.xlsx");
try {
List<List<Object>> lists = importExcel(null, fis);
exportExcel(lists, response);
} catch (IOException e) {
e.printStackTrace();
}
}
public static List<List<Object>> importExcel(MultipartFile file, InputStream in) throws IOException {
ExcelReader excelReader = ExcelUtil.getReader(in);
return excelReader.read();
}
public static void exportExcel(List<List<Object>> list, HttpServletResponse response) throws IOException {
ExcelWriter writer = ExcelUtil.getWriter();
List<Object> rowHead = list.get(0);
//rowHead.add(4, "herd");
writer.writeHeadRow(rowHead);
writer.write(list);
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("test-" + DateUtil.today() + ".xls", "utf-8"));
ServletOutputStream out = response.getOutputStream();
writer.flush(out, true);
writer.close();
IoUtil.close(out);
}
public static String rule(String cvsString) {
String one = cvsString.substring(0, 10);
String two = cvsString.substring(13, 16);
String three = cvsString.substring(16);
int i1 = Integer.parseInt(three);
if (i1 > 99) {
return "数据异常";
}
int i;
if (i1 > 9) {
i = i1;
} else {
i = i1 * 10;
}
return one + "132" + two + i + "**";
}
}
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.6.6</version> </dependency>
边栏推荐
- [harmony OS] [ark UI] [demo] loading animation
- Sqlmap is opened in the form of code rather than image
- 【微服务~远程调用】整合RestTemplate、WebClient、Feign
- 网络之物理层
- Family Trivia
- VR全景在各行各业是如何展示?如何落地应用的?
- [HMS core] [Health Kit] [FAQ] collection of data subscription function questions
- [Digital IC] understand Axi protocol in simple terms
- JS to determine whether the linked image exists
- Win11显示麦克风未插上怎么办?Win11显示麦克风未插上的解决方法
猜你喜欢
【HMS core】【FAQ】【Account Kit】典型问题合集1
[how to optimize her] teach you how to locate unreasonable SQL? And optimize her~~~
Branch and loop statements
2022-07-21:给定一个字符串str,和一个正数k, 你可以随意的划分str成多个子串, 目的是找到在某一种划分方案中,有尽可能多的回文子串,长度>=k,并且没有重合。 返回有几个回文子串。 来
备战攻防演练,这里有一张腾讯安全重保布防图!
【FPGA教程案例35】通信案例5——基于FPGA的16QAM调制信号产生,通过matlab测试其星座图
Real time synchronization and conversion of massive data based on Flink CDC
[Digital IC] understand Axi protocol in simple terms
[HMS core] [Health Kit] [FAQ] collection of data subscription function questions
How does win11 run as an administrator? Win11 setup method running as Administrator
随机推荐
[must see for developers] [push kit] collection of typical problems of push service 1
[Digital IC] understand Axi protocol in simple terms
Boss直聘怎么写出优秀的简历?
Pytorch实现Word2Vec
【FPGA教程案例35】通信案例5——基于FPGA的16QAM调制信号产生,通过matlab测试其星座图
Misc advanced
MySQL JDBC编程
(11) 51 Single Chip Microcomputer -- realize the storage of stopwatch data with AT24C02 (attached with achievement display)
How does Oracle set up not to check compilation errors during creation?
subprocess
PostgreSQL database is deployed on Linux server. Ms level is queried locally. Pgadmin installed on windows is used to query super slow for about 20s. Is it a network problem or a database configuratio
How can VR panoramic display attract users' attention in a new way of online promotion?
2022-07-21:给定一个字符串str,和一个正数k, 你可以随意的划分str成多个子串, 目的是找到在某一种划分方案中,有尽可能多的回文子串,长度>=k,并且没有重合。 返回有几个回文子串。 来
How does win11 run as an administrator? Win11 setup method running as Administrator
JS to determine whether the linked image exists
VR全景在各行各业是如何展示?如何落地应用的?
【HMS core】【push kit】关于消息分类问题的集合
Install vscode offline
[network counting] (III) hypernetwork, routing, NAT protocol
MySQL 增删改查(進階)