• 导出excel——弹出框


    表单提交

    凡是表单提交(表单提交分3种,见以下的1、2、3)的话。而且设置了表单标签的enctype="multipart/form-data"属性。那么这个时候就会打开弹出框。

    1.表单提交
    2.js表单提交
    3.jquery、extjs等等其它的表单提交


    代码演示样例
    //jsp代码
    <s:form id="myform1" method="post" enctype="multipart/form-data">
    		<table width="400">
    			<tr>
    				<td>导出</td>
    				<td align="center"><input type="button" value="导出Excel表格"
    					onclick="excelExpert()" />
    				</td>
    			</tr>
    		</table>
    	</s:form>

    //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<Object[]> 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<TianBaoEntity>和username List<String>
    			List<TianBaoEntity> tianBaoList = new ArrayList<TianBaoEntity>();
    			List<String> usernameList = new ArrayList<String>();
    			List<String> projectnameList = new ArrayList<String>();
    			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("<img src='/images/icon_loading.gif' border='0'/>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;
    	}




  • 相关阅读:
    urlEncode
    rsyslog queue队列权威指南
    rsyslog的安装、使用、详解
    长连接及心跳保活原理简介
    python时间日期格式化和反格式化
    无锁编程
    如何快速定位找出SEGV内存错误的程序Bug
    ElasticSearch搜索数据到底有几种方式?
    golang string和[]byte的对比
    PHP中使用Redis长连接笔记
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/6894740.html
Copyright © 2020-2023  润新知