第一步:先确定好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文件框选择文件我以前还是真没做过在网上               找了一个很好用的方法请看代码:

  1. /**
  2. * 打开文件选择窗口选择导入文件
  3. * @return 返回文件路径
  4. * @throws Exception
  5. */
  6. public String getExcelPath() throws Exception{
  7. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  8. JFileChooser jFileChooser=new JFileChooser();
  9. int i = jFileChooser.showOpenDialog(null);
  10. if(i== jFileChooser.APPROVE_OPTION){ //打开文件
  11. String path = jFileChooser.getSelectedFile().getAbsolutePath();
  12. String fileName = jFileChooser.getSelectedFile().getName();
  13. String extName =fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
  14. System.out.println("当前文件路径:"+path+";\n当前文件名:"+fileName+";\n当前文件扩展名:"+extName);
  15. if(null!=extName&&"xlsx".equals(extName)){
  16. return path;
  17. }else{
  18. System.out.println("您好,只能导入扩展名为xlsx的Excel文件!");
  19. return null;
  20. }
  21. }else{
  22. System.out.println("没有选中文件");
  23. return null;
  24. }
  25. }
  26. /**
  27. * 使用jxls解析导入的Excel
  28. * @param path 导入文件路径
  29. * @return List<VideoInfo> 导入对象集合
  30. */
  31. public List<VideoInfo> getExcelDataForVideoInfo(String path){
  32. List<VideoInfo> videoInfoList = new ArrayList<VideoInfo>();
  33. try {
  34. InputStream inputXML = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream(ConsForSystem.XML_CONFIG));
  35. XLSReader mainReader = ReaderBuilder.buildFromXML( inputXML );
  36. InputStream inputXLS = new BufferedInputStream(new FileInputStream(new File(path)));
  37. VideoInfo videoInfo = new VideoInfo();
  38. Map<String,Object> beans = new HashMap<String,Object>();
  39. beans.put("videoInfo", videoInfo);
  40. beans.put("videoInfoList", videoInfoList);
  41. XLSReadStatus readStatus = mainReader.read( inputXLS, beans);
  42. if(readStatus.isStatusOK()){
  43. System.out.println("jxls读取Excel成功!");
  44. }
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. }
  48. return videoInfoList;
  49. }

其中有个静态变量我是统一写在配置类中的:

public static String XML_CONFIG ="videoConfig.xml";

第六步:写一个main函数执行我们写好的方法试一下

  1. public class Test {
  2. public static void main(String[] args) {
  3. SyncDataServiceImpl syncDataService = new SyncDataServiceImpl();
  4. try {
  5. String filePath = syncDataService.getExcelPath();
  6. if(null!=filePath&&StringUtils.isNotBlank(filePath)){
  7. //导入Excel文件解析信息获取资源id
  8. List<VideoInfo> infoList = syncDataService.getExcelDataForVideoInfo(filePath);
  9. System.out.println("infoList大小==="+infoList.size());
  10. for(VideoInfo video:infoList){
  11. System.out.println("打印ideoInfo详细信息======"+video.toString());
  12. }
  13. }
  14. } catch (Exception e) {
  15. e.printStackTrace();
  16. }
  17. }
  18. }

其中SyncDataServiceImpl类是我把前面二个方法写到这个类里面了,里面还有一些其他的业务处理逻辑,就不贴上来了, new SyncDataServiceImpl()对象就可以调用刚才的方法了!

下面的运行截图:

运行结果截图,导入Excel成功:


 
相比较POI来读取Excel数据个人觉得jxls用起来还是更方便一点!同时jxls导出Excel也是比较方便的,有自己的标签类似JSTL,以后有时间再写一篇吧!希望能帮到需要的人,哈哈!有写的不对的希望高手可以指点一下!谢谢!

