日期:2014-05-16  浏览次数:20453 次

struts2下载excel文件并解析数据插入数据库
public InputStream getDownloadFile()
	{
		
			return ServletActionContext.getServletContext().getResourceAsStream("/excel/jkmalfuncexperienceDownLoadMold.xls");
	}
	
	@SuppressWarnings("unchecked")
	public String download(){
		
		 ServletContext aplication = ServletActionContext.getServletContext();
		 String path = aplication.getRealPath("/excel");
		WritableWorkbook book = null;
		try {
			List<AlarmRmsJkmalfuncexperience> exportlist = null;
			if(ids!=null&&ids.size()>0)
			{
				 exportlist = alarmRmsJkmalfuncexperienceManager.getList(ids);
			}else
			{
				exportlist = alarmRmsJkmalfuncexperienceManager.getList(alarmRmsJkmalfuncexperience);
			}
			
			
			book =  Workbook.createWorkbook(new File(path,"jkmalfuncexperienceDownLoadMold.xls"));
			WritableSheet sheet = book.createSheet("故障经验库", 0); //添加excel的标题列
			Label titleExpid = new Label(0, 0, "经验编号");
			sheet.addCell(titleExpid);
			Label titleExptitle = new Label(1, 0, "经验主题");
			sheet.addCell(titleExptitle);
			Label titleMalfuncarea = new Label(2, 0, "故障发生地区");
			sheet.addCell(titleMalfuncarea);
			Label titleSpecialtype = new Label(3, 0, "专业类别");
			sheet.addCell(titleSpecialtype);
			Label titleDevicetype = new Label(4, 0, "故障设备");
			sheet.addCell(titleDevicetype);
			Label titleDevicecompany = new Label(5, 0, "故障设备厂商");
			sheet.addCell(titleDevicecompany);
			Label titleVersion = new Label(6, 0, "软硬件编号");
			sheet.addCell(titleVersion);
			Label titleExplevel = new Label(7, 0, "经验等级");
			sheet.addCell(titleExplevel);
			Label titleExpkeyword = new Label(8, 0, "经验关键字");
			sheet.addCell(titleExpkeyword);
			Label titleMalfuncdescription = new Label(9, 0, "故障描述");
			sheet.addCell(titleMalfuncdescription);
			Label titleExperiencesummary = new Label(10, 0, "经验总结");
			sheet.addCell(titleExperiencesummary);
			
			for(int i=0;i<exportlist.size();i++) //添加excel数据
			{
				AlarmRmsJkmalfuncexperience arj = exportlist.get(i);
				int a=i+1;//意思是从第二行开始添加数据
				Label lableExpid = new Label(0, a, arj.getExpid());
				sheet.addCell(lableExpid);
				Label lable1Exptitle = new Label(1, a, arj.getExptitle());
				sheet.addCell(lable1Exptitle);
				Label lableMalfuncarea = new Label(2, a, arj.getMalfuncarea());
				sheet.addCell(lableMalfuncarea);
				Label lableSpecialtype = new Label(3, a, arj.getSpecialtype());
				sheet.addCell(lableSpecialtype);
				Label lableDevicetype = new Label(4, a, arj.getDevicetype());
				sheet.addCell(lableDevicetype);
				Label lableDevicecompany = new Label(5, a, arj.getDevicecompany());
				sheet.addCell(lableDevicecompany);
				Label lableVersion = new Label(6, a, arj.getVersion());
				sheet.addCell(lableVersion);
				Label lableExplevel = new Label(7, a, arj.getExplevel());
				sheet.addCell(lableExplevel);
				Label lableExpkeyword = new Label(8, a, arj.getExpkeyword());
				sheet.addCell(lableExpkeyword);
				Label lableMalfuncdescription = new Label(9, a, arj.getMalfuncarea());
				sheet.addCell(lableMalfuncdescription);
				Label lableExperiencesummary = new Label(10, a, arj.getExperiencesummary());
				sheet.addCell(lableExperiencesummary);
			}
			
			
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(Exception e)
		{
			
		}finally{
			try {
				book.write();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				book.close();
			} catch (WriteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return SUCCESS;
	}
?
<action name="download" class="alarmRmsJkmalfunce