博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
导出excel——弹出框
阅读量:6094 次
发布时间:2019-06-20

本文共 9189 字,大约阅读时间需要 30 分钟。

表单提交

凡是表单提交(表单提交分3种,见以下的1、2、3)的话。而且设置了表单标签的enctype="multipart/form-data"属性。那么这个时候就会打开弹出框。
1.表单提交
2.js表单提交
3.jquery、extjs等等其它的表单提交
代码演示样例
//jsp代码
导出
//js代码function excelExpert() {		var form = document.getElementById("myform1");		form.action = "/ProjectManagerWeb/shangbaoAction!exportExcelOfSta.action";		form.submit();}
//java代码:业务控制器/**	 * 项目数据查询 导出到excel	 */	public void exportExcelOfSta(){		try{			this.setSuccess(true);			System.out.println("--開始导出项目数据查询--");			List
list = TianBaoService.queryProjectData(projectName, startTime, endTime, inputUser, organId); //t.Id,t.type,t.worktype,t.workload,t.documentpage,t.code, //t.problem,t.starttime,t.endtime,,t.Intputtime,pin.projectname ,u.username" //获取tianbaos数据实体List
和username List
List
tianBaoList = new ArrayList
(); List
usernameList = new ArrayList
(); List
projectnameList = new ArrayList
(); for(Object[] obj:list){ TianBaoEntity tianBao=new TianBaoEntity(); tianBao.setId(String.valueOf(obj[0])); tianBao.setType(String.valueOf(obj[1])); tianBao.setWorkType(String.valueOf(obj[2])); tianBao.setWorkLoad(String.valueOf(obj[3])); tianBao.setDocumentPage(String.valueOf(obj[4])); tianBao.setCode(String.valueOf(obj[5])); tianBao.setProblem(String.valueOf(obj[6])); tianBao.setStartTime(DateUtil.format(String.valueOf(obj[7]))); tianBao.setEndTime(DateUtil.format(String.valueOf(obj[8]))); tianBao.setIntputTime(DateUtil.format(String.valueOf(obj[9]))); tianBaoList.add(tianBao); String projectname = String.valueOf(obj[10]); projectnameList.add(projectname); String username = String.valueOf(obj[11]); usernameList.add(username); } System.out.println("数据总计:"+tianBaoList.size());// if (!isEmpty(noTaskStr)) {// if (noTaskStr.equals("excelexpert")) {// // }// } System.out.println("開始创建表格"); HttpServletResponse response = this.getResponse(); OutputStream outData = response.getOutputStream(); response.reset(); response.setContentType("application/vnd.ms-excel;charset=gbk"); response.addHeader( "Content-Disposition", "attachment;filename=" + new String( ("项目数据查询" + DateUtil.getCurrDateStr() + ".xls") .getBytes("gbk"), "ISO-8859-1")); HSSFWorkbook noTaskExcel = new HSSFWorkbook(); // 定义Excel单元格样式 HSSFCellStyle cs = noTaskExcel.createCellStyle(); cs.setAlignment(HSSFCellStyle.ALIGN_CENTER); cs.setBorderLeft(HSSFCellStyle.BORDER_THIN); cs.setBorderTop(HSSFCellStyle.BORDER_THIN); cs.setBorderRight(HSSFCellStyle.BORDER_THIN); cs.setBorderBottom(HSSFCellStyle.BORDER_THIN); HSSFFont f = noTaskExcel.createFont(); f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); f.setCharSet(HSSFFont.ANSI_CHARSET); cs.setFont(f); // 样式定义结束 HSSFSheet sheet = noTaskExcel.createSheet("项目数据查询"); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1)); // 设置标题 HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(0); if (cs != null) { cell.setCellStyle(cs); } cell.setCellValue("项目数据查询"); // 设置列的标题 HSSFRow rowFirst = sheet.createRow(1); HSSFCell cell0 = rowFirst.createCell(0); if (cs != null) { cell0.setCellStyle(cs); } cell0.setCellValue("序号"); HSSFCell cell1 = rowFirst.createCell(1); if (cs != null) { cell1.setCellStyle(cs); } cell1.setCellValue("项目名字"); //third fourth fifth sixth seventh eighth ninth tenth eleventh HSSFCell cell2 = rowFirst.createCell(2); if (cs != null) { cell2.setCellStyle(cs); } cell2.setCellValue("分类"); HSSFCell cell3 = rowFirst.createCell(3); if (cs != null) { cell3.setCellStyle(cs); } cell3.setCellValue("类型"); HSSFCell cell4 = rowFirst.createCell(4); if (cs != null) { cell4.setCellStyle(cs); } cell4.setCellValue("工作量"); HSSFCell cell5 = rowFirst.createCell(5); if (cs != null) { cell5.setCellStyle(cs); } cell5.setCellValue("文档页数"); HSSFCell cell6 = rowFirst.createCell(6); if (cs != null) { cell6.setCellStyle(cs); } cell6.setCellValue("代码行数"); HSSFCell cell7 = rowFirst.createCell(7); if (cs != null) { cell7.setCellStyle(cs); } cell7.setCellValue("问题数"); HSSFCell cell8 = rowFirst.createCell(8); if (cs != null) { cell8.setCellStyle(cs); } cell8.setCellValue("開始日期"); HSSFCell cell9 = rowFirst.createCell(9); if (cs != null) { cell9.setCellStyle(cs); } cell9.setCellValue("结束日期"); HSSFCell cell10 = rowFirst.createCell(10); if (cs != null) { cell10.setCellStyle(cs); } cell10.setCellValue("录入人"); HSSFCell cell11 = rowFirst.createCell(11); if (cs != null) { cell11.setCellStyle(cs); } cell11.setCellValue("录入日期"); for (int i = 0; i < tianBaoList.size(); i++) { TianBaoEntity tianBao = (TianBaoEntity) tianBaoList.get(i); HSSFRow row1 = sheet.createRow(i + 2); HSSFCell cl0 = row1.createCell(0); if (cs != null) { cl0.setCellStyle(cs); } cl0.setCellValue(i + 1); HSSFCell cl1 = row1.createCell(1); if (cs != null) { cl1.setCellStyle(cs); } cl1.setCellValue(projectnameList.get(i)); HSSFCell cl2 = row1.createCell(2); if (cs != null) { cl2.setCellStyle(cs); } //分类render String type = tianBao.getType(); String typeTemp = null; if(type.equals("promanager")){ typeTemp="项目管理"; }else if(type.equals("development")){ typeTemp="产品研发"; }else if(type.equals("implementation")){ typeTemp="产品实施"; }else if(type.equals("supportmanager")){ typeTemp="支持管理"; } cl2.setCellValue(typeTemp); HSSFCell cl3 = row1.createCell(3); if (cs != null) { cl3.setCellStyle(cs); } //类型render String workType = tianBao.getWorkType(); String wortTypeTemp = null; if(workType.equals("plan")){ wortTypeTemp="计划"; }else if(workType.equals("planaduit")){ wortTypeTemp="计划评审"; }else if(workType.equals("requiretment")){ wortTypeTemp="需求"; }else if(workType.equals("requiretmentaduit")){ wortTypeTemp="需求评审"; }else if(workType.equals("planaduit")){ wortTypeTemp="计划评审"; }else if(workType.equals("design")){ wortTypeTemp="设计"; }else if(workType.equals("designaduit")){ wortTypeTemp="设计评审"; }else if(workType.equals("code")){ wortTypeTemp="编码"; }else if(workType.equals("codeaduit")){ wortTypeTemp="代码走查"; }else if(workType.equals("test")){ wortTypeTemp="測试"; }else if(workType.equals("unittest")){ wortTypeTemp="用例评审"; }else if(workType.equals("execution")){ wortTypeTemp="实施"; }else if(workType.equals("cm")){ wortTypeTemp="CM"; }else if(workType.equals("qa")){ wortTypeTemp="QA"; }else if(workType.equals("other")){ wortTypeTemp="其它"; } cl3.setCellValue(wortTypeTemp); HSSFCell cl4 = row1.createCell(4); if (cs != null) { cl4.setCellStyle(cs); } String workload = tianBao.getWorkLoad(); cl4.setCellValue(workload.equals("null")?

