标签:打开 tcl ima inpu list buffer set copy word
第一步:先确定好Excel导入的格式以及各表格字段值的含义
第二步:定义好解析的XML--videoConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <workbook> <worksheet name="Sheet1"> <section startRow="0" endRow="0"/> <loop startRow="1" endRow="1" items="videoInfoList" var="videoInfo" varType="com.iflytek.weike.job.bo.VideoInfo"> <section startRow="1" endRow="1"> <mapping row="1" col="0">videoInfo.index</mapping> <mapping row="1" col="1">videoInfo.videoName</mapping> <mapping row="1" col="2">videoInfo.resourceId</mapping> <mapping row="1" col="3">videoInfo.upload</mapping> <mapping row="1" col="4">videoInfo.content</mapping> <mapping row="1" col="5">videoInfo.schoolName</mapping> </section> <loopbreakcondition> <rowcheck offset="0"> <cellcheck offset="0"></cellcheck> </rowcheck> </loopbreakcondition> </loop> </worksheet> </workbook>
第三步:生成一下解析的实体类VideoInfo(这个需要根据excel文件的列去手工写一个)
public class VideoInfo { //序号 private int index; //视频名称(全称) private String videoName; //视频资源ID private String resourceId; //上传者 private String upload; //课程说明 private String content; //学校名称 private String schoolName; public VideoInfo() { } public VideoInfo(int index, String videoName, String resourceId, String upload, String content, String schoolName) { super(); this.index = index; this.videoName = videoName; this.resourceId = resourceId; this.upload = upload; this.content = content; this.schoolName = schoolName; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } public String getVideoName() { return videoName; } public void setVideoName(String videoName) { this.videoName = videoName; } public String getResourceId() { return resourceId; } public void setResourceId(String resourceId) { this.resourceId = resourceId; } public String getUpload() { return upload; } public void setUpload(String upload) { this.upload = upload; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getSchoolName() { return schoolName; } public void setSchoolName(String schoolName) { this.schoolName = schoolName; } @Override public String toString() { return "VideoInfo [index=" + index + ", videoName=" + videoName + ", resourceId=" + resourceId + ", upload=" + upload + ", content=" + content + ", schoolName=" + schoolName + "]"; } }
第四步:添加jxls的jar包,我这里项目用maven管理jar包的版本是1.0.6大家可以去下面这个maven资源库下 载jar包 maven资源库地址:http://mvnrepository.com/open-source/excel-libraries;
第五步:windows弹框选择文件并解析Excel数据,这个windows文件框选择文件我以前还是真没做过在网上 找了一个很好用的方法请看代码:
其中有个静态变量我是统一写在配置类中的:
public static String XML_CONFIG ="videoConfig.xml";
第六步:写一个main函数执行我们写好的方法试一下
其中SyncDataServiceImpl类是我把前面二个方法写到这个类里面了,里面还有一些其他的业务处理逻辑,就不贴上来了, new SyncDataServiceImpl()对象就可以调用刚才的方法了!
下面的运行截图:
运行结果截图,导入Excel成功:
相比较POI来读取Excel数据个人觉得jxls用起来还是更方便一点!同时jxls导出Excel也是比较方便的,有自己的标签类似JSTL,以后有时间再写一篇吧!希望能帮到需要的人,哈哈!有写的不对的希望高手可以指点一下!谢谢!
标签:打开 tcl ima inpu list buffer set copy word
原文地址:http://www.cnblogs.com/pangpanghuan/p/6401968.html