使用jxls技术导入Excel模版数据(转自其他博客)的更多相关文章

  1. springMVC(5)---导入excel文件数据到数据库

    springMVC(5)---导入excel文件数据到数据库 上一篇文章写了从数据库导出数据到excel文件,这篇文章悄悄相反,写的是导入excel文件数据到数据库.上一篇链接:springMVC(4 ...

  2. 使用Javascript/jQuery将javascript对象转换为json格式数据 - 海涛的CSDN博客 - 博客频道 - CSDN.NET

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  3. Vue3实现动态导入Excel表格数据

    1.  前言 在开发工作过程中,我们会遇到各种各样的表格数据导入,大部分我们的解决方案:提供一个模板前端进行下载,然后按照这个模板要求进行数据填充,最后上传导入,这是其中一种解决方案.个人认为还有另外 ...

  4. 向MySql数据库导入excel表数据

    最近要开发一个小的答题系统,如果题目人工录入那确实很麻烦.所以想到是不是可以从用一些现有数据格式的文件导入数据.在网上查了一下,看到有关于将excel的数据导入到mysql的方法.所以将题库数据整理成 ...

  5. 向SQL Server中导入Excel的数据

    1.  手动界面导入Excel数据 同 https://jingyan.baidu.com/article/ce09321b9a0e252bff858ff9.html 首先打开并登陆sql serve ...

  6. java导入Excel表格数据

    首先导入Excel数据需要几样东西 第一需要两个依赖包,这里直接是在pom注入依赖 <!--excel--> <dependency> <groupId>org.a ...

  7. 利用Metaweblog技术的API接口同步到多个博客网站(详细)

    很早就有这个想法:自己有时候会用到多个博客,有些博客在一个网站上写完之后,要同步到其他博客网站,自己只能复制粘贴,感觉特别没意思,复制粘贴的麻木了.一直在想有哪些技术能实现一次写博,多站同步.最近网上 ...

  8. Sql server2008如何导入Excel文件数据?

    sql server 中如何使用Excel文件导入数据?我做个测试,首先建立一个测试表(民族表) --创建一个民族表-- create table BdsNation( Uid int not nul ...

  9. java后端导入excel将数据写入数据库

    参考:https://www.cnblogs.com/hanfeihanfei/p/7079210.html @RequestMapping("/importExcel.do") ...

随机推荐

  1. Math.round、Math.floor、Math.ceil 区别

    1.Math.round() 按照四舍五入的方式返回值 例如:Math.round(9.5)=10    Math.round(9.4)=9 2.Math.floor()返回最小整数 例如:Math. ...

  2. 【温故知新】C#基于事件的异步模式(EAP)

    在开发winform和调用asp.net的web service引用的时候,会出现许多命名为 MethodNameAsync 的方法. 例如: winform的按钮点击 this.button1.Cl ...

  3. typescript -- ts

    算是强类型语言,javascrpt是弱类型语言,是指对数据的类型的处理,弱类型语言的特点有时候只是在支行的时候才告诉你出错了,但写的时候你是查觉不到的 ts也是以es5-7为语法标准的,开发的算是另外 ...

  4. C# CultureInfo中常用的InvariantCulture

    本文参考自CultureInfo中重要的InvariantCulture,纯属读书笔记,加深记忆 1.CultureInfo的InvariantCulture的作用 (1).CultureInfo使整 ...

  5. IDEA里运行代码时出现Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger的解决办法(图文详解)

    不多说,直接上干货! 问题详情 运行出现log4j的问题 -classpath "C:\Program Files\Java\jdk1.8.0_66\jre\lib\charsets.jar ...

  6. php.ini配置max_execution_time和FPM配置request_terminate_timeout

    PHP限定脚本执行时长的方式有几种,下面说下php.ini中的max_execution_time和php-fpm.conf中的request_terminate_timeout 1. php.ini ...

  7. EveryThing 使用方法

    1.按照时间范围:datetimed: <*.doc|*.docx>  dm:1/2015-6/2015// 如果本地的时间设置是年份在前,那么把年份放在月份前面// d: <*.d ...

  8. C#实体对象序列化成Json,格式化,并让字段的首字母小写

    解决办法有两种:第一种:使用对象的字段属性设置JsonProperty来实现(不推荐,因为需要手动的修改每个字段的属性) public class UserInfo { [JsonProperty(& ...

  9. Expression Blend实例中文教程(9) - 行为快速入门Behaviors

    在Blend强大的设计功能支持下,设计人员和开发人员可以无代码实现Silverlight/WPF动画效果,例如上文介绍的StoryBoard,就是一个典型例子,设计人员和开发人员仅需提供必要元素,即可 ...

  10. css常用左右布局方案整理

     实际项目开发过程中我们经常会遇到页面div左右布局的需求:左侧 div 固定宽度,右侧 div 自适应宽度,填充满剩余页面,下面整理几种常用的方案  1 左侧 div 设置 float 属性为 le ...