ssh框架下利用poi解析excel文件后导入数据库到相应的字段,列出遇到的问题,贴出解决办法,网上的代码虽然是遍地开发,但最开始的时候确实给了我很多的启示,参差不齐,要花大量的的时间来甄选,比较苦恼,另外得根据本身的实际状况做出符合的调整,才能实现,才能为我所用,有一些代码虽然能够实现最后的目的,可是过程繁琐,本能够很简单的解决。javascript
最开始的时候,一心只想着只有可以把excel成功导入数据就OK了,作到了以后,就开始想着,对不一样版本的excel(2003.2007)都可以知足,excel中数据格式的问题,能够在弄出一个通用的版本出来,之后再碰到一样的事件就要简单许多。html
直接贴代码java
jspjquery
<form id="file" name="form1" action="upload.action" method="post" enctype="multipart/form-data"> <s:file name="uploadExcel" label="文件" id="fileName" accept=".xls,.xlsx"></s:file> <input type="submit" value="导入数据" onclick="checkFile();" id="go" > </form>
actionc++
private File uploadExcel; public File getUploadExcel() { return uploadExcel; } public void setUploadExcel(File uploadExcel) { this.uploadExcel = uploadExcel; } SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); String filePath = uploadExcel.getPath(); Workbook workBook = WorkbookFactory.create(new FileInputStream(filePath)); for (int numSheet = 0; numSheet < workBook.getNumberOfSheets(); numSheet++) { Sheet sheet = workBook.getSheetAt(numSheet); if (sheet == null) { continue; } User user = new User(); for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) { Row xRow = sheet.getRow(rowNum); int cellNum = xRow.getPhysicalNumberOfCells(); for(int c=1 ; c<cellNum ; c++){ Cell xCell = xRow.getCell(c); int cellType = xCell.getCellType(); String cellValue = null; int cv = 0; switch (cellType) { case Cell.CELL_TYPE_STRING: //文本 cellValue = xCell.getStringCellValue(); break; case Cell.CELL_TYPE_NUMERIC: //日期或数字 if(DateUtil.isCellDateFormatted(xCell)) { cellValue = fmt.format(xCell.getDateCellValue()); // 日期 }else{ cv = (int)xCell.getNumericCellValue(); cellValue = String.valueOf(cv); // 数字 } break; case Cell.CELL_TYPE_BOOLEAN: // 布尔型 cellValue = String.valueOf(xCell.getBooleanCellValue()); break; case Cell.CELL_TYPE_BLANK: // 空白 cellValue = xCell.getStringCellValue(); break; case Cell.CELL_TYPE_ERROR: // 错误 cellValue = "错误"; break; case Cell.CELL_TYPE_FORMULA: // 公式 cellValue = "错误"; break; default: cellValue = "错误"; } switch (c) { case 1: user.setUserName(cellValue); break; case 2: user.setName(cellValue); break; case 3: user.setPassword(cellValue); break; case 4: user.setPhone(cellValue); break; case 5: user.setGender(cellValue); break; case 6: user.setDeleteFlg(cellValue); break; case 7: user.setGroupId(Integer.parseInt(cellValue)); break; case 8: user.setCreateUserId(Integer.parseInt(cellValue)); break; case 9: user.setCreateUserName(cellValue); break; case 10: user.setCreateTime(Date.valueOf(cellValue)); break; case 11: user.setUpdateUserId(Integer.parseInt(cellValue)); break; case 12: user.setUpdateUserName(cellValue); break; case 13: user.setUpdateTime(Date.valueOf(cellValue)); break; default: break; } } this.userService.save(user); } }
遇到的问题数据库
一、老是在构建excel文件对象的那句出错,在控制台只有warn,缘由是缺一个xmlbeans.jar这个包(以前已经导入了其余poi的包,不少网上的都没有这个包),导入便可框架
poi包下载连接: http://download.csdn.net/detail/forrest_ou/9611291ssh
二、User user = new User(); 这行代码忘记写和写错,那样传过来的值就是nulljsp
三、关于精简的问题,下面的两种方案均可以达到目的,代码量天壤之别post
String path = ServletActionContext.getServletContext().getRealPath( "/upload");
File f = new File(path , uploadExcelFileName); FileUtil.copyFile(uploadExcel,f); String filePath = f.getPath();
String filePath = uploadExcel.getPath();
四、excel格式的问题,上面的代码对excel单元格的格式进行了判断,并都转为string格式,在导入数据库相应格式的时候在转,这样就不用管excel中的格式问题了,下面是最开始用的简单的代码,若是项目简单,也能够试试,毕竟很好弄
user.setUserName(xRow.getCell(0).getStringCellValue()); user.setName(xRow.getCell(1).getStringCellValue()); user.setPassword(xRow.getCell(2).getStringCellValue()); user.setGender(xRow.getCell(3).getStringCellValue()); user.setDeleteFlg(xRow.getCell(4).getStringCellValue()); this.userService.save(user);
以上基本上是遇见的全部内容了,若是有朋友有其余的看法,欢迎留言,但愿能共同进步
--------------------------更新分割线------------------------------------------------------------------------------------------------------------
对上传的文件进行JS验证
一、选择文件是否为空
二、选择文件是不是excel文件
解决问题一 。 由于个人jsp页面中并无像完整项目中的common.jsp 。因此须要本身引入jQuery和对应的js文件,这里就涉及到文件相对路径的问题,引入的jQuery和js的src是相对于jsp的文件地址(../返回上一级)
JSP代码
<!-- 引入jQuery --> <script src="js/jquery/jquery-1.12.3.js"></script> <!-- 引入JS --> <script src="js/uploadFile.js"></script> <button type="button" id="btnUpload" onclick="checkFile">导入数据 </button>
JS代码
function checkFile() { if($('#fileName').val()){ $('#uploadFile').submit(); }else{ alert('请选择文件!'); } }
以上解决选择文件为空的问题
解决问题二。在解决这个问题的时候走了一个捷径,并无使用JS来作判断,而是使用了struts的file标签的accept属性,直接说明只能选择后缀名为.xls 和.xlsx的文件
代码
<s:file name="uploadExcel" label="文件" id="fileName" accept=".xls,.xlsx"></s:file>