excle报盘之导出处理失败数据

本文介绍了一个用于导出处理失败数据的功能实现,包括前端JSP页面设计与后端Servlet处理逻辑。前端通过按钮触发导出操作,后端则负责生成Excel文件并下载,具体涵盖了文件路径设置、响应头配置及数据填充等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前端jsp:

<div class="blockhead">
					<span>处理失败数据导出</span>

						<form id="importForm" action="" method="post" enctype="multipart/form-data" style="float: right;margin-right: 20px;padding-top: 3px;">
							<input type="file" name="textfield" class="file" style="width:170px;" id="textfield" height="22px"    /> 
							<!--<a href="javascript:void(0)" style="background-color: #ff6600;margin-left: 0px;" onclick="importData()">导入</a> -->
							<a href="${base!}/get_export.do?Id="+id  style="background-color: #057ac5;margin-left: 0px;">导出处理失败数据</a>
	
						</form>

				</div>


servlet:

	@RequestMapping(value = "get_export.do")
	public void doExport(HttpServletRequest request, HttpServletResponse response,String id) {
		OutputStream out = null;
		try {
			String path = request.getSession().getServletContext().getRealPath("/");
			String fileName = new String(("报盘(处理失败数据).xls").getBytes("GBK"), "ISO8859_1");
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition", "attachment;filename=\"" + fileName + "\"");
			out = response.getOutputStream();
			importExcelMng.createExcelExport(path + "/WEB-INF/excelTemplate/zj-error.xls", out, aqb080Id);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

zj-error.xls存放位置:

项目根目录:

/WEB-INF/excelTemplate/zj-error.xls

处理类:

importExcelMng.createExcelExport

@Override
	public void createExcelExport(String path, OutputStream out, String Id) {
		InputStream inputSteam = null;
		//通过主表信息主键查询处理失败数据
		List<BpWeb> BpWebList = BpWebDao.queryList(Id);
		try {
			inputSteam = new FileInputStream(path);
			HSSFWorkbook wb = new HSSFWorkbook(inputSteam);
			wb.setSheetName(0, "报盘处理失败数据");
			HSSFSheet sheet = wb.getSheetAt(0);
			HSSFRow titleRow = null;
			titleRow = sheet.getRow(0);
			if (titleRow == null) {
				titleRow = sheet.createRow(0);
			}
			titleRow.setHeight((short)600);
			Cell cell0 = prepareCell(titleRow, 0);
			cell0.setCellValue("处理标志");
			Cell cell1 = prepareCell(titleRow, 1);
			cell1.setCellValue("处理日期");
			Cell cell2 = prepareCell(titleRow, 2);
			cell2.setCellValue("处理失败原因");
			Cell cell3 = prepareCell(titleRow, 3);
			cell3.setCellValue("身份证号");
			Cell cell4 = prepareCell(titleRow, 4);
			cell4.setCellValue("姓名");
			Cell cell5 = prepareCell(titleRow, 5);
			cell5.setCellValue("类别");
			Cell cell6 = prepareCell(titleRow, 6);
			cell6.setCellValue("类别2");
			Cell cell7 = prepareCell(titleRow, 7);
			cell7.setCellValue("毕业院校");
			Cell cell8 = prepareCell(titleRow, 8);
			cell8.setCellValue("毕业时间");
			Cell cell9 = prepareCell(titleRow, 9);
			cell9.setCellValue("开始年月");
			Cell cell10 = prepareCell(titleRow,10);
			cell10.setCellValue("终止年月");
			
			int rowNum = 1;
			for(int i=0;i<BpWebList.size();i++) {
				BpWeb BpWeb = BpWebList.get(i);
				HSSFRow row = null;
				row = sheet.getRow(rowNum);
				if (row == null) {
					row = sheet.createRow(rowNum);
				}
				rowNum ++;
				row.setHeight((short)400);
				Cell cell_n0 = prepareCell(row, 0);
				cell_n0.setCellValue(formatString(getA103("BP003",BpWeb.getBp003())));
				Cell cell_n1 = prepareCell(row, 1);
				cell_n1.setCellValue(formatString(BpWeb.getBp009()));
				Cell cell_n2 = prepareCell(row, 2);
				cell_n2.setCellValue(formatString(BpWeb.getBp004()));
				Cell cell_n3 = prepareCell(row, 3);
				cell_n3.setCellValue(formatString(BpWeb.getA002()));
				Cell cell_n4 = prepareCell(row, 4);
				cell_n4.setCellValue(formatString(BpWeb.getA003()));
				Cell cell_n5 = prepareCell(row, 5);
				cell_n5.setCellValue(formatString(getA103("A012",BpWeb.getA012())));
				Cell cell_n6 = prepareCell(row, 6);
				cell_n6.setCellValue(formatString(getA103("A029",BpWeb.getA029())));
				Cell cell_n7 = prepareCell(row, 7);
				cell_n7.setCellValue(formatString(BpWeb.getA045()));
				Cell cell_n8 = prepareCell(row, 8);
				cell_n8.setCellValue(formatString(BpWeb.getA046()));

			}
			
			inputSteam.close();
			wb.write(out);
			wb.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Cell prepareCell(Row row, int index) {
		Cell cell = row.getCell(index);
		if (cell == null) {
			cell = row.createCell(index);
		}
		return cell;
	}
	
	private String formatString(Object text) {
		if (text == null) {
			return "";
		}
		return String.valueOf(text);
	}


相关类 :

BpWeb等在此省略

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值