`
xiaoxiaokuang
  • 浏览: 16658 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

Annotation Excel

    博客分类:
  • file
阅读更多
最近在开发一些通用的excel数据导入的功能,由于涉及到导入的模块很多,所以开发了一个比较通用的excel导入模板类文件。并且使用annotation作为验证数据的配置。

01 package com.hp.dylan.jv; 

02   

03 import java.lang.annotation.ElementType; 

04 import java.lang.annotation.Retention; 

05 import java.lang.annotation.RetentionPolicy; 

06 import java.lang.annotation.Target; 

07   

08 @Retention(RetentionPolicy.RUNTIME) 

09 @Target(ElementType.FIELD) 

10 public @interface ExcelAnnotation { 

11       

12 //excel column name 

13     public String importCellName(); 

14   

15 //excel cell data weather is required 

16     public boolean isRequired(); 

17   

18 //excel cell data datalength.    

19     public int importDataLen(); 

20   

21 //excel cell data type.  

22     public String importDataType(); 

23       

24 //excel cell mapping index. 

25     public int importCellIndex(); 

26   

27 //excel data weather eixts in db.    

28     public boolean isEixtance(); 

29 }
1 看看需要导入excel的模型类的定义:(AdditionJvImportModel)
01 package com.hp.dylan.jv; 

02 public class AdditionJvImportModel { 

03  @ExcelAnnotation( importCellName = "EN1",isRequired=true ,importDataLen=100,importDataType="String",importCellIndex = 1,isEixtance=true) 

04  private String sellEntity; 

05  @ExcelAnnotation(importCellName = "SE2",isRequired=false ,importDataLen=4,importDataType="String",importCellIndex =2, isEixtance=false) 

06  private String sellSe; 

07  @ExcelAnnotation(importCellName = "DI3",isRequired=false ,importDataLen=10,importDataType="String",importCellIndex = 3, isEixtance=false) 

08  private String sellDi; 

09  @ExcelAnnotation(importCellName = "DEPT4",isRequired=false ,importDataLen=8,importDataType="String",importCellIndex = 4, isEixtance=false) 

10  private String sellDept; 

11  @ExcelAnnotation(importCellName = "SF5",isRequired=false ,importDataLen=10,importDataType="String",importCellIndex = 5, isEixtance=false) 

12  private String sellSf; 

13  @ExcelAnnotation(importCellName = "PT6",isRequired=false ,importDataLen=4,importDataType="String",importCellIndex = 6, isEixtance=false) 

14  private String sellPt; 

15  @ExcelAnnotation(importCellName = "PL7",isRequired=false ,importDataLen=10,importDataType="String",importCellIndex = 7, isEixtance=false) 

16  private String sellPl; 

17  @ExcelAnnotation(importCellName = "SL8",isRequired=false ,importDataLen=4,importDataType="String",importCellIndex = 8, isEixtance=false) 

18  private String sellSl; 

19  @ExcelAnnotation(importCellName = "Amount9",isRequired=false ,importDataLen=25,importDataType="number(25,10)",importCellIndex = 9, isEixtance=false) 

20  private String sellAmout; 

21  @ExcelAnnotation(importCellName = "EN10",isRequired=true ,importDataLen=100,importDataType="String",importCellIndex = 10, isEixtance=true) 

22  private String buyEntity; 

23  @ExcelAnnotation(importCellName = "SE11",isRequired=false ,importDataLen=4,importDataType="String",importCellIndex = 11, isEixtance=false) 

24  private String buySe; 

25  @ExcelAnnotation(importCellName = "DI12",isRequired=false ,importDataLen=10,importDataType="String",importCellIndex = 12, isEixtance=false) 

26  private String buyDi; 

27  @ExcelAnnotation(importCellName = "DEPT13",isRequired=false ,importDataLen=8,importDataType="String",importCellIndex = 13, isEixtance=false) 

28  private String buyDept; 

29  @ExcelAnnotation(importCellName = "SF14",isRequired=false ,importDataLen=10,importDataType="String",importCellIndex = 14, isEixtance=false) 

30  private String buySf; 

31  @ExcelAnnotation(importCellName = "PT15",isRequired=false ,importDataLen=4,importDataType="String",importCellIndex = 15, isEixtance=false) 

32  private String buyPt; 

33  @ExcelAnnotation(importCellName = "PL16",isRequired=false ,importDataLen=10,importDataType="String",importCellIndex = 16, isEixtance=false) 

34  private String buyPl; 

35  @ExcelAnnotation(importCellName = "SL17",isRequired=false ,importDataLen=4,importDataType="String",importCellIndex = 17, isEixtance=false) 

36  private String buySl; 

37  @ExcelAnnotation(importCellName = "LSA Factor19",isRequired=false ,importDataLen=25,importDataType="number(25,10)",importCellIndex = 19, isEixtance=false) 

38  private String tranFactor; 

39  @ExcelAnnotation(importCellName = "GrossICPurchase20",isRequired=false ,importDataLen=25,importDataType="number(25,10)",importCellIndex = 20, isEixtance=false) 

40  private String tranGross; 

41  @ExcelAnnotation(importCellName = "EX Code21",isRequired=false ,importDataLen=5,importDataType="String",importCellIndex = 21, isEixtance=false) 

42  private String tranExCode; 

43  @ExcelAnnotation(importCellName = "Transaction Description24",isRequired=false ,importDataLen=200,importDataType="String",importCellIndex = 24, isEixtance=false) 

44  private String tranDesr; 

45  @ExcelAnnotation(importCellName = "Line Comments25",isRequired=false ,importDataLen=200,importDataType="String",importCellIndex = 25, isEixtance=false) 

46  private String tranLineComm; 

47  @ExcelAnnotation(importCellName = "Additional Line Comments30",isRequired=false ,importDataLen=200,importDataType="String",importCellIndex = 30, isEixtance=false) 

48  private String tranAddLinComm; 

49  @ExcelAnnotation(importCellName = "FROM VATID31",isRequired=false ,importDataLen=6,importDataType="String",importCellIndex = 31, isEixtance=false) 

50  private String tranFVatId; 

51  @ExcelAnnotation(importCellName = "TO VATID32",isRequired=false ,importDataLen=6,importDataType="String",importCellIndex = 32, isEixtance=false) 

52  private String tranTVatId; 

53 }
1 excel模型的解析中如何获取到此模型的描述配置。也就是获取他的annotation。
1 Map<Integer,ExcelAnnotation> mapping=new HashMap<Integer,ExcelAnnotation>(); 

2    for (int i = 0; i < filed.length; i++) { 

3     Field f = filed[i]; 

4     ExcelAnnotation excel = f.getAnnotation(ExcelAnnotation.class); 

5     if (excel != null) { 

6      mapping.put(excel.importCellIndex(), excel); 

7     } 

8    }
1 如何根据annotation的描述来验证。
1 if(anno.isRequired()){ 

2        if(cellValue==null||"".equals(cellValue.trim())){ 

3             sb.append(anno.importCellName()+" can not be empty!"); 。。。} 

4      }
1 如果需要跟db交互来进行检验的话,只需要定义一个abstract校验方法,具体的校验规则交给其实现者来完成
1 public abstract String validateExist(String value);
Noted:

你可以annotation完全的看做一种配置,你只需要获取你的model种的annotation标识,然后根据标识来进行一个校验规则的解析

你也可以结合 class reflection 机制来使用annotation,获取到model的annotation描述之后,根据class reflection 来动态的激活调用一些model中的方法,
分享到:
评论

相关推荐

    EXCEL所需要的jar包

    EXCEL所需要的jar包我们做项目,开发项目中经常会用到了

    Annotation--学习:限定使用、文档、继承

    NULL 博文链接:https://lansky07.iteye.com/blog/299893

    EXCEL_VBA编程24学时教程 with annotation

    EXCEL_VBA编程24学时教程 第1学时 Visual Basic for Application 第2学时处理录制的宏

    alibaba_easyexcel.rar

    Java操作excel的一个开箱即用的小demo

    基于poi Excel导出

    import org.xlp.annotation.ExcelOutputed; public class Student { @ExcelOutputed private int id; @ExcelOutputed("姓名") private String name; @ExcelOutputed("生日") private Date timDate; @...

    xlsmapper:将Excel工作表映射到JavaBean(POJO)的库

    XlsMapper是Java库,用于将Excel工作表映射到POJO。 被许可人 Apache License Verion 2.0 依靠 Java1.8的 Apache POI v3.17 SpringFramework 3.0+(可选) BeanValidation 1.0 / 1.1 / 2.0(可选) 设置 为...

    report-annotation:用于报告物化的 Java bean 注释

    所以我设计了注释机制来将 Java bean 填充为三种流行的文件格式,即 CSV、Excel 和 PDF。 安装 使用 Maven 构建在 Spring Boot 框架上运行的报告注释。 获取代码,然后运行 ​​mvn package。 Java版本1.7.X与Maven...

    testcase-annotation:我们可以使用@testcase为Java方法编写测试用例,并将其生成为一个excel和html。

    在传统的测试过程中,我们熟悉先在excel中编写测试用例,然后再逐个实现测试用例的自动化。 最后,我们将Java文档添加到每个测试方法中。 问题是,当我们在excel中更改测试用例时,我们应该找到相关的测试脚本在...

    一个支持annotation的SSH整合示例项目

    卷 (USB)Project 的文件夹 PATH 列表 卷序列号码为 0006EE44 CCBE:F425 I:. │ .project │ pom.xml │ pom.xml~ │ text.txt │ ├─.settings │ org.maven.ide.eclipse.prefs ...│ │ org.eclipse.wst.common....

    SEG V0.0.3.3.7z

    rhino平台下插件,用于grasshopper平台的优秀插件,可用于rhino...5,修复SEG.DataExchange.ReadExcel,读写Excel不支持单个单元格等问题。 6,修复SEG.Table.CreateTable电池考虑汉字变窄问题。 7,其他一些bug的修复。

    Bracket Based Arabic Annotation-开源

    基于括号的阿拉伯语注释 (B2A2) 方案使用户能够使用词性 (POS) 标记手动标记阿拉伯语文本。 B2A2 引入了一种新方法,可以使用形态感知标签标记来标记阿拉伯... 这些结果可以导出到 excel 或文本文件以供进一步处理。

    zerocell:简单,高效的Excel to Java POJO库

    ZeroCell提供了一个简单的API,可使用注释将Excel表格中的列映射到Java类中的字段,从而将数据从Excel表格加载到Plain Old Java Object(POJO)中。 如果您不喜欢注释或不想更改现有的类,则可以将列映射到没有注释...

    easypoi工具包-其他

    默认值多,write less do more4.AbstractView 支持,web导出可以简单明了几个工程的说明:1.easypoi 父包--作用大家都懂得2.easypoi-annotation 基础注解包,作用与实体对象上,拆分后方便maven多工程的依赖管理3....

    框架整合jar包及其它功能包_spring4.2.3+hibernate5.0.2+struts2.3.24

    ehcache二级缓存,c3p0连接池,文件上传,dom4j,mysql数据库驱动,jscharts图表统计图走势,JSTL,struts日历控件包,base64加密,Excel文件生成,邮件发送,log4j以及log4j配置文件,mysql数据库连接Properties...

    POI、EAYSPOI所有包依赖、冲突(高版4.X)

    本地MacOS及WIndows通过JAR运行都没问题,上传到生产CENTOS+WAR包运行的时候,导出或导入EXCEL会有各种错误。该资源包含所有POI相关包及依赖,主要有: commons-compress-1.18.jar、 easypoi-annotation-4.1.3.jar、...

    M_hirsutus:沙雷氏Maconellicoccus hirsutus-基因组学

    M_hirsutus hironus Maconellicoccus hirsutus(M....augustus_QCed_transcripts_blobs.fa) 预测基因的注释-22,058个转录本中的17,047个转录本的Blast注释(Excel File:Annotation_mhirsutus_24032021)

    java web 开发详解

    Java 的 Web框架虽然各不相同,但基本也都是遵循特定的路数的:使用Servlet或者Filter拦截请求,使用MVC的思想设计架构,使用约定,XML或 Annotation实现配置,运用Java面向对象的特点,面向抽象实现请求和响应的...

    leetcode数组下标大于间距-leetcode:leetcode刷热题

    Annotation 目录 LeetCodeEasy1 350 两个数组的交集2 237 删除链表中的节点 104.二叉树的最大深度 108.将有序数组转换为二叉搜索树 344.反转字符串 206.反转链表 136.只出现一次的数字 191.位1的个数 171.Excel表列...

    ArcGIS开发教程

    1.11.2. 如何将数据输出到Excel 209 1.11.3. 如何把Labels转换为Annotation 211 1.11.4. 如何把Annotation转换为Polygon Features 215 1.11.5. 如何设置Featurelayer的Label 218 1.11.6. 如何设置图层显示的透明度 ...

    java开发常用jar包

    Annotation 方式实现 AOP commons-dbcp.jar commons-pool-1.2.jar DBCP数据库连接池 cglib-nodep-2.1_3.jar CGLIB是一个强大的高质量高性能的代码生成库,在运行时可以用它来扩展Java类 jfreechart-1.0.12.jar ...

Global site tag (gtag.js) - Google Analytics