OAF_文件系列10_實現OAF將數據資料導出Excel到本地JXL(案例)

20150729 Created By BaoXinjianjava

1、摘要web


將頁面上的信息經過調用JXL腳本,將資料導出爲Exceloracle

引用的一些包方法app

1. 獲取輸出文件流dom

1.1. HttpServletResponse: 將文件寫到客戶端所引用的包post

1.2. OutputStream: 從HttpServeltResponse中導出輸出文件流測試

2. 建立Excel字體

2.1 WorkbookSetting:建立Excel的格式ui

2.2 WritableWorkbook:建立Excel文件spa

2.3  WritableSheet:建立Excel工做簿

3. 設定Excel中單元格格式

3.1  WritableFont:定義單元格格式

3.2 WritableCellFormat :定義單元格內容

 

2、實現分析


1. 建立Export Excel Button,觸發在CO中的方法,調用導出Excel功能

 

2. 設計CO中的方法

  public void processFormRequest(OAPageContext pageContext, OAWebBean webBean) { super.processFormRequest(pageContext, webBean); EmpManageAMImpl empam = (EmpManageAMImpl) pageContext.getApplicationModule(webBean); EmployeesVOImpl empvo = empam.getEmployeesSummaryVO(); if ("exportexcel".equals(pageContext.getParameter(EVENT_PARAM))) { System.out.println("exportexcel co"); empexportexcel(pageContext, empvo); } public void empexportexcel(OAPageContext pageContext, EmployeesVOImpl empvo) { HttpServletResponse response = (HttpServletResponse) pageContext.getRenderingContext().getServletResponse(); response.setCharacterEncoding("gb2312"); response.reset(); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); String filenames = "gavin_emp_export.xls"; String[] expLabels = new String[] {"Employee Id","Employee Num","Employee Name","Type","HireDate","Country","Address","Salary"}; String[] expFields = new String[] {"EmployeeId","EmployeeNum","EmployeeName","EmployeeType","HireDate","EmployeeCountry","EmployeeAddress","Salary"}; Class[] colClasses = new Class[] {null,null,null,null,null,null,null,null}; int inId = empvo.getRangeStart(); EmployeesVORowImpl currentRow = (EmployeesVORowImpl) empvo.getCurrentRow(); if (empvo == null) { return; } try{ OutputStream fileos = response.getOutputStream(); response.addHeader("Content-Disposition", (new StringBuilder()).append("attachment; filename=\"").append(filenames).append("\"").toString()); WorkbookSettings workbookSettings = new WorkbookSettings(); workbookSettings.setEncoding("ISO-8859-1"); //亂碼處理 WritableWorkbook workbook = Workbook.createWorkbook(fileos, workbookSettings); WritableSheet sheet = workbook.createSheet("Employee List Sheet",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(jxl.format.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(jxl.format.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.setBorder(jxl.format.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(jxl.format.Border.ALL,BorderLineStyle.MEDIUM,Colour.GREY_25_PERCENT); Label expLabel = null; // 頭 expLabel = new Label(0, 0, "Employee Summary Report", 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); // 內容  EmployeesVORowImpl rowfield; rowfield = (EmployeesVORowImpl) empvo.first(); if (rowfield != null) { for (int i = 0; i < expFields.length; i++) //獲取每一個字段的數據類型String/Date/Number  { colClasses[i] = rowfield.getStructureDef().lookupAttributeDef(expFields[i]).getJavaType(); } } empvo.previous(); while (empvo.hasNext()) { rowfield = (EmployeesVORowImpl) empvo.next(); int rowcount = empvo.getCurrentRowIndex() + 2; for (int i = 0, j=0; i < expFields.length; i++, j++) { if (rowfield.getAttribute(expFields[i]) != null) { if (colClasses[i].equals(Class.forName("oracle.jbo.domain.Date"))) { expLabel = new Label(j, rowcount, rowfield.getAttribute(expFields[i]).toString(), wcf_cs); //Date類型格式  } else if (colClasses[i].equals(Class.forName("oracle.jbo.domain.Number"))) { expLabel = new Label(j, rowcount, rowfield.getAttribute(expFields[i]).toString(), wcf_cn); //Number類型格式  } else if (colClasses[i].equals(Class.forName("java.lang.String"))) { expLabel = new Label(j, rowcount, rowfield.getAttribute(expFields[i]).toString(), wcf_cs); //String類型格式  } } else { expLabel = new Label(j, rowcount, "", wcf_cs); } sheet.addCell(expLabel); } } sheet.setColumnView(0, 15); //設定欄位EmployeeId長度 sheet.setColumnView(1, 15); //設定欄位EmployeeNum長度 sheet.setColumnView(2, 20); //設定欄位EmployeeName長度 sheet.setColumnView(3, 15); //設定欄位Type長度 sheet.setColumnView(4, 15); //設定欄位HireDate長度 sheet.setColumnView(5, 15); //設定欄位Country長度 sheet.setColumnView(6, 15); //設定欄位Address長度 sheet.setColumnView(7, 10); //設定欄位Salary長度 //關閉服務 sheet.getSettings().setShowGridLines(false); workbook.write(); workbook.close(); fileos.close(); response.flushBuffer(); }catch(Exception ex){ empvo.setRangeStart(inId); empvo.setCurrentRow(currentRow); throw new OAException(ex.getMessage()); } }
 

3、測試運行


1. 點擊導出ExportExcel

 

2. 頁面出現保存文件提示框

 

3. 打開導出的Excel

 

Thanks and Regards

相關文章
相關標籤/搜索