Below is the example to create an excel file in Oracle Forms.

Pass the Sql query string to the below procedure to generate an Excel file using Ole2 package.

PROCEDURE Create_Excel_File (CSQL Varchar2)
Is
   source_cursor    Integer;
   l_colCnt         Number            Default 0;
   l_descTbl        Dbms_sql.desc_tab;
   newval1          Varchar2 (4000);
   application      OLE2.OBJ_TYPE;
   workbooks        OLE2.OBJ_TYPE;
   workbook         OLE2.OBJ_TYPE;
   worksheets       OLE2.OBJ_TYPE;
   worksheet        OLE2.OBJ_TYPE;
   colour           OLE2.OBJ_TYPE;

   cell             OLE2.OBJ_TYPE;
   RANGE            OLE2.OBJ_TYPE;
   range_col        OLE2.OBJ_TYPE;
   range_row        OLE2.OBJ_TYPE;
   args             OLE2.LIST_TYPE;
   rows_processed   Number;
   row_n            Number;
   VAL              Varchar2 (100);
   x                Number;
   filename         Varchar2 (200);
BEGIN
   BEGIN
      source_cursor := Dbms_Sql.open_Cursor;
      Dbms_Sql.parse (source_cursor, cSql, 2);
      Dbms_Sql.describe_Columns (c => source_cursor, col_cnt => l_colCnt, desc_t => l_descTbl);
   EXCEPTION
      When Others
      Then
         Error_Message (SQLERRM);
         RETURN;
   END;

   application := OLE2.CREATE_OBJ ('Excel.Application');
   OLE2.SET_PROPERTY (application, 'Visible', 'False');
   workbooks := OLE2.GET_OBJ_PROPERTY (application, 'Workbooks');
   workbook := OLE2.GET_OBJ_PROPERTY (workbooks, 'Add');
   worksheets := OLE2.GET_OBJ_PROPERTY (workbook, 'Worksheets');
   args := OLE2.CREATE_ARGLIST;
   OLE2.ADD_ARG (args, 1);
   worksheet := OLE2.GET_OBJ_PROPERTY (worksheets, 'Item', args);
   OLE2.DESTROY_ARGLIST (args);

   For T In 1 .. l_colCnt
   LOOP
      BEGIN
         Dbms_Sql.define_Column (source_cursor, T, newval1, 4000);
         args := OLE2.CREATE_ARGLIST;
         OLE2.ADD_ARG (args, 1);
         OLE2.ADD_ARG (args, T);                                                      --Next column
         cell := OLE2.GET_OBJ_PROPERTY (worksheet, 'Cells', args);
         OLE2.DESTROY_ARGLIST (args);
         colour := ole2.get_obj_property (cell, 'Borders');
         ole2.set_property (colour, 'ColorIndex', 1);
         ole2.Release_obj (colour);
         colour := ole2.get_obj_property (cell, 'Interior');
         ole2.set_property (colour, 'ColorIndex', 15);
         ole2.Release_obj (colour);
         OLE2.SET_PROPERTY (cell, 'Value', l_descTbl (T).col_name);
         OLE2.Release_obj (cell);
      EXCEPTION
         When Others
         Then
            Null;
      END;
   END LOOP;

   Rows_processed := Dbms_Sql.EXECUTE (source_cursor);
   row_n := 1;

   LOOP
      IF Dbms_Sql.fetch_Rows (source_cursor) > 0
      Then
         For T In 1 .. l_colCnt
         LOOP
            BEGIN
               Dbms_Sql.column_Value (source_cursor, T, newval1);
               args := OLE2.CREATE_ARGLIST;
               OLE2.ADD_ARG (args, row_n + 1);
               OLE2.ADD_ARG (args, T);                                                --Next column
               cell := OLE2.GET_OBJ_PROPERTY (worksheet, 'Cells', args);
               OLE2.DESTROY_ARGLIST (args);
               colour := ole2.get_obj_property (cell, 'Borders');
               ole2.set_property (colour, 'ColorIndex', 1);
               ole2.Release_obj (colour);
               OLE2.SET_PROPERTY (cell, 'Value', newval1);
               OLE2.Release_obj (cell);
            EXCEPTION
               When Others
               Then
                  EXIT;
            END;

            newval1 := Null;
         END LOOP;
      Else
         EXIT;
      END IF;

      row_n := row_n + 1;
   END LOOP;

   Dbms_Sql.close_Cursor (source_cursor);
   -- Autofit columns
   RANGE := OLE2.GET_OBJ_PROPERTY (worksheet, 'UsedRange');
   range_col := OLE2.GET_OBJ_PROPERTY (RANGE, 'Columns');
   range_row := OLE2.GET_OBJ_PROPERTY (RANGE, 'Rows');
   OLE2.INVOKE (range_col, 'AutoFit');
   OLE2.INVOKE (range_row, 'AutoFit');
   OLE2.Release_obj (RANGE);
   OLE2.Release_obj (range_col);
   OLE2.Release_obj (range_row);
   -- Get filename and path
   filename :=  'Yourexcel.xls';

   -- Save as worksheet
   IF Nvl (filename, '0') <> '0'
   Then
      OLE2.SET_PROPERTY (application, 'Visible', 'True');
      args := OLE2.CREATE_ARGLIST;
      OLE2.ADD_ARG (args, filename);
      OLE2.INVOKE (worksheet, 'SaveAs', args);
      OLE2.DESTROY_ARGLIST (args);
   END IF;

