首页 java三层 springmvc 导入excel

springmvc 导入excel

springmvc 导入excel 测试代码 @RequestMapping(value=”/AddImportCoop…

springmvc 导入excel

测试代码

		@RequestMapping(value="/AddImportCooperation")
		public void AddImportCooperation(
				ModelMap model, HttpServletResponse response,HttpServletRequest request) throws IOException{

			  MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			  MultipartFile multipartFile = multipartRequest.getFile("file");
			  String sourceName = multipartFile.getOriginalFilename(); // 原始文件名
			  String fileType = sourceName.substring(sourceName.lastIndexOf("."));


			  //System.out.println("上传的文件名为:"+sourceName+"类型为:"+fileType);

			  //System.out.println("项目Id为:"+proId+"项目名称为:"+proName);
			  String base = request.getSession().getServletContext().getRealPath("/WEB-INF/views/upload");
			  File file = new File(base);
			  if(!file.exists()){
			   file.mkdirs();
			  }
			   String path=base + File.separator + sourceName;
			   multipartFile.transferTo(new File(path));
			   //service.insert("insertAttachment", attach);
			   //上传成功后读取Excel表格里面的数据
			   System.out.println("路径是"+path);
			  // File read=new File(path);

			   File read=new File(path);
			   Map<String,Cooperation> map=FileUtil.readExcelFile(path);



			JSONObject json = DWZUtil.returnSuccessJson("导入成功", "", "closeCurrent", "coo/initCooperationList");
			DWZUtil.writer(response, json);
		}

 

  public static Map<String,Cooperation> readExcelFile(String fileName){
		  //创建对Excel工作薄文件的引用

		  HashMap<String, Cooperation> map=new HashMap<String, Cooperation>();
		  try {

		    InputStream is = new FileInputStream(fileName);
	        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);

	        // 循环工作表Sheet
	        for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
	            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
	            if (hssfSheet == null) {
	                continue;
	            }
	            // 循环行Row
	            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
	                HSSFRow hssfRow = hssfSheet.getRow(rowNum);
	                if (hssfRow == null) {
	                    continue;
	                }
	                HSSFCell no = hssfRow.getCell(0);
	                HSSFCell name = hssfRow.getCell(1);
	                HSSFCell age = hssfRow.getCell(2);
	                HSSFCell score = hssfRow.getCell(3);
	                System.out.println(no+""+name+age+score+"测试数据");
	               // xlsDto.setCj(Float.parseFloat(getValue(cj)));
	               // list.add(xlsDto);
	            }
	        }



		  } catch (Exception e) {
		   e.printStackTrace();

		  }finally{


		  }


		  return map;
		 }

参考如下

http://www.cnblogs.com/hongten/archive/2012/02/22/java2poi.html

http://www.cnblogs.com/hongten/p/java_poi_excel_xls_xlsx.html

免责声明:文章内容不代表本站立场,本站不对其内容的真实性、完整性、准确性给予任何担保、暗示和承诺,仅供读者参考,文章版权归原作者所有。如本文内容影响到您的合法权益(内容、图片等),请及时联系本站,我们会及时删除处理。

为您推荐

Hibernate 中对象 set 属性时自动保存入库的问题

Hibernate 中对象 set 属性时自动保存入库的问题

遇到这个问题了 背景 在某个项目中,通过 Heibernate 查出某个对象后,对属性进行了 set 操作,并没有进行 ...
hibernate中@DynamicInsert和@DynamicUpdate

hibernate中@DynamicInsert和@DynamicUpdate

在Hibernate中可以利用@DynamicInsert和@DynamicUpdate生成动态SQL语句,即在插入和修...
redis简单命令

redis简单命令

linux进入redis redis-cli -p 6379 查询 config get requirepass # 对...
“”字符串解析为date报错

“”字符串解析为date报错

  Spring3MVC中Controller层接受前端页面的参数有一种情况: @RequestMapping...
Restrictions 非空验证

Restrictions 非空验证

Hibernate-Restrictions用法 Restrictions or用法   criteria.a...
返回顶部