4

I have no problem in writing data in excel sheet which is stored in some predefined directory.

Now I have 10 sets of data and for each set I have to create 10 seperate excel sheet. But what I want is to create Workbook conating sheet1,sheet2,. Sheet10. Which will have all 10 sets of record. If my question is not clear let me know.

I'm using PL/SQL Oracle 9i

My Code which will write to excel for one set of data. What if I have more set of data and I don't want to have multiple excel sheet instead I want one workbook with diff sheet.

CREATE OR REPLACE PROCEDURE SP_ORACLE_EXCEL(I_FILE_NAME    IN VARCHAR2) AS

  FILENAME  UTL_FILE.FILE_TYPE;
  FILENAME1 VARCHAR2(1000);
  CURSOR C1 IS
    SELECT * FROM MY_TABLE;
  VARC1 C1%ROWTYPE;
BEGIN

  FILENAME1   := 'TEST_' || I_FILE_NAME || '_' || SYSDATE || '.CSV';

  FILENAME    := UTL_FILE.FOPEN('TEMP_DIR', FILENAME1, 'W');

  /* THIS WILL CREATE THE HEADING IN EXCEL SHEET */
    UTL_FILE.PUT_LINE(FILENAME,
                   'HEADER1' || ',' || 'HEADER2' || ',' || 'HEADER3' || ',' ||
                    'HEADER4' || ',' || 'HEADER5');
  OPEN C1;
  LOOP
    FETCH C1
       INTO VARC1;
    EXIT WHEN C1%NOTFOUND;
     /*  THIS WILL PRINT THE RECORDS IN EXCEL SHEET AS PER THE QUERY IN CURSOR */
    UTL_FILE.PUT_LINE(FILENAME,
                      '"' || VARC1.COL1 || '"' || ' ,' || '"' ||
                      VARC1.COL2 || '"' || ' ,' || '"' ||
                      VARC1.COL3 || '"' || ' ,' || '"' ||
                      VARC1.COL4 || '"' || ' ,' || '"' ||
                      VARC1.COL5|| '"');


  END LOOP;

  UTL_FILE.FCLOSE(FILENAME);

END SP_ORACLE_EXCEL;
3
  • how do you write xls files with UTL_FILE? Can you share with us? Commented Dec 7, 2011 at 12:58
  • @florin code shared.. :) Commented Dec 7, 2011 at 13:36
  • 3
    OK, that is a csv file, not xls. It is a flat text file, that MS Excel knows to read. in csv, obvious, you can't have sheets. xls is more complicated. Commented Dec 7, 2011 at 13:46

3 Answers 3

6

There's no ready-to-use implementation that I'm aware of.

Excel files (.xslx) since '07 are actually zip archives containing separate xml files for each worksheet.

The XML schema they're using is pretty straight forward. You'd have to use Java to create the folders and do the zip compression in order to write such files.

Sign up to request clarification or add additional context in comments.

1 Comment

The link you posted no longer works. This link to MSDN should be the same thing though.
3

You can do this with the open source PL/SQL ExcelDocumentType. (Although I haven't tried it on 9i.)

Comments

0

I was able to find a solution to this problem recently posted here on SO (Create an Excel File (.xlsx) using PL/SQL).

i used a package called as_xlsx created by Anton Scheffer, Create an Excel-file with PL/SQL and did a few modifications to the package so it creates multiple sheets inside a single Excel Workbook by putting it through a Loop.

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.