[java]轻量级隔离化的excel导入导出的导入导出,解放导入导出冗余代码(有github源码)
如何使用
只需要添加相应的注解
@Excel(name = "学生统计数据表",sheetName = "Sheet1"
)
@Data
public class Student {public Student() {}public Student(Long id, String name, Integer age, Date date, Boolean isDel, Double balance, Integer select, String select2) {this.id = id;this.name = name;this.age = age;this.date = date;this.isDel = isDel;this.balance = balance;this.select = select;this.select2 = select2;}private Long id;/*** value 支持绝大多数常用类型 都可以直接显示*/@CellWidth(width = 150)@CellValue(name = "名字",index = 0)private String name;/*** value 支持绝大多数常用类型 都可以直接显示*/@CellWidth(width = 50)@CellValue(name = "年龄",index = 1)private Integer age;@CellWidth(width = 250)@CellDate(name = "日期",index = 2,formatStr = "yyyy-MM-dd hh:mm:ss")private Date date;/*** boolean类型 需要在excel变成其他值*/@CellWidth(width = 50)@CellBoolean(name = "是否删除",index = 3,tureValue = "是", falseValue = "否")private Boolean isDel;/*** double类型 需要保留位数*/@CellWidth(width = 100)@CellDouble(name = "收入",index = 4, fixed = 2)private Double balance;/*** 枚举类型 可以使用数字 keys 就是数据值 values 就是对应显示的值*/@CellWidth(width = 100)@CellSelect(name = "选择",index = 5,keys = {"1","2","3"},values = {"选择1","选择2","选择3"})private Integer select;/*** 枚举类型 可以使用字符串*/@CellWidth(width = 100)@CellSelect(name = "选择2",index = 6,keys = {"a","b","c"},values = {"2选择1","2选择2","2选择3"})private String select2;/*** Formula 填excel公式进* 下面这个就是 计算 e/b 的值 就是计算 下标是4(收入) / 下标是1(年龄) 的值 保留两位小数点*/@CellFormula(name = "年收比", fomula = "=round(E#index/B#index,2)", index = 7)private Double testFormula;}
导出的代码
只需要将设置好注解的对象传进去就可以完成导出
List<Student> students = new ArrayList<>();// 创建需要导出的对象students.add(new Student(1l, "小明", 18, new Date(), null, 1000.12, 1, "a"));students.add(new Student(2l, "小红", 19, new Date(), false, 200.0078, 2,"b"));students.add(new Student(3l, "小国", 19, new Date(), true, 300.567, null,"c"));students.add(new Student(4l, "小太", 18, null, false, 400.1, 2,null));// 生成文件的位置File file = new File("F:\\student.xlsx");try (FileOutputStream outputStream = new FileOutputStream(file)){ExcelImportAndExport<Student> excelUtils = new ExcelImportAndExport<>(Student.class);// 将对象列表转成 excelWorkbook workbook = excelUtils.exportExcel(students);workbook = excelUtils.workbook();workbook.write(outputStream);} catch (Exception e) {e.printStackTrace();}
导入的代码
只需要将excel输入流 放入工具类中 就可以完成入
File file = new File("F:\\student.xlsx");FileInputStream fileInputStream = new FileInputStream(file);// 使用ExcelImportAndExport<Student> excelUtils = new ExcelImportAndExport<>(Student.class);excelUtils.importExcel(fileInputStream, "student.xlsx");List<Student> list = new ArrayList<>();for (int i = 1; i <= excelUtils.getNotNullLastIndex(); i++) {Student student = null;try {student = excelUtils.getObjByRow(i);} catch (ExcelDateParseException e) {System.out.println("第" + (e.getRowIndex() + 1) + "行、第" + (e.getCellIndex() + 1) + "列格式异常 请使用标准格式例:2020-10-10(表格设置单元格格式中选择日期)");continue;} catch (ExcelChanelException e) {System.out.println("第" + (e.getRowIndex() + 1) + "行、第" + (e.getCellIndex() + 1) + "列格式异常 请检查数据格式是否正确,单元格格式是否正确");continue;} catch (ExcelNullpointExcetion e) {System.out.println("第" + (e.getRowIndex() + 1) + "行、第" + (e.getCellIndex() + 1) + "列不能为空");continue;} catch (ExcelOutLenException e) {System.out.println("第" + (e.getRowIndex() + 1) + "行、第" + (e.getCellIndex() + 1) + "列列长度超过了" + e.getMaxLen());continue;}list.add(student);}System.out.println(list);
此处点击进入github地址
[java]轻量级隔离化的excel导入导出的导入导出,解放导入导出冗余代码(有github源码)相关推荐
- php导入qq数据txt代码,/谁有能都实现将excel文件导入到数据中,并在php网页上显示的源码啊,有的发送1091932879@qq.com,谢谢!...
PHP网页怎么导入Excel的数据 参码如下: // 1.引用ExcelReader类文 require_once 'Excel/reader.php'; // 2.实例化读取Excel类 $data ...
- 前端复杂表格导出excel,一键导出 Antd Table 看这篇就够了(附源码)
前端导出 excel 的需求很多,但市面上好用的库并不多,讲明白复杂使用场景的文章更少. 本文将以文字 + demo 源码的形式,力求讲清楚满足 99% 使用场景的终极 excel 导出方案. 如果项 ...
- java毕业设计——基于java+JSP+sqlserver的Smart系统-题库及试卷管理模块设计与实现(毕业论文+程序源码)——学生信息管理系统模板2
基于java+JSP+sqlserver的Smart系统-题库及试卷管理模块设计与实现(毕业论文+程序源码) 大家好,今天给大家介绍基于java+JSP+sqlserver的Smart系统-题库及试卷 ...
- 一招学会DIY官网可视化设计支持导出微擎、UNIAPP、H5、微信小程序源码
DIY官网一站式婚庆案例实战,可视化设计导出微擎.UNIAPP.H5.微信小程序源码. 这里将给大家展示演示出来的所有不同类型演示. DIY官网一站式婚庆案例实战官网在线演示例子 一站式婚庆服务公司后 ...
- java计算机毕业设计HTML5“忆红楼梦之味”网站设计与实现MyBatis+系统+LW文档+源码+调试部署(2)
java计算机毕业设计HTML5"忆红楼梦之味"网站设计与实现MyBatis+系统+LW文档+源码+调试部署(2) java计算机毕业设计HTML5"忆红楼梦之味&quo ...
- Java随机生成Mac地址(GitHub源码下载)
生成随机 Mac 地址 这段时间做项目,涉及到虚拟机 Mac 地址生成,网上找了一些 Java 随机生成 Mac 地址的代码,大多都是从 {'0', '1', '2', '3', '4', '5', ...
- java计算机毕业设计Vue.js网上书城管理系统设计与实现服务端MyBatis+系统+LW文档+源码+调试部署
java计算机毕业设计Vue.js网上书城管理系统设计与实现服务端MyBatis+系统+LW文档+源码+调试部署 java计算机毕业设计Vue.js网上书城管理系统设计与实现服务端MyBatis+系统 ...
- 【java毕业设计】基于javaEE+原生servlet+tomcat的教师工资管理系统设计与实现(毕业论文+程序源码)——教师工资管理系统
基于javaEE+原生servlet+tomcat的教师工资管理系统设计与实现(毕业论文+程序源码) 大家好,今天给大家介绍基于javaEE+原生servlet+tomcat的教师工资管理系统设计与实 ...
- java进销存系统静态页_java 库存 进销存 商户 多用户管理系统 SSM springmvc 项目源码...
系统介绍: 1.系统采用主流的 SSM 框架 jsp JSTL bootstrap html5 (PC浏览器使用) 2.springmvc +spring4.3.7+ mybaits3.3 SSM ...
最新文章
- c语言 python-C语言、Java语言和python语言的区别在哪里
- Python 序列化 pickle/cPickle模块
- leetcode 449. Serialize and Deserialize BST | 449. 序列化和反序列化二叉搜索树(BST后序遍历性质)
- 微博拟全球发售1100万股 发售价不超388港元
- Python之网络编程(socket基础)
- Akka边学边写(3)-- ByteString介绍
- DES加密/解密类。
- Millumin for Mac视频实时编辑软件
- nginx 过滤post报文 防火墙_详解nginx限制IP恶意调用短信接口处理方法
- EXCEL图表之瀑布图
- 策略模式探究(二)多个门禁对接使用策略模式
- 空间直线与球面的相交算法
- contents()与children()的用法和区别
- OpenCV检测手指个数
- 小米系硬件查询保修日期
- 数据仓库系列(一)什么是维度建模以及维度建模的基本要素
- 揭秘团队业绩不好的原因
- QT 绘图工具-QCustomPlot
- 蚂蚁客服介绍-微服网络
- 小程序源码:实用的智力测试智商提升