poi-excel上传、下载问题总结

一、Excel 文件上传
0.0 功能实现
参考内容:
http://blog.csdn.net/u013871100/article/details/52901996

1.0问题描述

测试过程中,选择一个 Excel 文件上传,版本为 office 2007

1.2异常信息


Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:128)
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:112)
at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:302)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:86)


1.3异常解释

代码中用HSSFSheet 进行表单获取,以及使用HSSFXXX对行、列内容进行处理
而HSSFSheet 导入时只支持2007版本而不支持其他版本

1.4问题解决
参考内容:
http://blog.csdn.net/mmm333zzz/article/details/7962377

HSSFSheet 系列进行文件的处理,只能处理2007版本以下的
XSSFSheet 系列进行文件的处理,处理2007版本


2.0问题描述

测试过程中,获取的数据总数与已经总数不一致,即数据丢失

2.1问题解决
参考内容:
http://blog.csdn.net/u013871100/article/details/52901996

row / cell 取值从0 开始


3.0问题描述
上传文件中含有手机号码,对手机号码进行格式校验时异常,检查正则无误,跟踪代码发现接收的数据变成科学计数法

3.1问题解决
参考内容:
http://blog.csdn.net/cclovett/article/details/16343615
使用BigDecimal 对数据进行转化

参考内容:
http://jingyan.baidu.com/article/0964eca27a39808285f5363c.html
将模板中手机号码的单元格格式,设置为 数字--文本

4.0问题描述
读取文本内容时,出现异常

4.1异常描述

java.lang.IllegalStateException: Cannot get a text value from a numeric cell
at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:994) ~[poi-ooxml-3.14.jar:3.14]
at org.apache.poi.xssf.usermodel.XSSFCell.getRichStringCellValue(XSSFCell.java:399) ~[poi-ooxml-3.14.jar:3.14]
at org.apache.poi.xssf.usermodel.XSSFCell.getStringCellValue(XSSFCell.java:351) ~[poi-ooxml-3.14.jar:3.14]


4.2异常解释
试图将Number 转换为String
4.3异常解决
参考内容:
http://blog.csdn.net/ysughw/article/details/9288307
进行格式转化

5.0问题描述
取出Map的Values集合,并当做查询参数时异常
5.1异常描述

java.util.HashMap$Values cannot be cast to java.util.Set

5.2异常解决
遍历Map,组装Value


二、文件下载

0.0功能实现
参考内容:
http://meigesir.iteye.com/blog/1539358
http://www.cnblogs.com/ungshow/archive/2009/01/12/1374491.html

1.0问题描述
无法映射访问路径

1.1代码内容
	@RequestMapping("excelModelDown")
	public void excelModelDown(

//			@RequestParam(defaultValue="",required=false)
// 添加此行注释后,请求正常映射
			HttpServletResponse  response 
			
			){
		return ;
	}
// 在实现中重新定义response 依然无法解决问题
// 取消上述@RequestParam标签即可

2.0问题描述
无法获取放到src/main/resources下的文件
2.1问题解决
resources中的文件打包时也被放在classes目录下。使用this.getClass().getClassLoader().getResource("");
读取src/main/resources 下的文件
获取文件路径 this指代当前类

3.0问题描述

放在SRC的文件为英文名称,下载的文件欲换成中文名称
但更换名称后,下载的文件变为未知文件

注:
response.addHeader("Content-Disposition", "attachment;filename=" + new String(EXCEL_MODEL_CHINESE_NAME.getBytes("gb2312"), "ISO8859-1" )+"."+ext.toLowerCase());

若文件名称不变,则设置"fileName = "+new String(currentFileName.getBytes())
若文件名称变更,setFileName 时,给出 全限定名称,文件名+.+文件类型

下载文件名称乱码问题解决
http://lj830723.iteye.com/blog/1415479

三、上传

1.MultipartFile 使用 -- 接收上传的 Excel 文件
参考内容:
http://blog.csdn.net/swingpyzf/article/details/20230865

2.上传文件时报异常

Caused by: java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
at org.springframework.util.Assert.notNull(Assert.java:112) ~[spring-core-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.resolveName(RequestParamMethodArgumentResolver.java:168) ~[spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:88) ~[spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:78) ~[spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162) ~[spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129) ~[spring-web-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:775) ~[spring-webmvc-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705) ~[spring-webmvc-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) ~[spring-webmvc-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) ~[spring-webmvc-4.1.9.RELEASE.jar:4.1.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:965) ~[spring-webmvc-4.1.9.RELEASE.jar:4.1.9.RELEASE]
... 33 common frames omitted


解决方案:
http://blog.csdn.net/jiangyu1013/article/details/60758582

3.

Expected MultipartHttpServletRequest: is a MultipartResolver configured

http://blog.csdn.net/jiangyu1013/article/details/60758582

4.接收上传文件内容为空

MultipartFile 接收上传结果为空
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
         <!-- 请求编码格式 -->  
         <property name="defaultEncoding" value="utf-8"></property>  
         <!-- 上传文件大小(单位:字节) -->  
         <property name="maxUploadSize" value="50000000"></property>  
         <!-- 缓冲区大小(单位:KB) -->  
         <property name="maxInMemorySize" value="1024"></property>  
    </bean> 
		<context:component-scan base-package="com.jrd.dams.admin">
		<context:exclude-filter type="regex"
			expression="com.jrd.dams.admin.controller.*" />
	</context:component-scan>

备注:
1.放在 scan 上,报错,数据库连接异常
appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop
2.更换位置后正常

解决方案:
http://www.cnblogs.com/songyunxinQQ529616136/p/6646070.html
接收文件名称与 HTML中上传文件名称相同

5.与File对比
// 包含文件名称及扩展名
String fileName = excelFile.getOriginalFilename();
// 只包含文件名,与File 的getFileName方法有区别
String fileName = excelFile.getFileName();

wb = new XSSFWorkbook(excelFile.toString());
XSSFWorkbook 加载 MultipartFile 时异常
http://jingyan.baidu.com/article/11c17a2c073e12f446e39d38.html
wb = new XSSFWorkbook(excelFile.getInputStream());

猜你喜欢

转载自mingyundezuoan.iteye.com/blog/2366709