zoukankan      html  css  js  c++  java
  • OAF客制化代码导出页面数据到Excel文件

    项目开发过程中经常遇到要求导出页面的数据,oaf有标准控件支持,但是灵活性不够,这里介绍一下导出数据到excel的客制化方法,有不正确的地方,欢迎大家指正。

    co中processFormRequest方法中捕捉导出按钮事件,然后调用ExportUtility类中的导出方法。

    代码:

    // 导出按钮事件
    
    if("export".equals(pageContext.getPrameter(EVENT_PARAM))) {
    
      //  文件名
    
      String fileName = "xxx.xls";
    
      // 获取需要导出的数据集
    
      LinkedHashMap map = new LinkedHashMap();
    
      OAViewObject viewObject =(OAViewObject)am.findViewObject("xxxxVO1");
    
      map.put("excel字段名1", "vo字段名1");
    
      map.put("excel字段名2", "vo字段名2");
    
      // 调用ExportUtil类中的导出方法
    
      ExportUtil.exportExcel(pageContext, viewObject, map, fileName);
    
    }

    ExportUtil类中的exportExcel(下载)方法如下:

    /*
    
     * 导出数据到excel通用方法
    
     * @param pageContext
    
     * @param viewObejct 数据源
    
     * @param columnMap 列及其对于数据源关系
    
     * @param fileName  文件名
    
    */
    
    public static void exportExcel(OAPageContext pageContext,
    
                                                OAViewObject viewObject,
    
                                                LinkedHashMap columnMap,
    
                                                String fileName) {
    
      // 判断参数是否正确
    
      if(pageContext == null ||
    
         viewObject == null ||
    
         columnMap.size() < 1) {
    
        return;
    
      }
    
     
    
      // 穿件sheet页
    
      HSSFWorkbook hWorkbook = new HSSFWorkbook();
    
      HSSFSheet hSheet = hWorkbook.createSheet();
    
     
    
      // 设置excel列宽
    
      for(int i = 0; i < columnMap.size(); i++) {
    
        hSheet.setColumnWidth(i, 5000);
    
      }
    
     
    
      // 设置excel头行样式
    
      Object[] keyColumn = columnmap.keySet().toArray();
    
      HSSFCellstyle headerStyle = hWorkbook.createCellstyle();
    
      headerStyle.setFillPattern(Short.parseShort("1"));
    
      headerStyle.setFillForegroundColor(HSSFColor.YELLOW.index);
    
     
    
      // 创建头行
    
      createExcelRow(hSheet, headerStyle, keyColumn, 0);
    
     
    
      // 将vo中的数据插入到excel中
    
      int xRowCnt = 0;
    
      viewObject.setRangeStart(0);
    
      viewObject.setRangeSize(viewObject.getRowCount());
    
      for(int i = 0; i < viewObject.getRowCount(); i++) {
    
        Row row = viewObject.getRowAtRangeIndex(i);
    
        // 循环每一行每一列插入excel
    
        HSSFRow xRow = hSheet.createRow(++xRowCnt);
    
        for(int j = 0; j< keyColumn.length, j++) {
    
          // 讲数据插入单元格
    
          HSSFCell xCell = xRow.createCell(j);
    
          Object attrValue = row.getAttribute((String)columnMap.get(keyColumn[j]));
    
          if(attrValue instansof oracle.jbo.domain.Number) {
    
            xCell.setCellValue((oracle.jbo.domain.Number)attrValue).doubleValue(1);
    
          } else if(attrValue instansof oralce.jbo.domain.Date) {
    
             SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    
             Date date = ((oracle.jbo.domain.Date)attrValue).getValue();
    
             xCell.setCellValue(dateFormat.format(date));
    
          } else {
    
            xCell.setCellValue((String)attrValue)
    
          }
    
        }
    
      }
    
      // 处理下载文件名
    
      if(fileName == null || "".equlse(fileName.trim())) {
    
        fileName = "DownloadExcel.xls";
    
      } else if(!"xls".equalsIgnoreCase(fileName.substring(fileName.lastIndexOf(".")+1))) {
    
        fileName = fileName + ".xls";
    
      }
    
      // 弹出下载框
    
      downloadExcel(pageContext, hWorkbook, fileName);
    
    }

    ExportUtil类中的exportExcel(下载)方法会调用如下两个方法

    /*
    
      * 创建头行
    
      *@param xSheet
    
      *@param xCellStyle
    
      *@param columnValues
    
      *@param rowIindex
    
    */
    
    public static void createExcelRow(HSSFSheet xSheet,
    
                                                      HSSFCellStyle xCellStyle,
    
                                                      Object[] columnValues,
    
                                                      int rowIndex) {
    
      HSSFRow xRow = xSheet.createRow(rowIndex);
    
      for(int i = 0; i< columnValues.length; i++) {
    
        HSSFCell xCell = xRow.createCell(i);
    
        if(xCellStyle != null) {
    
          xCell.setCellStyle(xCellStyle);
    
        }
    
        xCell.setCellValue(columnvalues[i].toString());
    
      }
    
    }
    
     
    
    /*
    
      *弹出下载excel文档框
    
      *param pageContext
    
      *param xWorkbook
    
      *param fileName
    
    */
    
    public static void downloadExcel(OAPageContext pageContext,
    
                                                    Workbook xWorkbook,
    
                                                    String fileName) {
    
      DateObject sessionDictionary = pageContext.getNameDateObject("_SessionParameters");
    
      HttpServletResponse response = (HttpServletResponse)sessionDictionary.selectValue(null, "HttpServletResponse");
    
      response.setContentType("application/vnd.ms-excel; charset = UTF-8");
    
      // 创建excel工作薄
    
      try{
    
        response.setHeader("Content-disposition", "attachment; filename="+URLEncoder.encode(fileName, "UTF-8"));
    
        xWorkbook.write(response.getOutputStream());
    
      } catch(Exception e) {
    
        throw new OAException(e.getMessage());
    
      }
    
    }

    以上下载的方法同样使用于Java开发中。

    以上若有不对之处,请不吝指正。若有其他更好的方法,望不吝赐教

  • 相关阅读:
    OAuth2 协议原理简析及Azure AD OAuth2示例
    MySQL Innodb MVCC(多版本并发控制)
    乐观锁和悲观锁
    数据库索引
    SQL盲注
    JAVA array to list and list to array
    缓存穿透,缓存雪崩和缓存击穿
    四、Spring Cloud 之旅 -- Ribbon 负载均衡
    JavaScript中 require、import 有什么区别?
    vue 2.x Vue 3.x 日常采坑之 设置alias别名、background引入图片、全局引入scss文件 的问题
  • 原文地址:https://www.cnblogs.com/running-fish/p/9472757.html
Copyright © 2011-2022 走看看