0

Possible Duplicate:
How to export the result into different tabs of Excel in Toad for Data Analyst?

I have a pl/sql code where I have two procedures in it (independent of each other) and on executing the procedure I want the output to be written in one excel file but with two worksheets in it.

Ex:
for one procedure the output should be in sheet 1

for next procedure the ouput should be in sheet 2.

can any one help? i am using utl_file

0

2 Answers 2

1

If you really want to tackle this here's some code I picked up somewhere. Warning: for my purposes I abandoned this approach as I found that loading the XML data into Excel was too slow. Loading a comma-separated values file is much faster although you lose the pretty formatting (not important in my case).

To use:

  1. Call WORKBOOK_OPEN to initialize the CLOB that represents the workbook.
  2. Call WORKSHEET_OPEN to create a worksheet in the book.
  3. Use ROW_OPEN to create a row.
  4. Make a series of calls to CREATE_CELL to create and populate cells in the row.
  5. Continue making calls to ROW_OPEN and CREATE_CELL to create rows and cells.
  6. When you're done with a worksheet call WORKSHEET_CLOSE.
  7. If you need another worksheet call WORKSHEET_OPEN again to open another sheet, then use ROW_OPEN and CREATE_CELL to populate it.
  8. When you're done, call WORKBOOK_CLOSE to close the workbook.
  9. Use EXPORT_WORKBOOK_TO_FILE to write the workbook to a file.

Note that EXPORT_WORKBOOK_TO_FILE uses UTL_FILE to write its output, satisfying your requirement to use UTL_FILE.

