导出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)打开一个窗口(前提是服务器的磁盘上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;
	}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-17 17:50:10

导出excel——弹出框的相关文章

Java POI 导出EXCEL经典实现 Java导出Excel弹出下载框

在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作.而数据导出的格式一般是EXCEL或者PDF,我这里就用两篇文章分别给大家介绍下.(注意,我们这里说的数据导出可不是数据库中的数据导出!么误会啦^_^) 呵呵,首先我们来导出EXCEL格式的文件吧.现在主流的操作Excel文件的开源工具有很多,用得比较多的就是Apache的POI及JExcelAPI.这里我们用Apache POI!我们先去Apach

java生成excel表格和pdf并实现下载弹出框

今天在pdf和excel中都实现了在浏览器弹出下载框 将之前在网上查找的生成excel表格代码稍微修改下: public class CreateSimpleExcelToDisk { /** * @功能:手工构建一个简单格式的Excel */ private static List<News> getNews() throws Exception { List<News> data = new ArrayList<News>(); NewsDao dao = new

安卓中的弹出框

AlertDialog.Builder builder = new Builder(Context context) 得到一个context所在页面的弹出框, 可以是单选 builder.setSingleChoiceItems(items, checkedItem,listener)  items为弹出框的条目,checkedItem为默认选择的条目(一个),listener为弹出框的点击事件监听器(DialogInterface.OnClickListener的类型,是内部类), 也可以是多

Bootstrap方法为页面添加一个弹出框

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Bootstrap 实例 - 弹出框(Popover)插件</title> <link rel="stylesheet" href="https://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap

popover弹出框

<style> #view{width: 300px;height: 200px;border: 1px solid red;} </style> 以上是为了viewport更容易看出来设置的样式 <body style="margin: 50px;"> <div id="view"> <button class="btn btn-danger btn-lg" data-toogle=&quo

js 常见弹出框学习

模拟系统的弹出框 系统自带的弹出框 总结 链接  http://blog.csdn.net/anhuidelinger/article/details/17024491 参考这个网站学习模态框的动态弹出   http://tympanus.net/codrops/2013/06/25/nifty-modal-window-effects/,网站提供打包好的资源下载. html中的基本结构: <div class="md-modal md-effect-1" id="mo

UIAlertView弹出框

<Alert弹出框提示用户信息>    1.遵循代理方法<UIAlertViewDelete>    2.调用方法UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"恭喜通关!" message:@"更多精彩,请购买下一关~~" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"购买&

UIActionSheet底部弹出框

<底部弹出框来提示用户信息>    1.遵循代理方法<UIActionSheetDelete>    2.调用放法 [UIActionSheet *sheet=[UIActionSheet alloc]initWithTitle:@“通关了!” delegate:self cancelButtonTitle:@“取消” destructiveButtonTitle:@“购买” otherButtonTitles:@“购买1”,@“购买2”,nil]    [sheet showIn

Bootstrap弹出框

弹出框3要素 modal-headermodal-bodymodal-footer <!--促发弹窗--> <a href="#" data-toggle="modal" data-target="#about">点我弹出</a> <!--弹窗代码--> <div class="modal fade" id="about"> <div cla