How to import Excel files using Excel Buffer

by Mar 10, 2021AL Language

Home 9 Development 9 AL Language 9 How to import Excel files using Excel Buffer

Last week, I saw a question about importing data from Excel file to Business Central using AL Language. A few years ago, the only solution was to use DotNet integration. However, with the Business Central, there is a much quicker and better approach.

All functions for working with Excel files are available in a table called “Excel Buffer” (table 370 “Excel Buffer”). This table contains many useful functions to work with the Excel file, even functions for working with formulas or column style.

For importing data to Business Central, there are three crucial functions.

SelectSheetsNameStream(…)

The first of them is called SelectSheetsNameStream(). This function opens the Excel file and finds the sheet name. If the file contains only one sheet, the sheet is selected automatically. If there are more sheets defined, the user is asked to select the one to import.

OpenBookStream(…), ReadSheet()

The remaining functions are ReadSheet() and OpenBookStream().

The ReadSheet function reads the file’s content (that must be initialised using OpenBookStream at first) and stores loaded values into the Excel Buffer table.

How to load data from Excel

Once we know all these functions, we can create a procedure to load an Excel file quickly. Firstly, we need to upload the file (we can do it using the BLOBImportWithFilter function from the Codeunit 419 “File Management”). Then we need to choose the sheet we want to upload (SelectSheetsNameStream(…)) and prepare the Excel buffer table (OpenBookStream(…), ReadSheet(…)).

After that, we can just go through records in the Excel buffer table and process the table as usual. Besides row number, column number or the cell value, we have available information about cell style (bold, italic, underlined) and cell data type (date, number, text, …).


     local procedure ImportExcelFile()
     var
         TempExcelBuffer: Record "Excel Buffer" temporary;

         FileManagement: Codeunit "File Management";
         TempBlob: Codeunit "Temp Blob";

         SheetName, ErrorMessage : Text;
         FileInStream: InStream;
         ImportFileLbl: Label 'Import file';
     begin
         // Select file and import the file to tempBlob
         FileManagement.BLOBImportWithFilter(TempBlob, ImportFileLbl, '', FileManagement.GetToFilterText('', '.xlsx'), 'xlsx');

         // Select sheet from the excel file
         TempBlob.CreateInStream(FileInStream);
         SheetName := TempExcelBuffer.SelectSheetsNameStream(FileInStream);

         // Open selected sheet
         TempBlob.CreateInStream(FileInStream);
         ErrorMessage := TempExcelBuffer.OpenBookStream(FileInStream, SheetName);
         if ErrorMessage <> '' then
             Error(ErrorMessage);

         TempExcelBuffer.ReadSheet();
         if Rec.FindSet() then
             repeat
                 Message('%1, %2: %3', TempExcelBuffer."Row No.", TempExcelBuffer."Column No.", TempExcelBuffer."Cell Value as Text");
             until TempExcelBuffer.Next() < 1;
     end;

Recent Articles from the category

BC Open Source? How to start?

BC Open Source? How to start?

BC Open Source? How to start? One of the most exciting news introduced last month in Lyon during Directions EMEA 2023 was the changes to the open-source initiative. This means that you can now contribute to the source code of the Base app and the System app, which are...

read more
Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What? There are not many things in the AL Language that surprised me. However, last week, I found one such thing - I reviewed customizations made by another partner and had to analyze the OOTB code of the Demand Forecast matrix. I run...

read more
Dynamics NAV 2013 & Expired Cronus License

Dynamics NAV 2013 & Expired Cronus License

We found an interesting problem - we were not able to run the development environment for Dynamics NAV 2013. Whenever we tried to run the development client, we got the following error message: "Your program license has expired" and the development client has closed...

read more
Indirect Dependencies and Access Modifiers

Indirect Dependencies and Access Modifiers

Last week, there was a discussion on Yammer on how to get values from the "Sent Email" record when all fields are marked as Internal. I was surprised that many people do not know what can/can't access modifiers (such as local, protected, or internal) be used for. I...

read more
AL Extensions: Replace Document Attachment

AL Extensions: Replace Document Attachment

I have published a new simple, open-source extension that allows replacing existing document attachments in all master entities as well as in open documents. The source code as well as the app file that can be installed in your environment is available on my GitHub...

read more

Sign Up for News

Certifications

Highest certification
Microsoft Data Management and
also in D365 Business Central

Microsoft Certified: Dynamics 365 Business Central Functional Consultant Associate

See other certifications here