html:

  1. <button class="layui-btn" onclick="exportData();">导出</button>
  2. <button class="layui-btn" onclick="downloadTemplate();">模板下载</button>
  3. <button id="importData" class="layui-btn" onclick="importData()">导入</button>

js:

  1. //导入 用layui upload插件
    layui.use([ "element", "laypage", "layer", "upload"], function() {
  2. var element = layui.element;
  3. var laypage = layui.laypage;
  4. var layer = layui.layer;
  5. var upload = layui.upload;//主要是这个
  6. layui.upload.render({
  7. elem: "#importData",//导入id
  8. url: "/xx/importData",
  9. size: '3072',
  10. accept: "file",
  11. exts: 'xls|xlsx|xlsm|xlt|xltx|xltm',
  12. done: function (result) {
  13. if (result.status == 0) {
  14. refreshTable()
  15. }
  16. if (result.message != null) {
  17. refreshTable();
  18. layer.msg(result.message)
  19. }
  20. }
  21. });
  22. refreshTable()
  23. });
  1. //导出
  2. function exportData() {
  3. $.ajax({
  4. type: "post", url: "/xx/exportData", data: {}, success: function (result) {
  5. if (result.status == 0) {
  6. window.open(result.data)
  7. }
  8. if (result.message != null) {
  9. layer.msg(result.message)
  10. }
  11. }, error: function (XMLHttpRequest, textStatus, errorThrown) {
  12. layer.msg('{"status":"' + XMLHttpRequest.status + '","readyState":"' + XMLHttpRequest.readyState + '","textStatus":"' + textStatus + '","errorThrown":"' + errorThrown + '"}')
  13. }
  14. })
  15. };
  16.  
  17. //模板下载
  18. function downloadTemplate() {
  19. $.ajax({
  20. type: "post", url: "/xx/downloadTemplate", data: {}, success: function (result) {
  21. if (result.status == 0) {
  22. window.open(result.data)
  23. }
  24. if (result.message != null) {
  25. layer.msg(result.message)
  26. }
  27. }, error: function (XMLHttpRequest, textStatus, errorThrown) {
  28. layer.msg('{"status":"' + XMLHttpRequest.status + '","readyState":"' + XMLHttpRequest.readyState + '","textStatus":"' + textStatus + '","errorThrown":"' + errorThrown + '"}')
  29. }
  30. })
  31. }
  1.  
  1.  

