java+layui的Excel导入导出
html:
- <button class="layui-btn" onclick="exportData();">导出</button>
- <button class="layui-btn" onclick="downloadTemplate();">模板下载</button>
- <button id="importData" class="layui-btn" onclick="importData()">导入</button>
js:
- //导入 用layui upload插件
layui.use([ "element", "laypage", "layer", "upload"], function() {- var element = layui.element;
- var laypage = layui.laypage;
- var layer = layui.layer;
- var upload = layui.upload;//主要是这个
- layui.upload.render({
- elem: "#importData",//导入id
- url: "/xx/importData",
- size: '3072',
- accept: "file",
- exts: 'xls|xlsx|xlsm|xlt|xltx|xltm',
- done: function (result) {
- if (result.status == 0) {
- refreshTable()
- }
- if (result.message != null) {
- refreshTable();
- layer.msg(result.message)
- }
- }
- });
- refreshTable()
- });
- //导出
- function exportData() {
- $.ajax({
- type: "post", url: "/xx/exportData", data: {}, success: function (result) {
- if (result.status == 0) {
- window.open(result.data)
- }
- if (result.message != null) {
- layer.msg(result.message)
- }
- }, error: function (XMLHttpRequest, textStatus, errorThrown) {
- layer.msg('{"status":"' + XMLHttpRequest.status + '","readyState":"' + XMLHttpRequest.readyState + '","textStatus":"' + textStatus + '","errorThrown":"' + errorThrown + '"}')
- }
- })
- };
- //模板下载
- function downloadTemplate() {
- $.ajax({
- type: "post", url: "/xx/downloadTemplate", data: {}, success: function (result) {
- if (result.status == 0) {
- window.open(result.data)
- }
- if (result.message != null) {
- layer.msg(result.message)
- }
- }, error: function (XMLHttpRequest, textStatus, errorThrown) {
- layer.msg('{"status":"' + XMLHttpRequest.status + '","readyState":"' + XMLHttpRequest.readyState + '","textStatus":"' + textStatus + '","errorThrown":"' + errorThrown + '"}')
- }
- })
- }
java后端:
- // 导入
- @PostMapping(value = "importData")
- ResultJson importData(@RequestParam MultipartFile file) {
- ResultJson resultJson = new ResultJson();
- List<ProjectJson> importData = null;
- try {
- importData = ExcelUtil.importExcel(file.getInputStream(), ProjectJson.class);
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (null == importData) {
- resultJson.setStatus(1);
- resultJson.setMessage("导入失败!");
- return resultJson;
- }
- int num = 0;
- for (ProjectJson projectJson : importData) {
- ProjectManageEntity projectManageEntity = new ProjectManageEntity();
- num++;
- if (CommonUtil.isEmpty(projectJson.getNumber())) {
- resultJson.setMessage("导入失败!第" + num + "行的编号不能为空");
- resultJson.setStatus(1);
- return resultJson;
- }
- if (projectService.findByprojectNumber(projectJson.getNumber()) != null) {
- resultJson.setStatus(1);
- resultJson.setMessage("导入失败!第" + num + "行的编号重复");
- resultJson.setData(null);
- return resultJson;
- }
- projectManageEntity.setCreateDate(new Date());
- projectManageEntity.setUpdateDate(new Date());
- projectManageEntity.setName(projectJson.getName());
- projectManageEntity.setNumber(projectJson.getNumber());
- projectManageEntity.setType(projectJson.getType());
- projectManageEntity.setDeleteMark(false);
- projectManageEntity.setCode("PROJECT_MANAGE" + UUID.randomUUID());
- projectService.save(projectManageEntity);
- }
- resultJson.setStatus(0);
- resultJson.setMessage("导入成功!");
- resultJson.setData(null);
- return resultJson;
- }
- //导出
- @PostMapping(value = "exportData")
- ResultJson exportData() {
- String excelTitle = "项目管理";
- String path = "/export/company/excel/" + DateUtil.getEightDateFormat().format(new Date());
- String realPath = CommonUtil.createFolderPath(propertyUtil.getUploadPath() + path);
- return ExcelUtil.exportExcel(excelTitle, realPath, path, ProjectJson.class, projectService.getAll());
- }
- //下载模板
- @PostMapping(value = "downloadTemplate")
- public ResultJson downloadTemplate() {
- String excelTitle = "项目管理模板";
- String path = "/export/company/excel/" + DateUtil.getEightDateFormat().format(new Date());
- String realPath = CommonUtil.createFolderPath(propertyUtil.getUploadPath() + path);
- return ExcelUtil.exportExcel(excelTitle, realPath, path, ProjectJson.class, projectService.getDownloadTemplate());
- }
- /**
- * 创建指定目录
- *
- * @param folderPath 目录地址
- * @return 目录地址
- */
- public static final String createFolderPath(String folderPath) {
- File file = new File(folderPath);
- if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
- String[] diskNames = {"A:", "B:", "C:", "D:", "E:", "F:", "G:", "H:", "I:", "J:", "K:", "L:", "M:", "N:",
- "O:", "P:", "Q:", "R:", "S:", "T:", "U:", "V:", "W:", "X:", "Y:", "Z:"};
- for (int i = 0; i < diskNames.length; i++) {
- if (i > 0) {
- folderPath = folderPath.replace(diskNames[i - 1], diskNames[i]);
- } else {
- folderPath = diskNames[0] + folderPath;
- }
- file = new File(folderPath);
- if (!file.exists()) {
- file.mkdirs();
- }
- if (file.exists()) {
- return folderPath;
- }
- }
- return null;
- } else {
- if (!file.exists()) {
- file.mkdirs();
- }
- if (file.exists()) {
- return folderPath;
- }
- return null;
- }
- }
- public class ExcelUtil {
- /**
- * 导入Excel
- *
- * @param inputstream
- * Excel数据流
- * @param clazz
- * 注解的实体类
- * @return 导入的数据
- */
- public static <T> List<T> importExcel(InputStream inputstream, Class<T> clazz) {
- ImportParams params = new ImportParams();
- params.setTitleRows(1);
- params.setNeedSave(true);
- try {
- return ExcelImportUtil.importExcel(inputstream, clazz, params);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * 导出Excel
- *
- * @param excelTitle
- * Excel标题
- * @param realPath
- * 真实路径
- * @param path
- * 响应路径
- * @param clazz
- * 注解的实体类
- * @param excels
- * 注解的实体类集合
- * @return 响应结果
- */
- public static <T> ResultJson exportExcel(String excelTitle, String realPath, String path, Class<T> clazz,
- List<T> excels) {
- ResultJson resultJson = new ResultJson();
- try {
- Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(excelTitle, excelTitle), clazz, excels);
- String filename = excelTitle + "-" + DateUtil.getEightDateFormat().format(new Date()) + ".xls";
- File dirPath = new File(realPath);
- if (!dirPath.exists()) {
- dirPath.mkdirs();
- }
- try {
- FileOutputStream outputStream = new FileOutputStream(realPath + "/" + filename);
- workbook.write(outputStream);
- resultJson.setStatus(0);
- resultJson.setData(path + "/" + filename);
- } catch (Exception e) {
- e.printStackTrace();
- resultJson.setStatus(1);
- return resultJson;
- }
- } catch (Exception e) {
- e.printStackTrace();
- resultJson.setStatus(1);
- return resultJson;
- }
- return resultJson;
- }
- }
java+layui的Excel导入导出的更多相关文章
- Java 使用 Jxl 实现 Excel 导入导出
开发过程中经常需要用到数据的导入导出功能,之前用的是POI,这次使用JXL,JXL相对于POI来说要轻量简洁许多,在数据量不大的情况下还是非常实用的.这里做一下使用JXL的学习记录.首先需要导入相应的 ...
- java jxl excel 导入导出的 总结(建立超链接,以及目录sheet的索引)
最近项目要一个批量导出功能,而且要生成一个单独的sheet页,最后后面所有sheet的索引,并且可以点击进入连接.网上搜索了一下,找到一个方法,同时把相关的excel导入导出操作记录一下!以便以后使用 ...
- Java实现将Excel导入数据库和从数据库中导出为Excel
实现的功能: 用Java实现从Excel导入数据库,如果存在就更新 将数据库中的数据导出为Excel 1.添加jxl.jar mysql-connector-java.1.7-bin.jar包到项目的 ...
- JAVA实现Excel导入/导出【转】
JAVA实现Excel导入/导出[转] POI的下载与安装 请到网站http://www.apache.org/dyn/closer.cgi/poi/右击超链接2.5.1.zip下载压缩包poi-bi ...
- Java之POI的excel导入导出
一.Apache POI是一种流行的API,它允许程序员使用Java程序创建,修改和显示MS Office文件.这由Apache软件基金会开发使用Java分布式设计或修改Microsoft Offic ...
- java简易excel导入导出工具(封装POI)
Octopus 如何导入excel 如何导出excel github项目地址 Octopus Octopus 是一个简单的java excel导入导出工具. 如何导入excel 下面是一个excel文 ...
- JAVA Excel导入导出
--------------------------------------------方式一(新)-------------------------------------------------- ...
- Java基础学习总结(49)——Excel导入导出工具类
在项目的pom文件中引入 <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifac ...
- Java Excel 导入导出(一)
本文主要描述通过java实现Excel导入导出 一.读写Excel三种常用方式 1.JXL——Java Excel开放源码项目:读取,创建,更新 2.POI——Apache POI ,提供API给Ja ...
随机推荐
- babel环境安装与编译
babel:将浏览器不支持的ES6语法转为javascript 查看node是否安装: npm -v node -v 实例演示:在桌面新建part5目录在cmd命令行中 cd desktop cd p ...
- Docke-ce 安装
Docker-ce 的安装 安装系统工具 yum install -y yum-utils device-mapper-persistent-data lvm2 添加docker镜像源 yum-con ...
- open()和with open()的区别
open 1,打开文件 file=open("文件名",“读写模式”) 2,操作文件 *** 3,关闭文件 file.close() 注意事项: 使用open方法,文件操作完毕之后 ...
- [[FJOI2016]神秘数][主席树]
明白之后 5min 就写好了-自闭- 这题的题意是问你 \([L,R]\) 区间的数字不能构成的数字的最小值- 首先考虑 如果 \([1,x]\) 可以被表示 那么加入一个 \(a_i\) 显然 \( ...
- 阻止a链接跳转的点击事件
<a href="http://www.baidu.com" id="btn">按钮</a> <script> docume ...
- java集合框架备忘
List,Set,Map三者的区别? List(对付顺序的好帮手): List接口存储一组不唯一(可以有多个元素引用相同的对象),有序的对象 Set(注重独一无二的性质): 不允许重复的集合.不会有多 ...
- 07-SV线程以及线程间的通信
1.几种语句块的区别 (1)fork……join:块内语句以并发方式执行 (2)begin……end:块内语句以顺序方式执行 (3)fork……join_none:其块内语句执行时,父线程继续执行 ( ...
- Linux 编译安装python3
编译安装python3的步骤 1.很重要,必须执行此操作,安装好编译环境,c语言也是编译后运行,需要gcc编译器golang,对代码先编译,再运行,python是直接运行yum install gcc ...
- php如何获取单选复选和选择框的值
1.很久没有写基础的东西了复习一下(往往简单的东西才复杂) <body> 选择语句 <form action="demo.php" method="po ...
- JS 动态改变浏览器title标题
onfocus 和 onblur 事件,监听离开页面和进入页面 <script> window.onfocus = function () { document.title = '要走的人 ...