且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

excel导入导出组件设计

更新时间:2022-03-31 01:05:09

这是我设计的excel导入导出组件,使用Java 编写,通过xml文件的配置,来设置excel到java bean的映射。关键(配置)文件:ExcelModeMappingl.xml。

之前一直为excel的导入导出烦恼,每写一次都感觉是“重复造***”,代码很繁琐,可移植性特别差。终于下决心写一个可重用性很高的组件,专门解决“重复造***”的问题。

 

xml配置文件名称:ExcelModeMappingl.xml

内容(示例):

<?xml version="1.0" encoding="UTF-8" ?>

<models>

     <model id="deptModel" class="com.test.excel.jn.model.DeptModel"<!--[if !supportAnnotations]-->[huangwei1]<!--[endif]--> >

         <property name<!--[if !supportAnnotations]-->[huangwei2]<!--[endif]--> ="deptName" column="1" excelTitleName="部门名称" dataType="String" maxLength="15"  default=""/>

         <property name="deptCode" column="2" excelTitleName<!--[if !supportAnnotations]-->[huangwei3]<!--[endif]--> ="部门编号" dataType="String" maxLength="15"  default="abc"/>

         <property name="sendFileName" column="4" excelTitleName="发文简称"dataType="String" maxLength="15" isConvertable="true"  default<!--[if !supportAnnotations]-->[huangwei4]<!--[endif]--> ="C">

         <map>

              <entry excel_key="永久" bean_value="c"></entry>

              <entry excel_key="长期1" bean_value="send_1"></entry>

              <entry excel_key="长期2" bean_value="send_2"></entry>

              <entry excel_key="长期3" bean_value="send_3"></entry>

              <entry excel_key="长期4" bean_value="send_4"></entry>

              <entry excel_key="长期5" bean_value="send_5"></entry>

         </map>

         </property>

         <property name="sendDate" column="3" excelTitleName="时间" dataType="Date"format="yyyyMMdd HH:mm:ss" maxLength="15"  default="10"/>

     </model>

    

</models>

这个文件有两个作用,(1)用于有excel文件转化为beans;(2)用于把beans转化为excel

<!--[if !supportLists]-->(1)    <!--[endif]--> default只有在excel转为java beans才有效;在java beans转化为excel时无效。

excel中的单元格中无内容,则采用default指定的值;

<!--[if !supportLists]-->(2)     <!--[endif]-->column只有在从java bean导出excel时才有效,表示excel中列(表头)的顺序,取值范围为[1,2,3……]

<!--[if !supportLists]-->(3)    <!--[endif]-->dataTypeDate,则多出一个属性format,例如format=”yyyy-MM-dd”;导入导出excel时都有效

<!--[if !supportLists]-->(4)    <!--[endif]-->isConvertable的值为true,则有map节点,否则则不会读取xml 配置文件中的map节点;

<!--[if !supportLists]-->(5)    <!--[endif]-->map说明:excel_key 表示excel单元格中的值,而bean_value则表示java bean中对应属性的值。

<!--[if !supportLists]-->(6)    <!--[endif]-->dataType只允许有两种取值[Date, String];

<!--[if !supportLists]-->(7)    <!--[endif]-->此配置文件采用UTF-8编码;

 

xml配置文件 各标签的说明

标签名称

含义

示例

model

定义一个java beanexcel的映射(mapping

 

class

model的属性,必须的。指定java bean的类路径

例如com.kunlunsoft.model.Student

property

java bean中的成员变量

 

name

property标签的属性,指定java bean的成员变量名称

 

excelTitleName

excel文件中表头的名称,与name是一一映射的。

 

dataType

数据类型,分为String Date,目前就这两种,int,float统一归于String类型

 

format

如果类型是Date,则可以指定该属性,用于日期的格式化

format="yyyyMMddHH:mm:ss"

maxLength

预留的,目前没有使用

 

default

默认值。excel文件转化为java bean时,excel单元格为空,则设置java bean对应属性值为该默认值

 

isConvertable

是否需要转化。比如java bean中的值为[yes,no],而对应的excel中的值为[是,否],这就需要使用一个map来进行映射

<map><entry excel_key="" bean_value="yes"></entry>

                            <entry excel_key="" bean_value="no"></entry></map>

 

 

 

 

 

 

 

 

 

 

 

 

<!--[if !supportAnnotations]-->
<!--[endif]-->
<!--[if !supportAnnotations]-->
<!--[endif]--><!--[if !supportAnnotations]--><!--[endif]-->

 <!--[if !supportAnnotations]-->[huangwei1]<!--[endif]-->这是实体类

<!--[if !supportAnnotations]-->
<!--[endif]-->
<!--[if !supportAnnotations]-->
<!--[endif]--><!--[if !supportAnnotations]--><!--[endif]-->

 <!--[if !supportAnnotations]-->[huangwei2]<!--[endif]-->实体类中的属性名称

<!--[if !supportAnnotations]-->
<!--[endif]-->
<!--[if !supportAnnotations]-->
<!--[endif]--><!--[if !supportAnnotations]--><!--[endif]-->

 <!--[if !supportAnnotations]-->[huangwei3]<!--[endif]-->excel文件中的表头名称

<!--[if !supportAnnotations]-->
<!--[endif]-->
<!--[if !supportAnnotations]-->
<!--[endif]--><!--[if !supportAnnotations]--><!--[endif]-->

 <!--[if !supportAnnotations]-->[huangwei4]<!--[endif]-->excel中的单元格为空,则转化为java bean时,采用默认值

 

详见附件。

<!--[if !supportAnnotations]-->
<!--[endif]-->