jsp中导入导出excel,ssh框架
导入Excel:jsp中
1 <form action="user_importTradingMoney" enctype="multipart/form-data" method="post"> 2 <label>导入数据 3 <input type="file" name="tradingMoneyFile" value="选择文件"> 4 <input type="submit" value="提交数据"> 5 </label> 6 </form>
action中:
action最前面肯定要有下面这几句,而且要有其对应的set、get方法
private File tradingMoneyFile;// 实际上传文件 private String tradingMoneyFileContentType; // 文件的内容类型 private String tradingMoneyFileFileName; // 上传文件名
这是为了通过struts2自动获得上传的文件,当然struts.xml中肯定要有这几句:
1 <constant name="struts.i18n.reload" value="true" /> 2 <constant name="struts.configuration.xml.reload" value="true" /> 3 <constant name="struts.devMode" value="true" /> 4 <constant name=" struts.action.extension" value="action,," /> 5 <constant name="struts.multipart.maxSize" value="102400000000000" />
文件大小什么的设定自己看着办。。。。
然后action中的importTradingMoney方法如下:
1 //导入交易金额的Excel表 2 public String importTradingMoney(){ 3 HttpServletRequest request = ServletActionContext.getRequest(); 4 Calendar cal = Calendar.getInstance(); 5 int month = cal.get(Calendar.MONTH); 6 int year = cal.get(Calendar.YEAR); 7 jxl.Workbook wb=null; 8 try { 9 //InputStream is=new FileInputStream(tradingMoneyFile);//读取存放数据的excel表格在电脑中的路径 10 InputStream is=new FileInputStream(tradingMoneyFile); 11 wb =Workbook.getWorkbook(is);//创建workbook对象,我的理解就是这个整个的excel 12 Sheet s=wb.getSheet(0);//创建sheet,其实就是excel中的每一页,其中的每一张表,他的下标是从0开始的//这里也就是取得第一张表 13 int rsRows=s.getRows();//得到所有的行 14 int rsColumns=s.getColumns();//得到所有的列 15 for(int i=1;i<rsRows;i++){ 16 String userName=s.getCell(0, i).getContents();//(列,行) 17 int tradingMoney=Integer.parseInt(s.getCell(1, i).getContents().toString()); 18 TradingMoney tradingMoneyClass=new TradingMoney(); 19 tradingMoneyClass.setUserName(userName); 20 tradingMoneyClass.setTradingMoney(tradingMoney); 21 tradingMoneyClass.setYear(year); 22 tradingMoneyClass.setMonth(month); 23 userService.addTradingMoney(tradingMoneyClass); 24 } 25 } catch (FileNotFoundException e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 } catch (BiffException e) { 29 // TODO Auto-generated catch block 30 e.printStackTrace(); 31 } catch (IndexOutOfBoundsException e) { 32 // TODO Auto-generated catch block 33 e.printStackTrace(); 34 } catch (IOException e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 } catch (Exception e) { 38 // TODO Auto-generated catch block 39 e.printStackTrace(); 40 } 41 List<User> userList = null; 42 try { 43 userList = userService.selectUser(null, null); 44 } catch (Exception e) { 45 // TODO Auto-generated catch block 46 e.printStackTrace(); 47 } 48 request.setAttribute("userList", userList); 49 return "selectUser"; 50 }
至于excel的导出,我只是简单地把当前页面复制到了excel中:
1 <%@page import="java.text.SimpleDateFormat"%> 2 <% 3 SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd"); 4 String filename = new String(("佣金明细-"+sf.format(new Date())).getBytes("utf8"),"ISO-8859-1"); 5 response.setHeader("Content-disposition","attachment; filename="+filename+".xls"); 6 %>
因为我要复制a.jsp页面的内容到excel中,而a.jsp中的内容又是便利list而出来,如果直接把上面这段代码加入到a.jsp中,那么a.jsp中的内容你就会看不到,这个页面一打开就会让你下载,所以就没有那种先预览再下载的效果了。。。。所以就在a.jsp中加了一个按钮
1 <input type="button" value="结果导出为Excel" style="width:120px" class="button-action" onclick="toExcel(<s:property value='#request.owerUser.id' />,'${request.owerUser.userAccount }')"/>
1 var userIdvar,userAccountvar; 2 function toExcel(userIdvar,userAccountvar){ 3 //脚本运行后,将在新窗体newwindow中打开,宽为100,高为400,距屏顶0象素,屏左0象素,无工具条,无菜单条,无滚动条,不可调整大小,无地址栏,无状态栏。 4 window.open('reportUser_selectDetailReportUser?notice=1&userId='+userIdvar+'&userAccount='+userAccountvar,'newwindow','height=600,width=1200,top=80,left=80,toolbar=no,menubar=no,scrollbars=yes, resizable=yes,location=no, status=no'); 5 };
就是重新发一遍请求到b.jsp中,其内容与a.jsp相同,所以这个b.jsp就不需要打开了。。。一切ok!
刚刚新增了不是简单地copy页面,而是从action导出excel表:
1 //导出Excel表 2 public void exportForm(){ 3 HttpServletRequest request = ServletActionContext.getRequest(); 4 try { 5 request.setCharacterEncoding("utf8"); 6 7 Date now = new Date(); 8 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 9 String sDate = sdf.format(now); 10 String fileName = new String(("用户-"+sDate).getBytes("utf8"),"ISO-8859-1"); 11 String file = fileName+".xls"; 12 13 HttpServletResponse response = ServletActionContext.getResponse(); 14 response.setContentType("application/vnd.ms-excel"); 15 response.setHeader("Content-Disposition" ,"attachment;filename="+file+""); 16 OutputStream os = response.getOutputStream(); 17 String[] title = {"姓名","账号","角色","电话","邮箱","发展人账号","备注"}; 18 int i=0; 19 20 WritableWorkbook wwb = Workbook.createWorkbook(os); 21 WritableSheet ws = wwb.createSheet("用户表", 0); 22 ws.setColumnView(0,15); 23 ws.setColumnView(1,15); 24 ws.setColumnView(2,20); 25 ws.setColumnView(3,15); 26 ws.setColumnView(4,18); 27 ws.setColumnView(5,15); 28 ws.setColumnView(6,35); 29 ws.setColumnView(7,15); 30 31 32 for( i=0;i<title.length;i++){ 33 WritableFont wf = new WritableFont(WritableFont.TIMES, 11, WritableFont.BOLD, false); 34 WritableCellFormat wcfF = new WritableCellFormat(wf); 35 Label labelCF = new Label(i, 0, title[i],wcfF); 36 ws.addCell(labelCF); 37 } 38 39 i=1; 40 String userName = null; 41 String userAccount = null; 42 String userRole = null; 43 String userPhone = null; 44 String userEmail = null; 45 String higherAgentAccount = null; 46 String userNote = null; 47 48 //根据selectedIds,生成一个List 49 50 51 //List list = this.pageBean.getList(); 52 53 List list = userService.selectUser(null, null); 54 Iterator it = list.iterator(); // 获得一个迭代子 55 56 while (it.hasNext()) { 57 User v = (User)it.next(); // 得到下一个元素 58 59 userName = v.getUserName(); 60 Label labelC0 = new Label(0,i,userName); 61 ws.addCell(labelC0); 62 63 userAccount = v.getUserAccount(); 64 Label labelC1 = new Label(1,i,userAccount); 65 ws.addCell(labelC1); 66 67 userRole = v.getRole().getRoleName(); 68 Label labelC2 = new Label(2,i,userRole); 69 ws.addCell(labelC2); 70 71 72 userPhone = v.getUserPhone(); 73 Label labelC3 = new Label(3,i,userPhone); 74 ws.addCell(labelC3); 75 76 userEmail = v.getUserEmail(); 77 Label labelC4 = new Label(4,i,userEmail); 78 ws.addCell(labelC4); 79 80 higherAgentAccount = v.getHigherAgentAccount(); 81 Label labelC5 = new Label(5,i,higherAgentAccount); 82 ws.addCell(labelC5); 83 84 userNote = v.getUserNote(); 85 Label labelC6 = new Label(6,i,userNote); 86 ws.addCell(labelC6); 87 88 i++; 89 } 90 //写入Exel工作表 91 os.flush(); 92 wwb.write(); 93 //关闭Excel工作薄对象 94 wwb.close(); 95 os.close(); 96 } catch (Exception e) { 97 98 e.printStackTrace(); 99 } 100 }
//如果想打开并写入一个已经存在的excel,只能通过副本操作 //Excel获得文件 Workbook wb2=Workbook.getWorkbook(excelFile); //打开一个文件的副本,并且指定数据写回到原文件 WritableWorkbook wwb2= Workbook.createWorkbook(excelFile,wb2); WritableSheet ws = wwb2.getSheet(0);