因为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文件的模板下载和导入相关推荐

  1. django返回excel文件给前端下载

    Django使用openpyxl返回excel文件给前端下载 虚拟环境导入 pip install openpyxl views.py代码: import datetime from openpyxl ...

  2. java实现excel文件上传_java相关:SpringMVC下实现Excel文件上传下载

    java相关:SpringMVC下实现Excel文件上传下载 发布于 2020-6-21| 复制链接 摘记: 在实际应用中,经常会遇到上传Excel或者下载Excel的情况,比如导入数据.下载统计数据 ...

  3. 【vue+springboot】excel模板下载、导入功能实现

    基于VUE+SpringBoot实现excel模板下载.导入功能 背景 最近在工作中经常遇到批量导入的功能,而且前端还要提示导入成功几条.失败几条.哪一条数据重复.是哪一条导入的数据出现问题等,抽空写 ...

  4. Java分页导出Excel文件

    Java分页导出Excel文件 1.开发微信小程序接入广告获取收益 技术: Springboot + mongodb + maven +Layui + thymeleaf 上一篇博客已经讲解了Java ...

  5. 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"); ...

  6. excel文件导出、下载的响应类型设置

    excel文件下载(模板下载)或者导出excel文件时请求接口需要设置响应类型responseType:'blob' 例如: // excel模板文件下载 export function downlo ...

  7. Java实现从Excel文件转换成XML文件(一)

    实现思路:         直接从Excel文件装换成XML文件是可以实现的,这里我采用一个中间装换,也就是先实现excel文件内容放入实现设计好的access数据库文件中,然后再从access中读取 ...

  8. java jxl 写 excel文件_使用jxl读和写Excel文件(速成)

    同行整理,简单明了,快速上手! =============================正文1============================ 最近和Excel频繁亲密接触,主要将N个Exc ...

  9. java 从excel中读取数据_在Java中读取Excel文件的内容和导出数据到Excel文件中

    转自www.chianjavaworld.net 原作者:SonyMusic 读:rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr 在Java ...

最新文章

  1. TextSwitcher--文本切换器
  2. 支持markdown的服务器,Vuejs中使用markdown服务器端渲染的示例
  3. 当退出python时是否释放全部内存_python如何释放内存
  4. EditPlus构建轻量级编译环境
  5. 中微公司2020年净利4.92亿元 投资中芯国际赚2.62亿
  6. 推荐系统走向下一阶段最重要的三个问题
  7. 网管学习日记-单臂路由
  8. 内存管理-基础知识框架和关键结构体(一)
  9. Ubunt 14.04 install Sogou input
  10. comsol 4.4 matlab,如何使用COMSOL with MATLAB的清单
  11. ENVI5.4中Himawari-8数据处理(向日葵8号卫星H-8)
  12. MATLAB解决常微分方程
  13. python 字符串 1.1 从键盘输入10个英文单词,输出其中以元音字母开头的单词。
  14. Arduino怎么设置中文版?
  15. 什么是GO+和GO哪里不一样
  16. python答题搜索_python实现百万答题自动百度搜索答案
  17. Typora如何将图片使用相对路径保存到统一文件夹中(解决.md文档传输丢图片的方法)
  18. 开源的微信个人号接口 itchat 使用
  19. 二、mybatis 多级关联映射配置
  20. DNS over HTTPs分析和威胁检测

热门文章

  1. C++链表02:反转链表
  2. 【愚公系列】2022年10月 微信小程序-电商项目-UI设计之蓝湖的使用
  3. 2021-10-15 - js
  4. SAP HANA Procedure的Odata更新(POST)表操作
  5. 机械精度设计与检测|几何精度
  6. sp公司新人培训经典教案
  7. 代码扫描问题以及解决方式(转载备忘)
  8. 三种录制接口脚本的方法
  9. 最详尽的PyCharm 实用教程,值得一看!
  10. revit二次开发——建连续刚构桥2(带平纵曲线)