当前位置:网站首页>Excel import export controller
Excel import export controller
2022-07-22 18:05: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 " Data exception ";
}
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>
边栏推荐
- 智汇华云 | 集群日志动态采集方案
- [micro Service ~ remote call] integrate resttemplate, webclient, feign
- [HMS core] [FAQ] [account kit] typical problem set 1
- Allegro如何导入高清Logo、二维码、防静电标识等图片以及汉字
- Request message details (request header, get, post, request body)
- 线性回归(公式推导+numpy实现)
- 力扣练习——34 组合总和
- go对接口简单理解
- 栈/堆/队列刷题(上)
- 【论文汇总】2D目标检测文章汇总,持续更新
猜你喜欢
随机推荐
力扣练习——29 按要求补齐数组
异常的理解学习
Using dichotomy to find the elements of an array
Rk3399 platform development series explanation (memory chapter) 15.33. Why does the available memory far exceed the physical memory?
Methods of downloading literature from IEEE
所有navicat版本都支持MySQL吗,为什么我打开连接不上呢?
[HMS core] [FAQ] [account kit] typical problem set 1
牛客刷SQL
Pytorch实现Word2Vec
17、 C function pointer and callback function
Bigder:38/100 a misoperation problem has been solved
网络之数据链路层(PPP协议)
MySQL constraints
JS String charAt substring() substr slice toUpperCase toLowerCase indexOf
【云原生】Docker部署数据库的持久化
线性回归(公式推导+numpy实现)
Vscode failed to install tools
impdp content=data_ Only can you choose to skip or overwrite when there are records?
RK3399平台开发系列讲解(ALSA子系统)4.37、ALSA驱动框架
RK3399平台开发系列讲解(内存篇)15.33、为什么可用内存会远超物理内存?