关于java读取Excel表格数据流出现的一系列报错问题的解决方案,含辛茹苦磨砺总结得出!

最近在实习,公司给安排了一个功能模块要实现,做的是自定义创建投票的的功能,里面就涉及了对评委和被评选人的一键导入的功能。想到这,当然是想到上传,获取Excel表中的数据即可,由于是新手,踩在这上面的坑可是一个又一个!!!痛苦至极啊,最后万幸,百度各路答案,最后给解决了,极度怀疑,我这次出现的问题几乎就是所有会犯错的问题!立贴总结,同时方便以后学习!

项目说明:ssh+maven+easyUI

目录

错误一、Excel导入什么错误都没报

错误二、java.lang.NoClassDefFoundError:org/apache/commons/collections4/ListValuedMap

错误三、Developer Notification (set struts.devMode to false to disable this message): Unexpected Excepti

错误四、java.lang.ClassNotFoundException: org.apache.poi.UnsupportedFileFormatException(Servlet.service() for servlet [springDispatcherServlet] in context with path [/mfile] threw exception [Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException] with root cause)

错误五、Cannot get a text value from a numeric cell

总结:所需的jar包,1/6版本没有要求,2,3,4,5版本要求一致即可,另附上

结语:这个Excel文件导入真的很锻炼找bug解决bug的能力,同时也要赶紧把myeclipse的调式功能掌握才可以,要不然效率就太低了,继续加油吧!


错误一、Excel导入什么错误都没报

起初用的是easyUI里面的文件上传的功能,在测试的过程中,发现什么错误都没有报,由于还不会调式,只能用最原始的方法加System.out.print打印来追踪代码的执行情况,但是就是不知道哪出了问题!最后想想有可能是框架的什么问题。于是乎将前端jsp那上传的按钮单独提取了出来,重新执行!

错误二、java.lang.NoClassDefFoundError:org/apache/commons/collections4/ListValuedMap

            经过检查发现缺了一个jar包:commons-collections4-4.1.jar,我之前有在其他的练习项目中学习到过上传Excel的功能,当时就是结合easyui来用的,当时并没有发现需要加这个包,当重新写的时候就报了这个错误,因此添加了上去。

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.1</version>
</dependency>

错误三、Developer Notification (set struts.devMode to false to disable this message): Unexpected Excepti

            因为之前是把上传文件的三要素写到了baseAction中,因此新开的时候忽略了名字不一致的问题,在新的地方要补上文件上传三要素:

private File excel; // 上传的EXCEL文件
private String excelFileName; // EXCEL文件名
private String excelContentType;

错误四、java.lang.ClassNotFoundException: org.apache.poi.UnsupportedFileFormatException(Servlet.service() for servlet [springDispatcherServlet] in context with path [/mfile] threw exception [Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException] with root cause)

          在程序运行XSSFWorkbook wb = new XSSFWorkbook(fis); //到这句出错,我查阅之后原来是有些jar包版本不一致而导致出现了这些问题,当我明白了这回事之后,改了,立马用回原来的demo,发现可以了!!!!可以成功运行了,就是jar包版本不一致的问题。

        <!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
		<dependency>
		    <groupId>org.apache.xmlbeans</groupId>
		    <artifactId>xmlbeans</artifactId>
		    <version>2.6.0</version>
		</dependency>
		 <!-- poi -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.9</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
		<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-ooxml</artifactId>
		    <version>3.9</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
		<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-ooxml-schemas</artifactId>
		    <version>3.9</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
		<dependency>
		    <groupId>org.apache.commons</groupId>
		    <artifactId>commons-collections4</artifactId>
		    <version>4.1</version>
		</dependency>

错误五、Cannot get a text value from a numeric cell

         原因就是,读取Excel表格中cell的格式与存储的格式不一致的问题,做类型处理即可,举个例子,例如电话号码

if(row.getCell(0)!=null){
     row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
     stuUser.setPhone(row.getCell(0).getStringCellValue());
}

总结:所需的jar包,1/5版本没有要求,2,3,4,版本要求一致即可,另附上


1.commons-collections4-4.1.jar
2.poi-ooxml-3.9.jar
3.poi-ooxml-schemas-3.9.jar   
4.poi-3.9.jar
5.xmlbeans-2.6.0.jar

查maven导入jar的好地址点击我即可跳转

结语:这个Excel文件导入真的很锻炼找bug解决bug的能力,同时也要赶紧把myeclipse的调式功能掌握才可以,要不然效率就太低了,继续加油吧!

猜你喜欢

转载自blog.csdn.net/weixin_40295575/article/details/81354099