Share and enjoy.

  PROCEDURE WORKBOOK_OPEN(pWorkbook  IN OUT NOCOPY CLOB) IS
  BEGIN
    pWorkbook := '<?xml version="1.0" encoding="ISO-8859-9"?>' || chr(10) ||
                 '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"' || chr(10) ||
                           'xmlns:o="urn:schemas-microsoft-com:office:office"' || chr(10) ||
                           'xmlns:x="urn:schemas-microsoft-com:office:excel"' || chr(10) ||
                           'xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"' || chr(10) ||
                           'xmlns:html="http://www.w3.org/TR/REC-html40">' || chr(10) ||
                   '<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">' || chr(10) ||
                     '<WindowHeight>8580</WindowHeight>' || chr(10) ||
                     '<WindowWidth>15180</WindowWidth>' || chr(10) ||
                     '<WindowTopX>120</WindowTopX>' || chr(10) ||
                     '<WindowTopY>45</WindowTopY>' || chr(10) ||
                     '<ProtectStructure>False</ProtectStructure>' || chr(10) ||
                     '<ProtectWindows>False</ProtectWindows>' || chr(10) ||
                   '</ExcelWorkbook>' || chr(10) ||
                   '<Styles>' || chr(10) ||
                     '<Style ss:ID="Default" ss:Name="Normal">' || chr(10) ||
                       '<Alignment ss:Vertical="Bottom"/>' || chr(10) ||
                       '<Borders/>' || chr(10) ||
                       '<Font/>' || chr(10) ||
                       '<Interior/>' || chr(10) ||
                       '<NumberFormat/>' || chr(10) ||
                       '<Protection/>' || chr(10) ||
                     '</Style>' || chr(10) ||
                     '<Style ss:ID="s22">' || chr(10) ||
                       '<Font x:Family="Swiss" ss:Bold="1" ss:Underline="Single"/>' || chr(10) ||
                     '</Style>' || chr(10) ||
                   '</Styles>';
  END WORKBOOK_OPEN;  

  PROCEDURE WORKBOOK_CLOSE(pWorkbook IN OUT NOCOPY CLOB) IS
  BEGIN
    pWorkbook := pWorkbook || '</Workbook>';
  END WORKBOOK_CLOSE;

  PROCEDURE WORKSHEET_OPEN(pWorkbook          IN OUT NOCOPY CLOB,
                           pstrWorksheet_name IN            VARCHAR2) IS
  BEGIN
    --
    -- Create the  worksheet
    --
    pWorkbook := pWorkbook || '<Worksheet ss:Name="' || pstrWorksheet_name || '"><Table>';
  END WORKSHEET_OPEN;

  PROCEDURE WORKSHEET_CLOSE(pWorkbook IN OUT NOCOPY CLOB) IS
  BEGIN
    pWorkbook := pWorkbook || '</Table></Worksheet>';
  END WORKSHEET_CLOSE;

  PROCEDURE ROW_OPEN(pWorkbook IN OUT NOCOPY CLOB) IS
  BEGIN
    pWorkbook := pWorkbook || '<Row>';
  END ROW_OPEN;

  PROCEDURE ROW_CLOSE(pWorkbook IN OUT NOCOPY CLOB) IS
  BEGIN
    pWorkbook := pWorkbook || '</Row>' || chr(10);
  END row_close;

  PROCEDURE CREATE_CELL(pWorkbook          IN OUT NOCOPY CLOB,
                        pstrCell_contents  IN            VARCHAR2) IS
  BEGIN
    pWorkbook := pWorkbook || '<Cell><Data ss:Type="String"> ' ||
                  pstrCell_contents || ' </Data></Cell>';
  END CREATE_CELL;

  PROCEDURE EXPORT_WORKBOOK_TO_FILE(pstrDirectory_name IN VARCHAR2,
                                    pstrFilename       IN VARCHAR2,
                                    pWorkbook          IN CLOB)
  IS
      nChunk_size    CONSTANT BINARY_INTEGER := 32767;
      strChunk       VARCHAR2(32767);
      nPos_chr10     NUMBER;
      nWorkbook_len  NUMBER;
      fHandle        UTL_FILE.FILE_TYPE;
      nPos           NUMBER := 1;
  BEGIN
      nWorkbook_len  := DBMS_LOB.GETLENGTH(pWorkbook);
      fHandle := UTL_FILE.FOPEN(pstrDirectory_name, pstrFilename, 'W', nChunk_size);

      WHILE nPos < nWorkbook_len LOOP
        strChunk := dbms_lob.substr(pWorkbook, nChunk_size, nPos);
        EXIT WHEN strChunk IS NULL;

        nPos_chr10 := INSTR(strChunk, CHR(10), -1);
        IF nPos_chr10 != 0 THEN
          strChunk := SUBSTR(strChunk, 1, nPos_chr10 - 1);
        END IF;

        UTL_FILE.PUT_LINE(fHandle, strChunk, TRUE);

        nPos := nPos + LEAST(LENGTH(strChunk)+1, nChunk_size);
      END LOOP;

      UTL_FILE.FCLOSE(fHandle);
  EXCEPTION
    WHEN OTHERS THEN
      IF UTL_FILE.IS_OPEN(fHandle) THEN
        UTL_FILE.FCLOSE(fHandle);
      END IF;

      RAISE;   
  END EXPORT_WORKBOOK_TO_FILE;
0

The common approach is to create the file with the Microsoft XML Spreadsheet (XMLSS), appending the content to a VARCHAR or CLOB.

For instance, to write a cell you would do something like this:

xmlBody := xmlBody || '<Cell><Data ss:Type="String">' || stringContent || '</Data></Cell>';

Now, to open a new worksheet you would do something like:

xmlBody := xmlBody || '<Worksheet ss:Name="' || yourWorksheetName || '"><Table>';

To close it (after appending all your content):

xmlBody := xmlBody || '</Table></Worksheet>';

There are a few utility packages that allow you to do this or you could write your own like I did. You would just have to write functions to open and close elements, for instance, openRow, closeRow, openWorkbook, closeWorkbook, openWorksheet, etc.

Refer to Oracle PL/SQL Utility Library, codename "Alexandria" for more information and examples.

1
  • But i am using utl_file..and i am confused to use xml..shall i give u code and will u edit the changes pls??.... Aug 29, 2012 at 4:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.