"":workload); HSSFCell cl5 = row1.createCell(5); if (cs != null) { cl5.setCellStyle(cs); } String documentPage = tianBao.getDocumentPage(); String d = (documentPage.equals("null")?

"":documentPage); cl5.setCellValue(d); HSSFCell cl6 = row1.createCell(6); if (cs != null) { cl6.setCellStyle(cs); } String code = tianBao.getCode(); cl6.setCellValue(code.equals("null")?

"":code); HSSFCell cl7 = row1.createCell(7); if (cs != null) { cl7.setCellStyle(cs); } String problem = tianBao.getProblem(); cl7.setCellValue(problem.equals("null")?

"":problem); HSSFCell cl8 = row1.createCell(8); if (cs != null) { cl8.setCellStyle(cs); } cl8.setCellValue(DateUtil.format(tianBao.getStartTime())); HSSFCell cl9 = row1.createCell(9); if (cs != null) { cl9.setCellStyle(cs); } cl9.setCellValue(DateUtil.format(tianBao.getEndTime())); HSSFCell cl10 = row1.createCell(10); if (cs != null) { cl10.setCellStyle(cs); } cl10.setCellValue(usernameList.get(i)); HSSFCell cl11 = row1.createCell(11); if (cs != null) { cl11.setCellStyle(cs); } cl11.setCellValue(DateUtil.format(tianBao.getIntputTime())); } noTaskExcel.write(outData); outData.flush(); outData.close(); }catch (Exception e) { LOG.error(e.getMessage(), e); } }

