excel表格界面导入功能实现,及实现过程中遇见的问题
2018-03-16 10:36
453 查看
/**
* 督导结果数据导入
* @param file 导入excel文件实体
* @param request
* @param session
* @return
* @throws Exception
*/
@RequestMapping(value="/surveyDataImport.html",method={RequestMethod.POST},produces= "text/html;charset=UTF-8")
public@ResponseBody String surveyDataImport(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request,HttpSession session,HttpServletResponse response) throws Exception{
InputStream is = null;
Workbook workbook = null;
String returnStr = "";
JSONObject jsonObj = new JSONObject();
String fileName = "";
boolean flag = false;
try {
if(file != null){
fileName = file.getOriginalFilename();
}else{
jsonObj.put("status", -1);
jsonObj.put("msg", "请选择文件在上传...");
return jsonObj.toString();
}
//高版本excel文件处理
is = file.getInputStream();
workbook = new XSSFWorkbook(is);
flag = true;
} catch (Exception e) {
if(workbook == null){//低版本excel文件处理
is = file.getInputStream();
workbook = new HSSFWorkbook(is);
flag = true;
}
}finally{
if(!flag || workbook == null){
jsonObj.put("status", -1);
jsonObj.put("msg", "该上传文件解析失败,请选择重新上传...");
return jsonObj.toString();
}
if(is != null){
is.close();
}
}
logger.info("该excel文件【"+ fileName +"】数据开始导入,请稍等...");
returnStr = serveyDataImportService.eduSurveyDataImport(workbook, fileName);
return returnStr;
}
public String eduSurveyDataImport(Workbook workbook, String fileName){
//根据项目名称判断数据文件数据是否重复导入
int beginIndex = fileName.indexOf("“");
int endIndex = fileName.indexOf("”");
String itemName = fileName.substring(beginIndex + 1, endIndex);
JSONObject jsonObj = new JSONObject();
List<EduSurveyMain>eduSurveyMainList = eduSurveyMainDao.getEduSurveyMainbyItemName(itemName);
if(eduSurveyMainList != null && eduSurveyMainList.size() > 0){
jsonObj.put("status", -1);
jsonObj.put("msg", "该文件内容已经导过,请勿重复添加...");
return jsonObj.toString();
}
//1.将excel数据处理并保存到edu_survey_detail_log表中
List<EduSurveyDetailLog> surveyLogList = excelData2DetailList(workbook);
boolean importFlag = false;
try{
importFlag = eduSurveyDetailLogDao.saveLog(surveyLogList);
if(importFlag){
//2.如果数据保存成功,开始掉存储过程
String procedureName = "bianmin.edu_survey";
boolean callFlag = eduSurveyDetailLogDao.callProcedure(procedureName);
if(callFlag){
jsonObj.put("status", 1);
jsonObj.put("msg", "该调查结果文件【"+ fileName +"】数据导入成功!");
logger.info("文件【"+fileName+"】数据导入完毕!");
}else{
jsonObj.put("status", -1);
jsonObj.put("msg", "存储过程调用失败!");
}
}else{
jsonObj.put("status", -1);
jsonObj.put("msg", "该调查结果文件【"+ fileName +"】数据保存edu_survey_detail_log表失败!");
}
}finally{
//3.清理edu_survey_detail_log表中所有数据
eduSurveyDetailLogDao.clearLogData();
}
return jsonObj.toString();
}
当时解析excel我们使用的组件是poi,而非jxl。因为poi支持更高版本的excel 处理。实现excel导入功能过程还比较顺利,只是在读取excel时出现了点问题。简述如下:
刚开始使用new HSSFWorkbook(new FileInputStream(excelFile))来读取Workbook,对Excel2003以前(包括2003)的版本没有问题,但读取Excel2007时发生如下异常:
* 督导结果数据导入
* @param file 导入excel文件实体
* @param request
* @param session
* @return
* @throws Exception
*/
@RequestMapping(value="/surveyDataImport.html",method={RequestMethod.POST},produces= "text/html;charset=UTF-8")
public@ResponseBody String surveyDataImport(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request,HttpSession session,HttpServletResponse response) throws Exception{
InputStream is = null;
Workbook workbook = null;
String returnStr = "";
JSONObject jsonObj = new JSONObject();
String fileName = "";
boolean flag = false;
try {
if(file != null){
fileName = file.getOriginalFilename();
}else{
jsonObj.put("status", -1);
jsonObj.put("msg", "请选择文件在上传...");
return jsonObj.toString();
}
//高版本excel文件处理
is = file.getInputStream();
workbook = new XSSFWorkbook(is);
flag = true;
} catch (Exception e) {
if(workbook == null){//低版本excel文件处理
is = file.getInputStream();
workbook = new HSSFWorkbook(is);
flag = true;
}
}finally{
if(!flag || workbook == null){
jsonObj.put("status", -1);
jsonObj.put("msg", "该上传文件解析失败,请选择重新上传...");
return jsonObj.toString();
}
if(is != null){
is.close();
}
}
logger.info("该excel文件【"+ fileName +"】数据开始导入,请稍等...");
returnStr = serveyDataImportService.eduSurveyDataImport(workbook, fileName);
return returnStr;
}
public String eduSurveyDataImport(Workbook workbook, String fileName){
//根据项目名称判断数据文件数据是否重复导入
int beginIndex = fileName.indexOf("“");
int endIndex = fileName.indexOf("”");
String itemName = fileName.substring(beginIndex + 1, endIndex);
JSONObject jsonObj = new JSONObject();
List<EduSurveyMain>eduSurveyMainList = eduSurveyMainDao.getEduSurveyMainbyItemName(itemName);
if(eduSurveyMainList != null && eduSurveyMainList.size() > 0){
jsonObj.put("status", -1);
jsonObj.put("msg", "该文件内容已经导过,请勿重复添加...");
return jsonObj.toString();
}
//1.将excel数据处理并保存到edu_survey_detail_log表中
List<EduSurveyDetailLog> surveyLogList = excelData2DetailList(workbook);
boolean importFlag = false;
try{
importFlag = eduSurveyDetailLogDao.saveLog(surveyLogList);
if(importFlag){
//2.如果数据保存成功,开始掉存储过程
String procedureName = "bianmin.edu_survey";
boolean callFlag = eduSurveyDetailLogDao.callProcedure(procedureName);
if(callFlag){
jsonObj.put("status", 1);
jsonObj.put("msg", "该调查结果文件【"+ fileName +"】数据导入成功!");
logger.info("文件【"+fileName+"】数据导入完毕!");
}else{
jsonObj.put("status", -1);
jsonObj.put("msg", "存储过程调用失败!");
}
}else{
jsonObj.put("status", -1);
jsonObj.put("msg", "该调查结果文件【"+ fileName +"】数据保存edu_survey_detail_log表失败!");
}
}finally{
//3.清理edu_survey_detail_log表中所有数据
eduSurveyDetailLogDao.clearLogData();
}
return jsonObj.toString();
}
当时解析excel我们使用的组件是poi,而非jxl。因为poi支持更高版本的excel 处理。实现excel导入功能过程还比较顺利,只是在读取excel时出现了点问题。简述如下:
刚开始使用new HSSFWorkbook(new FileInputStream(excelFile))来读取Workbook,对Excel2003以前(包括2003)的版本没有问题,但读取Excel2007时发生如下异常:
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)
该错误意思是说,文件中的数据是用Office2007+XML保存的,而现在却调用OLE2 Office文档处理,应该使用POI不同的部分来处理这些数据,比如使用XSSF来代替HSSF。
于是按提示使用XSSF代替HSSF,用new XSSFWorkbook(excelFile)来读取Workbook,对Excel2007没有问题了,可是在读取Excel2003以前(包括2003)的版本时却发生了如下新异常:
org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file: '*.xls'
该错误是说,操作无效,不能打开指定的xls文件。
下载POI的源码后进行单步调试,发现刚开始的时候还是对的,但到ZipFile类后就找不到文件了,到网上查了下,原来是XSSF不能读取Excel2003以前(包括2003)的版本,这样的话,就需要在读取前判断文件是2003前的版本还是2007的版本,然后对应调用HSSF或XSSF来读取。
小结:由于HSSFWorkbook只能操作excel2003一下版本,XSSFWorkbook只能操作excel2007以上版本,所以利用Workbook接口创建对应的对象操作excel来处理兼容性
相关文章推荐
- C# 将Excel数据导入到数据库(实现SQL2000企业管理器导入EXCEL功能)
- Excel导入数据库过程中遇到的问题
- 如何实现数据从EXCEL表格导入到SQL Server
- Java Excel导入功能的实现
- EXCEL导入功能java代码实现
- Excel的导入导出功能实现
- 实现类Excel表格编辑功能的jQuery插件:Handsontable
- ASP.NET实现Excel导入SQL数据库以及各种出现的问题解决方案
- ASP.NET下将Excel表格中的数据规则的导入数据库思路分析及实现
- asp.net实现 EXCEL数据导入到数据库功能
- 用jxl将Excel中的数据导入功能的一个问题
- 解决EXCEL导入 SQL 2000过程,列中存在不同格式的问题
- java实现Excel导入功能代码
- jQuery实现仿Excel表格编辑功能 - Handsontable
- C#解决从含身份证号码的Excel表格导入数据库的问题
- jquery实现的类似excel功能的表格----------请高手优化指点
- 使用SqlBulkCopy类实现导入excel表格
- 使用phpexcel类实现excel导入mysql数据库功能
- asp.net实现 EXCEL数据导入到数据库功能
- C# 操作EXCEL 类 可以实现导入导出功能 折腾了好长时间 现在两种方法显示出来