Importing one Excel sheet is easy with the built-in wizard. Importing a workbook with related parent and child sheets — and keeping the keys straight — takes a bit more care. Here's the full pattern.
Excel is still the undisputed king of business data. As Oracle APEX developers, we're frequently asked to build import utilities. While importing a single-sheet Excel file in APEX is straightforward using the built-in wizard or APEX_DATA_PARSER, things get trickier when a single workbook contains multiple worksheets with relational data — like a parent header and child lines.
In this post, we'll parse a multi-sheet Excel file, assign separate database sequences for primary keys, maintain the parent-child relationship, and stage everything into APEX Collections for reporting.
The Scenario
A user uploads a single workbook containing two worksheets:
- Parent_Department — a single row defining the main department details.
- Child_Department — multiple rows listing child sub-departments or teams.
The goal: parse both sheets from one upload, assign a unique primary key to the parent, link each child row to that parent via foreign key, generate unique keys for the child rows, and display both in separate reports.
- Create a new blank page (or edit an existing one) in your APEX application.
- Add a File Browse page item named
:P7_UPLOAD. - Set Storage Type to Table APEX_APPLICATION_TEMP_FILES and Purge Action to End of Session (or a custom cleanup).
- Add a Submit button to trigger page processing.
Create a page process that fires On Submit. This process retrieves the file, resolves the worksheet names, clears the target collections, and parses both sheets.
Code Walkthrough
File retrieval. The file uploaded through :P7_UPLOAD is temporarily stored in apex_application_temp_files. We retrieve its BLOB content and filename to pass to the parser.
Resolving sheet names. Excel stores worksheet names as display labels (e.g. Parent_Department) but references them internally by sheet ID (e.g. sheet1.xml). apex_data_parser.get_xlsx_worksheets returns that mapping, so we match the user-facing name to the correct internal file.
Collection management. apex_collection.create_or_truncate_collection clears any previous upload, giving the user a fresh staging area on every submit.
Sequence-based relational keying. The parent row gets its ID from parent_dept_seq.nextval, stored in p_n001. Each child row gets its own unique ID from child_dept_seq.nextval in p_n001, and carries the parent's ID as a foreign key in p_n002 — that's what keeps the relationship intact once both sheets land in separate collections.
With the data staged, use standard APEX report regions (Classic or Interactive) to display each collection.
Comments
Post a Comment