1 Star 0 Fork 85

lovememo / easy-excel

forked from lis1314 / easy-excel 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

通用文件导入说明 v0.8

暂时仅仅实现了excel解析

一、模板说明

src下的import_template_config.xml配置文件中,<templates>节点下可以配置多个excel文件的模板解析规则配置,说明如下:

  • <template>定义了一个解析模板,针对excel仅仅支持一个sheet(可以指定sheet页面),有如下属性:
    • templateCode为模板编码,唯一定义一个模板。excel建议以excel0001顺序向下编码
    • name 模板名称
    • fileType 解析的文件类型,针对EXCEL,只要填写“EXCEL"即可。更多后续支持的类型可以参考FileType
  • <template>定义可选属性节点<optional>
    • class属性指定为ExcelTemplateOptional即可
    • <sheetIndex>指定解析的excel sheet页,从0开始计数。
  • <sections>下的<section>节点指定了一个sheet所需要解析的区域/范围。可指定多个<section>。这里主要考虑到的情况是,通常情况下,一个sheet可以解析其数据头(行数确定),数据体(行数不确定),数据尾(行数确定)。而<section>就是针对这些情况,定义了一个解析的规则,整体设计思路可以参考下图: <section>有如下属性
    • sectionIndex指定了本section在所有sections中的相对位置。默认值为0.

    • sectionType定义了section的类型,可填写HEADER BODY FOOTER,默认值为BODY。 具体定义参考SectionType类。

    • parseType定义了解析类型,针对HEADER FOOTER指定为FIXED即可。针对BODY指定为DYNAMIC即可。默认值为DYNAMIC

    • direction定义了解析方向,HEADER填1,BODY填0,FOOTER填写-1。默认值为0。

    • batchSize定义了数据处理每个批次的记录数,默认值为2000。比如excel解析出来10条数据,batchSize设置为4,则解析器每解析出来4条数据就进行处理一次,一共处理三次(最后一次处理2条数据记录)。

      定义该值是为了控制内存大小,防止OutOfMemory异常。

    • <startRowIndex> 解析起始行,从0开始计数

    • <startColIndex> 解析起始列,从0开始计数

    • <endRowIndex> 解析结束列,从0开始,为负数。-1表示倒数第一行,该参数仅仅针对FOOTER和BODY有效

    • <rowCount> 解析的行数,仅对HEADER和FOOTER有用

    • <colCount> 解析的列数

    • <fields>下的<field>属性定义了解析的字段。

      • fieldIndex定义了字段顺序,从0开始,顺序加1
      • name定义了字段名,后续数据处理以name的值作为字段名称。
      • description定义了字段说明,一般为中文说明
      • validator 定义了字段校验器,详见校验规则定义说明。
      • filterError默认为true。配合validator使用。当该字段解析出来的结果通过validator校验验证没有通过且同时filterError为false,则该数据还是会出现在解析结果中。如果校验结果没有通过,而filterError为true,则该字段的值将不会在后续handler中被处理到。
      • <relativeRowPos>相对section的行数(不是sheet中的行数),仅对HEADER 和 FOOTER有作用
      • <relativeColumnPos>相对section的列数(不是sheet中的列数),BODY中可不配置,程序将读取fieldIndex属性作为相对列数。
    • <handlers>下的<handler>定义了数据处理器集合。

      • class 定义了handler类名(包含包名)
      • handlerIndex 定了多个handler相对位置,程序按此字段排序顺序将数据交由handlers处理。

      注意,此处定义的handler都需要实现IFileHandler接口。

      handler的说明:每个section解析出来的数据是按批次交由handlers处理的。程序会顺序调用各个handler。每个handler的输入为前一个handler的输出(即DataContext对象)。

      handler 中定义了对数据的最终处理,比如字典转换,保存到数据库等操作。

二、校验规则定义

总体说明

在解析模板配置定义中的<field>节点有个validator属性,各开发人员可以自行指定解析规则。

  • 每个解析规则以半角分号分隔“”。
  • 各个规则也有自己的参数,规则项和参数之间以半角冒号“:”分隔。
  • 如果有多个参数,各个参数之间要以半角逗号","分隔。

举个例子:

validator="isNullAble:false;maxAsciiLength:10;date:yyyy-MM-dd"

上述规则表示该字段为:

  1. 必输项
  2. 日期类型,日期格式为yyyy-MM-dd
  3. 长度最大为10

校验规则详细说明

  1. date 日期校验,参数为日期格式,遵循java的日期pattern规则
  2. dict 字典,比如 "dict:1,2,3"表示输入的值必须为1或者2或者3
  3. digit 由0-9组成的字符串
  4. isNullAble 是否必输
  5. maxAsciiLength 最大长度,一个全角字符的长度按3处理
  6. mustBe 指定值,比如 mustBe:上海表示传入值必须为“上海”
  7. noCheck 不校验,与其他规则项配合时,本规则项自动失效
  8. number 数字,举例如下:
    • number:5,2,>=100 表示整数位置最大位数为5-2=3,小数位最大位数为2,该数字要大于等于100。比较操作符支持>=, >, <=, < , ==
    • number:5,2 表示整数位置最大位数为5-2=3,小数位最大位数为2
    • number:5表示整数位置最大位数为5
  9. regex 正则校验器,参数有两个,一个是正则表达式,另一个是报错信息。举例如下:“regex:[0-9]+,必须为数字编码

    该正则校验器不支持表达式中包含分号,逗号,冒号。如需更复杂逻辑可参考自定义扩展

  10. weakId 身份证校验器,该校验器没有校验身份证最后一位校验码。
  11. weakPhone 11位数字即认为合法手机号

约定:除了isNullAble及mustBe,否则其他校验规则在读取到的校验对象为空时,认为校验通过。

自定义扩展

校验规则项可以自定义扩展,步骤如下:

  1. 定义规则项名称,比如“email”定义参数为company,表示邮箱服务器的公司,可以为空。我们期望的结果是
    • 校验输入字段一定是email格式;
    • 如果指定了公司名,比如“email:sina”表示一定是新浪公司的邮箱名
  2. 开发规则相对应的类,命名格式为“xxx.xxx.data.validate.impl.ValidatorItemEmail",注意红色部分和我们在第1步中定义的规则项名称是一致的(首字母变大写),该类一定要继承AbsTractValidatorItem。其中initParam方法为初始化规则参数。validate方法是针对输入的data进行校验。具体实现可参考其他校验项实现方式。
  3. 此外,如果需要实现的规则用正则表达式即可,可继承”ValidatorItemRegex“,具体可以参考ValidatorItemWeakId的实现方式。

三、调用流程

四、设计类图

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: You must give any other recipients of the Work or Derivative Works a copy of this License; and You must cause any modified files to carry prominent notices stating that You changed the files; and You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2016 lis1314 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

开发目的,省去复杂的excel代码 使用xml配置的形式完成导入导出 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/lovememo/easy-excel.git
git@gitee.com:lovememo/easy-excel.git
lovememo
easy-excel
easy-excel
master

搜索帮助