java后端:

  1. // 导入
  2. @PostMapping(value = "importData")
  3. ResultJson importData(@RequestParam MultipartFile file) {
  4. ResultJson resultJson = new ResultJson();
  5. List<ProjectJson> importData = null;
  6. try {
  7. importData = ExcelUtil.importExcel(file.getInputStream(), ProjectJson.class);
  8. } catch (IOException e) {
  9. e.printStackTrace();
  10. }
  11. if (null == importData) {
  12. resultJson.setStatus(1);
  13. resultJson.setMessage("导入失败!");
  14. return resultJson;
  15. }
  16. int num = 0;
  17.  
  18. for (ProjectJson projectJson : importData) {
  19. ProjectManageEntity projectManageEntity = new ProjectManageEntity();
  20. num++;
  21. if (CommonUtil.isEmpty(projectJson.getNumber())) {
  22. resultJson.setMessage("导入失败!第" + num + "行的编号不能为空");
  23. resultJson.setStatus(1);
  24. return resultJson;
  25. }
  26. if (projectService.findByprojectNumber(projectJson.getNumber()) != null) {
  27. resultJson.setStatus(1);
  28. resultJson.setMessage("导入失败!第" + num + "行的编号重复");
  29. resultJson.setData(null);
  30. return resultJson;
  31. }
  32.  
  33. projectManageEntity.setCreateDate(new Date());
  34. projectManageEntity.setUpdateDate(new Date());
  35. projectManageEntity.setName(projectJson.getName());
  36. projectManageEntity.setNumber(projectJson.getNumber());
  37. projectManageEntity.setType(projectJson.getType());
  38. projectManageEntity.setDeleteMark(false);
  39. projectManageEntity.setCode("PROJECT_MANAGE" + UUID.randomUUID());
  40. projectService.save(projectManageEntity);
  41. }
  42.  
  43. resultJson.setStatus(0);
  44. resultJson.setMessage("导入成功!");
  45. resultJson.setData(null);
  46. return resultJson;
  47. }
  48.  
  49. //导出
  50. @PostMapping(value = "exportData")
  51. ResultJson exportData() {
  52. String excelTitle = "项目管理";
  53. String path = "/export/company/excel/" + DateUtil.getEightDateFormat().format(new Date());
  54. String realPath = CommonUtil.createFolderPath(propertyUtil.getUploadPath() + path);
  55.  
  56. return ExcelUtil.exportExcel(excelTitle, realPath, path, ProjectJson.class, projectService.getAll());
  57. }
  58.  
  59. //下载模板
  60. @PostMapping(value = "downloadTemplate")
  61. public ResultJson downloadTemplate() {
  62. String excelTitle = "项目管理模板";
  63. String path = "/export/company/excel/" + DateUtil.getEightDateFormat().format(new Date());
  64. String realPath = CommonUtil.createFolderPath(propertyUtil.getUploadPath() + path);
  65. return ExcelUtil.exportExcel(excelTitle, realPath, path, ProjectJson.class, projectService.getDownloadTemplate());
  66. }
  1. /**
  2. * 创建指定目录
  3. *
  4. * @param folderPath 目录地址
  5. * @return 目录地址
  6. */
  7. public static final String createFolderPath(String folderPath) {
  8. File file = new File(folderPath);
  9. if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
  10. String[] diskNames = {"A:", "B:", "C:", "D:", "E:", "F:", "G:", "H:", "I:", "J:", "K:", "L:", "M:", "N:",
  11. "O:", "P:", "Q:", "R:", "S:", "T:", "U:", "V:", "W:", "X:", "Y:", "Z:"};
  12. for (int i = 0; i < diskNames.length; i++) {
  13. if (i > 0) {
  14. folderPath = folderPath.replace(diskNames[i - 1], diskNames[i]);
  15. } else {
  16. folderPath = diskNames[0] + folderPath;
  17. }
  18. file = new File(folderPath);
  19. if (!file.exists()) {
  20. file.mkdirs();
  21. }
  22. if (file.exists()) {
  23. return folderPath;
  24. }
  25. }
  26. return null;
  27. } else {
  28. if (!file.exists()) {
  29. file.mkdirs();
  30. }
  31. if (file.exists()) {
  32. return folderPath;
  33. }
  34. return null;
  35. }
  36. }
  1. public class ExcelUtil {
  2.  
  3. /**
  4. * 导入Excel
  5. *
  6. * @param inputstream
  7. * Excel数据流
  8. * @param clazz
  9. * 注解的实体类
  10. * @return 导入的数据
  11. */
  12. public static <T> List<T> importExcel(InputStream inputstream, Class<T> clazz) {
  13. ImportParams params = new ImportParams();
  14. params.setTitleRows(1);
  15. params.setNeedSave(true);
  16. try {
  17. return ExcelImportUtil.importExcel(inputstream, clazz, params);
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. return null;
  21. }
  22. }
  23.  
  24. /**
  25. * 导出Excel
  26. *
  27. * @param excelTitle
  28. * Excel标题
  29. * @param realPath
  30. * 真实路径
  31. * @param path
  32. * 响应路径
  33. * @param clazz
  34. * 注解的实体类
  35. * @param excels
  36. * 注解的实体类集合
  37. * @return 响应结果
  38. */
  39. public static <T> ResultJson exportExcel(String excelTitle, String realPath, String path, Class<T> clazz,
  40. List<T> excels) {
  41. ResultJson resultJson = new ResultJson();
  42. try {
  43. Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(excelTitle, excelTitle), clazz, excels);
  44. String filename = excelTitle + "-" + DateUtil.getEightDateFormat().format(new Date()) + ".xls";
  45. File dirPath = new File(realPath);
  46. if (!dirPath.exists()) {
  47. dirPath.mkdirs();
  48. }
  49. try {
  50. FileOutputStream outputStream = new FileOutputStream(realPath + "/" + filename);
  51. workbook.write(outputStream);
  52. resultJson.setStatus(0);
  53. resultJson.setData(path + "/" + filename);
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. resultJson.setStatus(1);
  57. return resultJson;
  58. }
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. resultJson.setStatus(1);
  62. return resultJson;
  63. }
  64. return resultJson;
  65. }
  66.  
  67. }

