POI读取excel表格
2015-08-20 18:02
435 查看
前台使用ajaxFileUpload上传文件,后台使用POI处理:
js代码:
后台接收和POI处理代码:
logger.info("print out readExcel: " + objList); } }//业务逻辑代码,调用service } response.getWriter().print("1");}
js代码:
function improtMessage() { $.messager.confirm('提示','确定您要导入配置?',function(r){ //jqueryEasyUI if (r){ $.ajaxFileUpload({ url: path+'/systemConfigureController/importSysConfig.do', secureuri: false,//异步 fileElementId: 'file',//上传控件ID dataType: 'text',//返回的数据信息格式 success: function(data){ } }) } }); }
后台接收和POI处理代码:
private static int version2003 = 2003; private static int version2007 = 2007; private static int version = version2003; /** * 导入配置 * @param request * @param response * @throws IOException */ @RequestMapping("importSysConfig") public void importData(HttpServletRequest request, HttpServletResponse response) throws IOException{ logger.info("**********into method importData*************"); // 设置上下文 CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); // 检查form是否有enctype="multipart/form-data" if (multipartResolver.isMultipart(request)) { MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; Iterator<String> iter = multiRequest.getFileNames(); List objList = new ArrayList(); while (iter.hasNext()) { MultipartFile file = multiRequest.getFile(iter.next()); logger.info("print out file: "+file+" fileName: "+file.getName()+" fileSize: "+file.getSize()); version = (file.getName().endsWith(".xls") ? version2003 : version2007); if (version == 2003) { POIFSFileSystem fs = new POIFSFileSystem(file.getInputStream()); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); <pre name="code" class="html"> objList = readCountryExcel(sheet);logger.info("print out readExcel: " + objList); }else if (version == 2007){ XSSFWorkbook hwk = new XSSFWorkbook(file.getInputStream());// 将is流实例到 一个excel流里 XSSFSheet sheet = hwk.getSheetAt(0);// 得到book第一个工作薄sheet objList = readCountryExcel(sheet);
logger.info("print out readExcel: " + objList); } }//业务逻辑代码,调用service } response.getWriter().print("1");}
public List<Country> readCountryExcel(Sheet sheet){ List<Country> countryList = new ArrayList<Country>(); int rowNum = sheet.getPhysicalNumberOfRows(); //获得总共的行数 Country country = null; for (int i =1; i<rowNum; i++){ country = new Country(); Row row = sheet.getRow(i); //得到第i行数据 Cell c = null; if (null != row){ c = row.getCell(0); if (null != c){ if (c.getCellType() == c.CELL_TYPE_NUMERIC) { c.setCellType(c.CELL_TYPE_STRING); country.setCountry(c.getStringCellValue()); }else{ country.setCountry(c.getStringCellValue()); } } c = row.getCell(1); if (null != c){ if (c.getCellType() == c.CELL_TYPE_NUMERIC) { c.setCellType(c.CELL_TYPE_STRING); country.setProvince(c.getStringCellValue()); }else { country.setProvince(c.getStringCellValue()); } } c = row.getCell(2); if (null != c){ if (c.getCellType() == c.CELL_TYPE_NUMERIC) { c.setCellType(c.CELL_TYPE_STRING); country.setZone(c.getStringCellValue()); }else { country.setZone(c.getStringCellValue()); } } c = row.getCell(3); if (null != c){ if (c.getCellType() == c.CELL_TYPE_NUMERIC) { c.setCellType(c.CELL_TYPE_STRING); country.setCode(c.getStringCellValue()); }else { country.setCode(c.getStringCellValue()); } } } countryList.add(country); logger.info("print out read Excel row" + i + "Country: " + country); } return countryList; }需要引入的jar:poi-3.13-beta1.jar poi-ooxml-3.13-beta1.jar poi-ooxml-schemas-3.13-beta1.jar
相关文章推荐
- POJ 3126 --Father Christmas flymouse【scc缩点构图 && SPFA求最长路】
- PostgreSQL物理备份与恢复
- nodejs学习:net模块
- SFTP 服务的纠结
- linux通过shell脚本修改文件内容
- HDOJ 3342 Llegal or Not (拓扑排序)
- nodejs系统学习路线(零)——起步
- 03-photoshop制作gif动画教程
- UNIX Tutorial Five
- 如何优化JAVA代码
- 关于在eclipse中插入图片的问题
- K2 BPM+Microsoft Dynamics CRM,妥妥的~
- 小记同学一次奇葩的DNS欺骗实验失败经历
- XML
- linux用户管理
- linux 防火墙和SSH配置
- SQL Server 2005 控制用户权限访问表
- 端口
- STL:如何理解容器对象的比较
- [已解决]SSH远程登录失败,提示“Password authentication failed”