--  OLE2.INVOKE( workbook ,'Close');
   OLE2.Release_obj (worksheet);
   OLE2.Release_obj (worksheets);
   OLE2.Release_obj (workbook);
   OLE2.Release_obj (workbooks);
   OLE2.Release_obj (application);
END;

Download demo form from the following link Generate Excel Report

Creating Excel File in Oracle Forms的更多相关文章

  1. Formatting Excel File Using Ole2 In Oracle Forms

    Below is the some useful commands of Ole2 to format excel file in Oracle Forms.-- Change font size a ...

  2. Create Hierarchical Tree To Control Records In Oracle Forms

    Download Source Code Providing an example form for creating hierarchical trees in Oracle Forms to co ...

  3. Changing Icon File Of Push Button At Runtime In Oracle Forms 6i

    Set Icon_File property in When-Mouse-Enter trigger Suppose you are creating icon based menu system i ...

  4. Freebie - Utility Form: Generate Excel Report From SQL Query In Oracle Forms 6i And 11g

    Sharing a form to generate Excel file report from SQL query in Oracle Forms. This form can be used i ...

  5. Writing Text File From A Tabular Block In Oracle Forms

    The example given below for writing text file or CSV using Text_IO package from a tabular block in O ...

  6. Creating, Stopping, Re-Starting and Deleting a Timer in Oracle Forms

    I have written many posts previously on Timers in Oracle Forms like how to change images randomly wi ...

  7. Creating Custom Login Screen In Oracle Forms 10g

    Below is the example plsql unit to validate login credentials and after successful validation open a ...

  8. Creating Dynamic LOV in Oracle D2k Forms

    Dynamic Lov is a good idea for the form where too many Lov requirement is there with different recor ...

  9. Oracle Forms 10g Tutorial Ebook Download - Oracle Forms Blog

    A step by step tutorial for Oracle Forms 10g development. This guide is helpful for freshers in Orac ...

随机推荐

  1. zw版_Halcon图像交换、数据格式、以及超级简单实用的DIY全内存计算.TXT

    zw版_Halcon图像交换.数据格式.以及超级简单实用的DIY全内存计算.TXT Halcon由于效率和其他原因,内部图像采用了很多自有格式,提高运行速度,但在数据交换方面非常麻烦. 特别是基于co ...

  2. HGE游戏引擎之hgeQuad结构体的使用(用于渲染图片)

    HGE基本的渲染图元是hgeQuad (Quad is the basic HGE graphic primitive),其中有一个hgeVertex成员结构,它用来描述图元顶点信息.The hgeV ...

  3. java类的访问权限

    1.解析 Java有四种访问权限, 其中三种有访问权限修饰符,分别为private,public和protected,还有一种不带任何修饰符. private: Java语言中对访问权限限制的最窄的修 ...

  4. ID3DXMesh接口 创建自己的立方体网格

    D3DXCreateMeshFVF 首先创建一个ID3DXMesh接口. ID3DXMesh接口的说明可以参数龙书. 这里主要是用代码来讲解: #define VERTEX_FVF (D3DFVF_X ...

  5. laravel的安装

    安装composer http://docs.phpcomposer.com/download/ curl -sS https://getcomposer.org/installer | php mv ...

  6. Linux 下多核CPU知识【转】

    转自:http://www.cnblogs.com/dongzhiquan/archive/2012/02/16/2354977.html 1. 在Linux下,如何确认是多核或多CPU: #cat ...

  7. java网络编程之UDP通讯

    详细介绍了java中的网络通信机制,尤其是UDP协议,通过对UDP的基本使用进行举例说明如何使用UDP进行数据的发送接收,并举了两个小demo说明UDP的使用注意事项. UDP协议原理图解: UDP协 ...

  8. Multi-source Replication

    MariaDB starting with 10.0.1 Multi-source replication means that one server has many masters from wh ...

  9. Mysql slow query log

    一.概念部分:  顾名思义,慢查询日志中记录的是执行时间较长的query,也就是我们常说的slow query,通过设--log-slow-queries[=file_name]来打开该功能并设置记录 ...

  10. JavaEE基础(九)

    1.面向对象(多态的概述及其代码体现) A:多态(polymorphic)概述 事物存在的多种形态 B:多态前提 a:要有继承关系. b:要有方法重写. c:要有父类引用指向子类对象. C:案例演示 ...