java+layui的Excel导入导出的更多相关文章

  1. Java 使用 Jxl 实现 Excel 导入导出

    开发过程中经常需要用到数据的导入导出功能,之前用的是POI,这次使用JXL,JXL相对于POI来说要轻量简洁许多,在数据量不大的情况下还是非常实用的.这里做一下使用JXL的学习记录.首先需要导入相应的 ...

  2. java jxl excel 导入导出的 总结(建立超链接,以及目录sheet的索引)

    最近项目要一个批量导出功能,而且要生成一个单独的sheet页,最后后面所有sheet的索引,并且可以点击进入连接.网上搜索了一下,找到一个方法,同时把相关的excel导入导出操作记录一下!以便以后使用 ...

  3. Java实现将Excel导入数据库和从数据库中导出为Excel

    实现的功能: 用Java实现从Excel导入数据库,如果存在就更新 将数据库中的数据导出为Excel 1.添加jxl.jar mysql-connector-java.1.7-bin.jar包到项目的 ...

  4. JAVA实现Excel导入/导出【转】

    JAVA实现Excel导入/导出[转] POI的下载与安装 请到网站http://www.apache.org/dyn/closer.cgi/poi/右击超链接2.5.1.zip下载压缩包poi-bi ...

  5. Java之POI的excel导入导出

    一.Apache POI是一种流行的API,它允许程序员使用Java程序创建,修改和显示MS Office文件.这由Apache软件基金会开发使用Java分布式设计或修改Microsoft Offic ...

  6. java简易excel导入导出工具(封装POI)

    Octopus 如何导入excel 如何导出excel github项目地址 Octopus Octopus 是一个简单的java excel导入导出工具. 如何导入excel 下面是一个excel文 ...

  7. JAVA Excel导入导出

    --------------------------------------------方式一(新)-------------------------------------------------- ...

  8. Java基础学习总结(49)——Excel导入导出工具类

    在项目的pom文件中引入 <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifac ...

  9. Java Excel 导入导出(一)

    本文主要描述通过java实现Excel导入导出 一.读写Excel三种常用方式 1.JXL——Java Excel开放源码项目:读取,创建,更新 2.POI——Apache POI ,提供API给Ja ...

随机推荐

  1. babel环境安装与编译

    babel:将浏览器不支持的ES6语法转为javascript 查看node是否安装: npm -v node -v 实例演示:在桌面新建part5目录在cmd命令行中 cd desktop cd p ...

  2. Docke-ce 安装

    Docker-ce 的安装 安装系统工具 yum install -y yum-utils device-mapper-persistent-data lvm2 添加docker镜像源 yum-con ...

  3. open()和with open()的区别

    open 1,打开文件 file=open("文件名",“读写模式”) 2,操作文件 *** 3,关闭文件 file.close() 注意事项: 使用open方法,文件操作完毕之后 ...

  4. [[FJOI2016]神秘数][主席树]

    明白之后 5min 就写好了-自闭- 这题的题意是问你 \([L,R]\) 区间的数字不能构成的数字的最小值- 首先考虑 如果 \([1,x]\) 可以被表示 那么加入一个 \(a_i\) 显然 \( ...

  5. 阻止a链接跳转的点击事件

    <a href="http://www.baidu.com" id="btn">按钮</a> <script> docume ...

  6. java集合框架备忘

    List,Set,Map三者的区别? List(对付顺序的好帮手): List接口存储一组不唯一(可以有多个元素引用相同的对象),有序的对象 Set(注重独一无二的性质): 不允许重复的集合.不会有多 ...

  7. 07-SV线程以及线程间的通信

    1.几种语句块的区别 (1)fork……join:块内语句以并发方式执行 (2)begin……end:块内语句以顺序方式执行 (3)fork……join_none:其块内语句执行时,父线程继续执行 ( ...

  8. Linux 编译安装python3

    编译安装python3的步骤 1.很重要,必须执行此操作,安装好编译环境,c语言也是编译后运行,需要gcc编译器golang,对代码先编译,再运行,python是直接运行yum install gcc ...

  9. php如何获取单选复选和选择框的值

    1.很久没有写基础的东西了复习一下(往往简单的东西才复杂) <body> 选择语句 <form action="demo.php" method="po ...

  10. JS 动态改变浏览器title标题

    onfocus 和 onblur 事件,监听离开页面和进入页面 <script> window.onfocus = function () { document.title = '要走的人 ...