springmvc上传下载和excel导入导出
1.easypoi的pom依赖
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.0.1</version>
</dependency>
2.下载文件
/**
* 下载导入模板
* @param response
*/
@RequestMapping(value = "/downloadmb", method = RequestMethod.GET)
public void downloadmb(HttpServletResponse response,HttpServletRequest request) {
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
File file = new File("E://专业计划导入模板.xlsx");
in = new BufferedInputStream(new FileInputStream(file));
out = new BufferedOutputStream(response.getOutputStream());
response.setContentType(new MimetypesFileTypeMap().getContentType(file));// 设置response内容的类型
String filename="专业导入模板";
if (isMSBrowsers.isMSBrowser(request)) {
filename = URLEncoder.encode(filename, "UTF-8");
} else {
filename = new String(filename.getBytes("utf-8"), "ISO8859-1");
}
response.setHeader("Content-disposition", "attachment;filename=" + filename + "." +"xlsx");// 设置头部信息
byte[] buffer = new byte[1024];
int length = 0;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.批量导入
/**
* *批量导入
* @param file
* @return
*/
@RequestMapping("/imports")
@ResponseBody
public MyAjaxResult imports(MultipartFile file) {
ImportParams params = new ImportParams();
List<Map<String, Object>> list = null;
Map<String,Object> map;
try {
params.setTitleRows(1);
params.setHeadRows(1);
list = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, params);
map=majorService.imports(list,"1");
} catch (Exception e) {
e.printStackTrace();
log.info("批量导入转换异常");
return MyAjaxResult.fail_300("批量导入转换异常");
}
for ( Map<String, Object> s : list) {
System.out.println(s.toString());
}
return MyAjaxResult.success(map);
}
4.批量导出数据
/**
* 批量导出数据
* @param response
*/
@RequestMapping(value = "/download")
public void download(HttpServletResponse response,HttpServletRequest request,String ids) {
List<MajorEntity> am=null;
if(ids==null) {//查询所有数据
am=majorService.selectAllMajor();
}else {//查询指定数据
try {
List<Integer> list;
String id[]=ids.split("-");
list = new ArrayList<>();
for(int i=0,j=id.length;i<j;i++) {
list.add(Integer.valueOf(id[i]));
}
am=majorService.selectAllMajor(list);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
try {
String filename="专业";
if (isMSBrowsers.isMSBrowser(request)) {
filename = URLEncoder.encode(filename, "UTF-8");
} else {
filename = new String(filename.getBytes("utf-8"), "ISO8859-1");
}
response.setContentType("application/ms-excel;charset=utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + filename + "." +"xls");// 设置头部信息
ServletOutputStream out=response.getOutputStream();
Workbook wb=getExcel(am);
wb.write(out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 封装
* @param l
* @return
*/
private Workbook getExcel(List<MajorEntity> l) {
List<ExcelExportEntity> colList = new ArrayList<ExcelExportEntity>();
ExcelExportEntity colEntity = new ExcelExportEntity("序号", "xh");
colEntity.setNeedMerge(true);
colList.add(colEntity);
colEntity = new ExcelExportEntity("专业代号", "zydh");
colEntity.setNeedMerge(true);
colList.add(colEntity);
colEntity = new ExcelExportEntity("专业代码", "zydm");
colEntity.setNeedMerge(true);
colList.add(colEntity);
colEntity = new ExcelExportEntity("专业名称", "zymc");
colEntity.setNeedMerge(true);
colList.add(colEntity);
colEntity = new ExcelExportEntity("文科报名人数", "wkbmrs");
colEntity.setNeedMerge(true);
colList.add(colEntity);
colEntity = new ExcelExportEntity("理科报名人数", "lkbmrs");
colEntity.setNeedMerge(true);
colList.add(colEntity);
colEntity = new ExcelExportEntity("对口报名人数", "dkbmrs");
colEntity.setNeedMerge(true);
colList.add(colEntity);
colEntity = new ExcelExportEntity("报名总人数", "bmzrs");
colEntity.setNeedMerge(true);
colList.add(colEntity);
colEntity = new ExcelExportEntity("计划人数", "jhrs");
colEntity.setNeedMerge(true);
colList.add(colEntity);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
int i=1;
for (MajorEntity m:l) {
Map<String, Object> valMap = new HashMap<String, Object>();
valMap.put("xh",i++);
valMap.put("zydh",m.getZydh());
valMap.put("zydm",m.getZydm());
valMap.put("zymc",m.getZymc());
valMap.put("wkbmrs",m.getWkbmrs());
valMap.put("lkbmrs",m.getLkbmrs());
valMap.put("dkbmrs",m.getDkbmrs());
valMap.put("bmzrs",m.getWkbmrs()+m.getLkbmrs()+m.getDkbmrs());
valMap.put("jhrs",m.getJhrs());
list.add(valMap);
}
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("专业计划", "数据"), colList,list);
return workbook;
}
评论区
请写下您的评论...
猜你喜欢
weblog
2450
java使用easypoi导出并下载excel文件简单导出测试如图:一、pom文件dependencygroupIdcn.afterturn/groupIdartifactIdeasypoi
weblog
1106
java使用easypoi批量导入excel实体类:importcn.afterturn.easypoi.excel.annotation.Excel
算法基础
1938
什么是jxlsjxls是一个简单的、轻量级的excel导出库,使用特定的标记在excel模板文件中来定义输出格式和布局。其实java中成熟的excel导出工具有pol、jxl,但他们都是使用java
weblog
5616
java使用easypoi导出excel时进行值替换如导出性别:数据库中储存的是数字,1代表男,2代表女。导出的时候需要是汉字。在实体类中使用@Excel注解。 @Excel(name="性别
框架
1376
最近在项目中遇到了一个批量导入excel的功能,excel导入用到的是esaypoi,可以轻松将excel中的数据封装成对象,但是不知为何,突然转换对象的过程变得很慢,一万条数据得转换一分钟。无奈只
blog
网络中的上行带宽和下行带宽
计算机网络基础
2228
(BitPerSecond),比特率越高,单位时间内传输的数据越多。上行带宽即上行速率一般是指从你的电脑上传的速度,别人从你的电脑进行通讯的速率。下行带宽即下行速率是指网络向用户电脑发送信息时的传输速率,从网上下载文件,
ofc
计算机网络-物理层下面的传输媒体
official
642
《计算机网络第七版谢希仁》
[TOC] 传输媒体也称为传输介质或传输媒介,它就是数据传输系统中在发送器和接收器之间的物理通路。传输媒体可分为两大类,即导引型传输媒体和非导引型传输媒体(这里的“导引
blog
基本求导法则与求导公式
其他
3517
基本求导法则与求导公式不定积分的性质:不定积分的计算:
最新发表
归档
2018-11
12
2018-12
33
2019-01
28
2019-02
28
2019-03
32
2019-04
27
2019-05
33
2019-06
6
2019-07
12
2019-08
12
2019-09
21
2019-10
8
2019-11
15
2019-12
25
2020-01
9
2020-02
5
2020-03
16
2020-04
4
2020-06
1
2020-07
7
2020-08
13
2020-09
9
2020-10
5
2020-12
3
2021-01
1
2021-02
5
2021-03
7
2021-04
4
2021-05
4
2021-06
1
2021-07
7
2021-08
2
2021-09
8
2021-10
9
2021-11
16
2021-12
14
2022-01
7
2022-05
1
2022-08
3
2022-09
2
2022-10
2
2022-12
5
2023-01
3
2023-02
1
2023-03
4
2023-04
2
2023-06
3
2023-07
4
2023-08
1
2023-10
1
2024-02
1
2024-03
1
2024-04
1
标签
算法基础
linux
前端
c++
数据结构
框架
数据库
计算机基础
储备知识
java基础
ASM
其他
深入理解java虚拟机
nginx
git
消息中间件
搜索
maven
redis
docker
dubbo
vue
导入导出
软件使用
idea插件
协议
无聊的知识
jenkins
springboot
mqtt协议
keepalived
minio
mysql
ensp
网络基础
xxl-job
rabbitmq
haproxy
srs
音视频
webrtc
javascript
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。