非表单提交

假设不是表单提交。那么一定是请求。仅仅要是请求的话,无论是以下的1、2中的哪一种,都不会打开弹出框——除非用window.open(url)发出请求。或者,在回调函数里用window.open(url)打开一个窗体(前提是server的磁盘上url这个路径下已经生成了这个excel文件)。
1.jquery请求
2.extjs请求
代码演示样例1
//js代码(注:extjs){columnWidth: .1, layout: 'form', border: false, items:[			    { xtype:'button',text: '导出Excel表格',handler: function(){//			    	Ext.Ajax.request({//			    		url :'/ProjectManagerWeb/shangbaoAction!exportExcelOfSta.action',//			        	  method :'post',//			        	  success :function(form,action){//			        		  Ext.Msg.alert("提示","success");//			        	  },//			        	  failure :function(form,action){//			        		  Ext.Msg.alert("提示","success");//			        	  }//					});				    	window.open("/ProjectManagerWeb/shangbaoAction!exportExcelOfSta.action");			    }}			]}
代码演示样例2
//js代码(注:jquery请求)var btnExportHtml;function doExport(id){	var obj=$("#div_btnExport");	btnExportHtml=obj.html();	obj.html("Exporting files, please wait...");	$.post("export.jsp",{ordId:id},function(json){		obj.html(btnExportHtml);		if(json.success){			window.open(json.uri,"","");		}	},"json");	return false;}
//java代码:业务控制器public String export() throws Exception{		SalesOrder var=handler.retrieve(form.getOrdId());		String uri="/exports/order/"+var.getOrderNo()+".xls";		String fn=application.getRealPath(uri);		SalesOrderExporter exp=new SalesOrderExporter(fn,var,application);		exp.export();		json="{\"success\":true,\"uri\":\""+uri+"\"}";		return SUCCESS;	}

转载地址:http://qsgwa.baihongyu.com/

你可能感兴趣的文章
充分利用HTML标签元素 – 简单的xtyle前端框架
查看>>
设计模式(十一):FACADE外观模式 -- 结构型模式
查看>>
iOS xcodebuile 自动编译打包ipa
查看>>
程序员眼中的 SQL Server-执行计划教会我如何创建索引?
查看>>
【BZOJ】1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路(floyd)
查看>>
cmake总结
查看>>
搜索引擎-倒排索引基础知识
查看>>
baby用品
查看>>
Jquery解析Json格式数据
查看>>
数据加密插件
查看>>
一款很不错的html转xml工具-Html Agility Pack
查看>>
linux安装中文输入法
查看>>
怎样用Java编写一段代码引发内存泄露
查看>>
2012年终总结
查看>>
eclipse导出jar包
查看>>
windows系统下利用MySql命令行进入MySql数据库
查看>>
如何隐藏日历列表的重复和全天事件栏目-方法2
查看>>
Java NIO与IO的差别和比較
查看>>
HTTP/1.1 中 If-Modified-Since 和 If-Unmodified-Since 区别简记
查看>>
怎样炒掉你的创业合作伙伴?
查看>>