前端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等在此省略
新闻热点
疑难解答