OAF_文件系列5_实现OAF将数据资料导出Excel到本地

222

 

 

      HttpServletResponse response = (HttpServletResponse) pageContext.getRenderingContext().getServletResponse();
      response.reset();
      response.setContentType("application/vnd.ms-excel;charset=UTF-8");
      String date = pageContext.getCurrentDBDate().toString();
      String filenames = date + "_Export.xls";


String[] expLabels = new String[] {"公司名称","合同名称","项目分期","付款类型","用款部门","制单人","财务审核人","款项说明","付款单号","收款单位","收款银行","收款账号","实付金额","币种","审批状态","经办日期","付出日期","合同编号","合同内款项","水费","电费","配合费","罚款","其它","发票金额","EAS导出日期"}; String[] expFields = new String[] { "CompanyName","OkcNumber","ProjectName","TypeName","PayDeptName","LastName","Approver","PaymentDesc","PaymentNumber","VendorName","BankAccountName","BankAccountNum","PaymentAmount","CurrencyCode","StatusDesc","CreationDate","PaymentDate","OkcNumber"}; Class[] colClasses = new Class[] { null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null};

OAViewObject expVO = (OAViewObject) am.findViewObject("RptPaymentRequestSummaryVO1"); Number currentPayId = null; int inId = expVO.getRangeStart(); Row currentRow = expVO.getCurrentRow(); if (expVO == null) { return; } try { OutputStream fileos = response.getOutputStream(); response.addHeader("Content-Disposition", (new StringBuilder()).append("attachment; filename=\"").append(filenames).append("\"").toString()); WorkbookSettings workbookSettings = new WorkbookSettings(); WritableWorkbook downLoad = Workbook.createWorkbook(fileos, workbookSettings); WritableSheet sheet = downLoad.createSheet("Payment Request Summary", 0); WritableFont wf_h = new WritableFont(WritableFont.ARIAL, 20, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLUE_GREY); // 定义格式 字体 下划线 斜体 粗体 颜色 WritableCellFormat wcf_h = new WritableCellFormat(wf_h); // 单元格定义 wcf_h.setAlignment(Alignment.LEFT); // 设置对齐方式 wcf_h.setBorder(Border.NONE,BorderLineStyle.NONE); WritableFont wf_t = new WritableFont(WritableFont.ARIAL, 11, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.WHITE); // 定义格式 字体 下划线 斜体 粗体 颜色 WritableCellFormat wcf_t = new WritableCellFormat(wf_t); // 单元格定义 wcf_t.setBackground(Colour.BLUE_GREY); // 设置单元格的背景颜色 wcf_t.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式 wcf_t.setVerticalAlignment(VerticalAlignment.TOP); wcf_t.setBorder(Border.ALL,BorderLineStyle.MEDIUM,Colour.GREY_25_PERCENT); WritableFont wf_cs = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色 WritableCellFormat wcf_cs = new WritableCellFormat(wf_cs); // 单元格定义 wcf_cs.setAlignment(Alignment.LEFT); // 设置对齐方式 wcf_cs.setVerticalAlignment(VerticalAlignment.TOP); wcf_cs.setWrap(true); wcf_cs.setShrinkToFit(true); wcf_cs.setBorder(Border.ALL,BorderLineStyle.MEDIUM,Colour.GREY_25_PERCENT); WritableFont wf_cn = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色 WritableCellFormat wcf_cn = new WritableCellFormat(wf_cn); // 单元格定义 wcf_cn.setAlignment(Alignment.RIGHT); // 设置对齐方式 wcf_cn.setVerticalAlignment(VerticalAlignment.TOP); wcf_cn.setBorder(Border.ALL,BorderLineStyle.MEDIUM,Colour.GREY_25_PERCENT); Row row = null; Label expLabel = null; // expLabel = new Label(0, 0, "付款情况汇总表", wcf_h); sheet.addCell(expLabel); sheet.mergeCells(0,0,2,0); sheet.setRowView(0, 500); // 标题 for (int i = 0, j=0; i < expLabels.length; i++, j++) { expLabel = new Label(j, 1, expLabels[i], wcf_t); sheet.addCell(expLabel); } sheet.setRowView(1, 400); // 内容 row = expVO.first(); if (row != null) { for (int i = 0; i < expFields.length; i++) { colClasses[i] = row.getStructureDef().lookupAttributeDef(expFields[i]).getJavaType(); } } expVO.previous(); while (expVO.hasNext()) { row = expVO.next(); int rowcount = expVO.getCurrentRowIndex() + 2; for (int i = 0, j=0; i < expFields.length; i++, j++) { if (row.getAttribute(expFields[i]) != null) { if (colClasses[i].equals(Class.forName("oracle.jbo.domain.Date"))) { expLabel = new Label(j, rowcount, ((Date) row.getAttribute(expFields[i])).toString(), wcf_cs); } else if (colClasses[i].equals(Class.forName("oracle.jbo.domain.Number"))) { expLabel = new Label(j, rowcount, ((Number) row.getAttribute(expFields[i])).toString(), wcf_cn); } else if (colClasses[i].equals(Class.forName("java.lang.String"))) { expLabel = new Label(j, rowcount, (String) row.getAttribute(expFields[i]), wcf_cs); } } else { expLabel = new Label(j, rowcount, "", wcf_cs); } sheet.addCell(expLabel); } currentPayId = (Number) row.getAttribute("PaymentRequestId"); FeeAmountVOImpl feeVO = am.getFeeAmountVO1(); feeVO.initQuery(currentPayId); FeeAmountVORowImpl feeRow = (FeeAmountVORowImpl)feeVO.first(); feeVO.previous(); int k = 18;
while (feeVO.hasNext()){ feeRow = (FeeAmountVORowImpl)feeVO.next(); Number feeAmount = feeRow.getFeeAmount(); expLabel = new Label(k, rowcount, feeAmount.toString(), wcf_cn); sheet.addCell(expLabel); k++; } if (row.getAttribute("EasDate") != null){ expLabel = new Label(25, rowcount, ((Date) row.getAttribute("EasDate")).toString(), wcf_cs); } else { expLabel = new Label(25, rowcount, "", wcf_cs); } sheet.addCell(expLabel); } sheet.setColumnView(0, 40); sheet.setColumnView(1, 25); sheet.setColumnView(2, 25); sheet.setColumnView(3, 15); sheet.setColumnView(4, 25); sheet.setColumnView(5, 25); sheet.setColumnView(6, 25); sheet.setColumnView(7, 40); sheet.setColumnView(8, 25); sheet.setColumnView(9, 25); sheet.setColumnView(10, 25); sheet.setColumnView(11, 25); sheet.setColumnView(12, 15); sheet.setColumnView(13, 10); sheet.setColumnView(14, 15); sheet.setColumnView(15, 15); sheet.setColumnView(16, 25); sheet.setColumnView(17, 15); sheet.setColumnView(18, 15); sheet.setColumnView(19, 15); sheet.setColumnView(20, 15); sheet.setColumnView(21, 15); sheet.setColumnView(22, 15); sheet.setColumnView(23, 15); sheet.setColumnView(24, 15); sheet.setColumnView(25, 25); sheet.getSettings().setShowGridLines(false); downLoad.write(); downLoad.close(); fileos.close(); response.flushBuffer(); expVO.setRangeStart(inId); expVO.setCurrentRow(currentRow); pageContext.setDocumentRendered(false); } catch (Exception e) { expVO.setRangeStart(inId); expVO.setCurrentRow(currentRow); throw new OAException(e.getMessage()); } }

 

你可能感兴趣的:(导出Excel)