PL/SQL UTL_FILE Excel Export - Style Issues When Opening File

I’m using a custom package named DataExportUtils to generate Excel files from queries in PL/SQL. Though the file is created, I encounter an error when opening it with Excel:

Error occurred in the given section during file load: Style

I suspect the issue lies with this header setup:

-- Configure headers
owa_util.mime_header('application/vnd.ms-excel', FALSE);

I attempted to add an XML declaration:

<?xml version="1.0" encoding="UTF-8"?>

However, this altered the error to: Error occurred in the given section during file load: 'Strict Parse Error'

Here’s the complete code for my procedure:

/* To execute, use */
execute departmentreport;

CREATE OR REPLACE PROCEDURE departmentReport AS

   v_query_wages      VARCHAR2(200) := 'SELECT surname,given_name,wage FROM hr.staff ORDER BY surname,given_name';
   v_query_details    VARCHAR2(200) := 'SELECT surname,given_name,phone,email_addr FROM hr.staff ORDER BY surname,given_name';
   v_query_startdate  VARCHAR2(200) := 'SELECT surname,given_name,to_char(start_date,''MM/DD/YYYY'') start_date FROM hr.staff ORDER BY surname,given_name';
   reportDocument     ExcelDocumentType := ExcelDocumentType();
   v_sheet_record     DataExportUtils.T_WORKSHEET_DATA := NULL;
   v_sheet_collection DataExportUtils.WORKSHEET_TABLE := DataExportUtils.WORKSHEET_TABLE();
   fileContent        ExcelDocumentLine := ExcelDocumentLine();
   v_output_file      UTL_FILE.FILE_TYPE;

BEGIN

    -- Wages sheet
    v_sheet_record.query           := v_query_wages;
    v_sheet_record.worksheet_name  := 'Staff_Wages';
    v_sheet_record.col_count       := 3;
    v_sheet_record.col_width_list  := '30,25,18';
    v_sheet_record.col_header_list := 'Surname,Given_Name,Wage';

    v_sheet_collection.EXTEND;
    v_sheet_collection(v_sheet_collection.count) := v_sheet_record;

    -- Details sheet
    v_sheet_record.query           := v_query_details;
    v_sheet_record.worksheet_name  := 'Staff_Details';
    v_sheet_record.col_count       := 4;
    v_sheet_record.col_width_list  := '30,25,22,28';
    v_sheet_record.col_header_list := 'Surname,Given_Name,Phone,Email';

    v_sheet_collection.EXTEND;
    v_sheet_collection(v_sheet_collection.count) := v_sheet_record;

    -- Start dates sheet
    v_sheet_record.query           := v_query_startdate;
    v_sheet_record.worksheet_name  := 'Start_Dates';
    v_sheet_record.col_count       := 3;
    v_sheet_record.col_width_list  := '30,25,22';
    v_sheet_record.col_header_list := 'Surname,Given_Name,Start_Date';

    v_sheet_collection.EXTEND;
    v_sheet_collection(v_sheet_collection.count) := v_sheet_record;
    
    owa.num_cgi_vars := NVL(owa.num_cgi_vars, 0);
    reportDocument := DataExportUtils.createExcelDocument(v_sheet_collection);
    fileContent := reportDocument.getDocumentData;

    v_output_file := UTL_FILE.fopen('C:\\','report.xml','W',4000);

    FOR i IN 1 .. fileContent.COUNT LOOP
        UTL_FILE.put_line(v_output_file,fileContent(i));
    END LOOP;

    UTL_FILE.fclose(v_output_file);

END;

Does anyone have insights into what could be causing these style errors while opening the generated file in Excel?

your XML declaration might be messing things up because it’s not encoded right. just ditch the XML declaration completely and stick with owa_util.mime_header. also double-check that DataExportUtils is actually creating proper Excel XML - I’ve seen these custom packages spit out basic XML that Excel chokes on.

Interesting issue! Are you generating actual Excel XML format or just CSV with an XML extension? What’s your DataExportUtils.createExcelDocument method actually outputting? Can you share a sample of the generated XML?

This style error happens when Excel hits malformed or missing style definitions in the XML. You’re saving as .xml but using the application/vnd.ms-excel MIME type - that’s a mismatch right there. I’ve run into this same issue with custom Excel generators in PL/SQL. Usually it’s incomplete XML namespaces or missing workbook structure elements causing the problem. Try switching the file extension to .xls if your DataExportUtils package generates Excel Binary format instead of proper Office Open XML. Or make sure your package includes proper Excel XML schema declarations and style sheet definitions. Can’t see your actual XML output, but I’m betting your custom package generates simplified XML that’s missing the required Excel formatting elements.