java+layui实现excel文件的模板下载和导入
因为layui表格自带导出功能,所以就不再做导出功能
相关pom文件
<!--引入poi-->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.0.0</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.0.0</version>
</dependency>
前端代码
<!--文件上传的div--><div class="layui-upload-drag" id="excel"><i class="layui-icon"></i><p>点击上传,或将文件拖拽到此处</p><div class="layui-hide" id="uploadDemoView"><hr><img src="" alt="上传成功后渲染" style="max-width: 196px"></div></div><form action="/student/downloadExcel" method="get"><div class="layui-form-item"><div class="layui-input-inline"></div><input class="layui-btn layui-btn-primary layui-border-orange" type="submit" value="下载模板"></div></form>
前端js
layui.use(['form','upload'], function () {var form = layui.form;var upload = layui.upload;//上传//拖拽上传upload.render({elem:'#excel',url:'student/excelImportStudent',accept:'file',done: function (res) {layer.msg(res.msg);console.log(res);}});
后端controller
//excel的拖拽和点击上传@RequestMapping("/excelImportStudent")@ResponseBodypublic DataView excelImportStudent(@RequestParam("file") MultipartFile file) throws Exception{DataView dataView = new DataView();//文件不能为空if(file.isEmpty()){dataView.setMsg("文件为空,不能上传");}//poi获取excelXSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());XSSFSheet sheet = wb.getSheetAt(0);//定义一个程序集合,接受文件中的数据List<Student> list = new ArrayList<>();XSSFRow row = null;//解析数据,装到集合里面for(int i = 0;i<sheet.getPhysicalNumberOfRows();i++){//定义实体Student student = new Student();//每行的数据,放到实体类里面row = sheet.getRow(i);row.getCell(0).setCellType(CellType.STRING);row.getCell(1).setCellType(CellType.STRING);row.getCell(2).setCellType(CellType.STRING);row.getCell(3).setCellType(CellType.STRING);row.getCell(4).setCellType(CellType.STRING);row.getCell(5).setCellType(CellType.STRING);//解析数据student.setStudentName(row.getCell(0).getStringCellValue());student.setSex(row.getCell(1).getStringCellValue());student.setAge(row.getCell(2).getStringCellValue());student.setGrade(row.getCell(3).getStringCellValue());student.setPhone(row.getCell(4).getStringCellValue());student.setAddress(row.getCell(5).getStringCellValue());student.setCollegeId((int)row.getCell(6).getNumericCellValue());//添加list集合list.add(student);}//插入数据库studentService.saveBatch(list);dataView.setCode(200);dataView.setMsg("插入成功");return dataView;}//excel模板文件下载@GetMapping("/downloadExcel")public void downloadExcel(HttpServletResponse response) {String filePath = "templates/excel/student.xlsx"; //模板文件存放路径try (OutputStream os = response.getOutputStream();InputStream bis = new BufferedInputStream(new ClassPathResource(filePath).getInputStream())) {String type = new MimetypesFileTypeMap().getContentType("student.xlsx");response.setContentType(type);String name = URLEncoder.encode("student.xlsx", "UTF-8");response.setHeader("Content-Disposition", "attachment;filename=" + name);studentService.download(os, bis); //业务层方法} catch (Exception e) {e.printStackTrace();}}
service
public interface StudentService extends IService<Student>{
void download(OutputStream os, InputStream bis);
}
serviceImpl
@Slf4j
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper,Student> implements StudentService {@Overridepublic void download(OutputStream os, InputStream bis) {try{XSSFWorkbook workbook = new XSSFWorkbook(bis);workbook.write(os);}catch (Exception e){e.printStackTrace();}}
}
别忘了给实体类添加
@TableId(value = "id",type = IdType.AUTO)
让主键id通过自增方式,不加的话会导致接口异常,导入不进去。
java+layui实现excel文件的模板下载和导入相关推荐
- django返回excel文件给前端下载
Django使用openpyxl返回excel文件给前端下载 虚拟环境导入 pip install openpyxl views.py代码: import datetime from openpyxl ...
- java实现excel文件上传_java相关:SpringMVC下实现Excel文件上传下载
java相关:SpringMVC下实现Excel文件上传下载 发布于 2020-6-21| 复制链接 摘记: 在实际应用中,经常会遇到上传Excel或者下载Excel的情况,比如导入数据.下载统计数据 ...
- 【vue+springboot】excel模板下载、导入功能实现
基于VUE+SpringBoot实现excel模板下载.导入功能 背景 最近在工作中经常遇到批量导入的功能,而且前端还要提示导入成功几条.失败几条.哪一条数据重复.是哪一条导入的数据出现问题等,抽空写 ...
- Java分页导出Excel文件
Java分页导出Excel文件 1.开发微信小程序接入广告获取收益 技术: Springboot + mongodb + maven +Layui + thymeleaf 上一篇博客已经讲解了Java ...
- java导出 elsx 文件,如何获取java导出的excel文件,发送请求导出excle文件
如何获取java导出的excel文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 request.setCharacterEncoding("utf-8"); ...
- excel文件导出、下载的响应类型设置
excel文件下载(模板下载)或者导出excel文件时请求接口需要设置响应类型responseType:'blob' 例如: // excel模板文件下载 export function downlo ...
- Java实现从Excel文件转换成XML文件(一)
实现思路: 直接从Excel文件装换成XML文件是可以实现的,这里我采用一个中间装换,也就是先实现excel文件内容放入实现设计好的access数据库文件中,然后再从access中读取 ...
- java jxl 写 excel文件_使用jxl读和写Excel文件(速成)
同行整理,简单明了,快速上手! =============================正文1============================ 最近和Excel频繁亲密接触,主要将N个Exc ...
- java 从excel中读取数据_在Java中读取Excel文件的内容和导出数据到Excel文件中
转自www.chianjavaworld.net 原作者:SonyMusic 读:rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr 在Java ...
最新文章
- [BI项目记]-TFS Express备份和恢复
- HashMap原理总结
- hanlp中的N最短路径分词
- WayOs路由器WAN口帐号保存工具,可直接发送至邮箱,及保存接口VLAN号
- 剑网三缘起,签到领奖活动还能获得白猫,满级玩家5天就得跟宠
- 你走过最长的路 ,就是机器学习过程中的弯路
- 从petshop中一实例谈using 的三种用法
- iOS 图片拉伸、拉伸两端保留中间
- Ruby on Rails,创建模型,附赠模型与表名不一致时的解决方法
- @Configuration与@Component作为配置类的区别
- CentOS 6.5 yum安装mysql5.6或其他版本【默认yum只能安装mysql 5.1】 by jason
- 稳压二极管(齐纳Zener二极管)的接法和应用详解
- 远程teamview下载网址,和ie 下载地址
- windows下的mujoco环境搭建
- 75.(leaflet之家)leaflet柱状图
- Android两种方式实现横向滚动图标+指示器
- HDU 1867(kmp应用)
- 人工智能 AI 绘画发展史
- QTEmbedded VCN实现
- CS:APP二进制炸弹phase5
热门文章
- Eclipse 进行 web开发的环境配置
- 小米笔记本电脑安装Ubuntu系统
- 从tushare获取场内ETF基金数据
- 【硬件调试-2】flir灰点相机usb设备权限设置,无法检测到设备
- C语言字符串string.h详解
- 单机启动schedule报错:Get http://127.0.0.1:10251/healthz: dial tcp 127.0.0.1:10251: connect: connection ref
- Vue 3的高颜值UI组件库
- 目标检测算法之YOLO
- 详解Unity中的粒子系统Particle System (一)
- matplotlib之pyplot模块之柱状图(bar():百分比堆积柱状图)