论坛首页 Java企业应用论坛

struts2上传excel文件并解析数据插入数据库

浏览 7165 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (3)
作者 正文
   发表时间:2011-11-18  
@SuppressWarnings("unchecked")
	public String uploadAlarmRmsJkmalfuncexperience()
	{
		String date = new SimpleDateFormat("yyyyMMddHHmmssSS").format(new Date());
		 ServletContext aplication = ServletActionContext.getServletContext();
		String path = aplication.getRealPath("/excel");
		System.out.println(path);
		FileUtil.copyFileToDir(path, upload, date+"_"+uploadFileName);
		
		try {
			InputStream is = new FileInputStream(new File(path,date+"_"+uploadFileName));
			try {
				Workbook book= Workbook.getWorkbook(is);
			Sheet seet= book.getSheet(0);
			List<AlarmRmsJkmalfuncexperience> lists = new ArrayList();
			AlarmRmsJkmalfuncexperience jkmalfuncexperience=null;
			for(int i=1;i<seet.getRows();i++)
			{
				jkmalfuncexperience =new AlarmRmsJkmalfuncexperience();
				jkmalfuncexperience.setExpid(seet.getCell(0,i).getContents());
				jkmalfuncexperience.setExptitle(seet.getCell(1,i).getContents());
				jkmalfuncexperience.setMalfuncarea(seet.getCell(2,i).getContents());
				jkmalfuncexperience.setSpecialtype(seet.getCell(3,i).getContents());
				jkmalfuncexperience.setDevicetype(seet.getCell(4,i).getContents());
				jkmalfuncexperience.setDevicecompany(seet.getCell(5,i).getContents());
				jkmalfuncexperience.setVersion(seet.getCell(6,i).getContents());
				jkmalfuncexperience.setExplevel(seet.getCell(7,i).getContents());
				jkmalfuncexperience.setExpkeyword(seet.getCell(8,i).getContents());
				jkmalfuncexperience.setMalfuncdescription(seet.getCell(9,i).getContents());
				jkmalfuncexperience.setExperiencesummary(seet.getCell(10,i).getContents());
					
					lists.add(jkmalfuncexperience);
			}
			alarmRmsJkmalfuncexperienceManager.saveOrUpdateAll(lists);
			
			} catch (BiffException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
 
   发表时间:2011-11-24  
写的太死板了吧,
0 请登录后投票
   发表时间:2011-11-24  
这也能上手液?
0 请登录后投票
   发表时间:2011-11-24   最后修改:2011-11-24
xiaobadi 写道
这也能上手液?

不知道了吧 有的代码适合高手看 为了提高  有些代码适合新手看 为了入门 我发这个代码 就是因为我学这个时上网找疯了才找到个能用的把它搞出来 我当然也为新手更好的学习而发出简单易看的代码 让他们快速入门了 并且也为了作为我的网络知识储备用
0 请登录后投票
   发表时间:2011-11-24  
会给新手造成负面影响,代码里至少要杜绝System.out.........,还有一个方法里面有这么多的代码,要给例子也要给好的代码风格的。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics