web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Business Central forum

Import Excel file using AL extension - D365 BC

(0) ShareShare
ReportReport
Posted on by 154

Hello Friends,

We have a requirement where in we need to import and Excel file into Project Journal using AL extensions (D365 BC SAAS model).

I researched online and found very few blog on this, hence I thought of writing it here.

However, I would like to thank the below blog for the source code:

https://community.dynamics.com/business/b/dynamics365businesscloudfronts/posts/workaround-to-edit-in-excel-functionality-with-import-export-using-excel-buffer-in-business-central

codeunit 50101 "Import Excel to Job Journal"
{
    trigger OnRun()
    begin
        ImportJobJournalExcel();
    end;

    procedure ImportJobJournalExcel()
    begin
        Rec_ExcelBuffer.DeleteAll();
        Rows := 0;
        Columns := 0;
        DialogCaption := 'Select File to upload';

        UploadResult := UploadIntoStream(DialogCaption, '', '', Name, NVInStream);

        If Name <> '' then
            Sheetname := Rec_ExcelBuffer.SelectSheetsNameStream(NVInStream)
        else
            exit;
        Rec_ExcelBuffer.Reset();
        Rec_ExcelBuffer.OpenBookStream(NVInStream, Sheetname);
        Rec_ExcelBuffer.ReadSheet();
        Commit();
        //Message('sheet %1', Sheetname);

        //finding total number of Rows to Import
        Rec_ExcelBuffer.Reset();
        Rec_ExcelBuffer.SetRange("Column No.", 1);
        If Rec_ExcelBuffer.FindFirst() then
            repeat
                Rows := Rows + 1;
            until Rec_ExcelBuffer.Next() = 0;
        Message('No. of rows %1', Rows);

        //Finding total number of columns to import
        Rec_ExcelBuffer.Reset();
        Rec_ExcelBuffer.SetRange("Row No.", 1);
        if Rec_ExcelBuffer.FindFirst() then
            repeat
                Columns := Columns + 1;
            until Rec_ExcelBuffer.Next() = 0;


        //Function to Get the last line number in Job Journal
        LineNo := FindLineNo();

        for RowNo := 2 to Rows do begin
            RecJobJnl.Init();
            Evaluate(RecJobJnl."Journal Template Name", GetValueAtIndex(RowNo, 1));
            Evaluate(RecJobJnl."Journal Batch Name", GetValueAtIndex(RowNo, 2));
            RecJobJnl."Line No." := LineNo;
            RecJobJnl.Type := RecJobJnl.Type::"G/L Account";

            Evaluate(RecJobJnl."Location Code", GetValueAtIndex(RowNo, 4));
            RecJobJnl.Validate(RecJobJnl."Location Code");
            Evaluate(RecJobJnl."Shortcut Dimension 1 Code", GetValueAtIndex(RowNo, 5));
            RecJobJnl.Validate(RecJobJnl."Shortcut Dimension 1 Code");
            //Evaluate(RecJobJnl."Shortcut Dimension 3 Code", GetValueAtIndex(RowNo, 6));
            Evaluate(RecJobJnl."Job No.", GetValueAtIndex(RowNo, 7));
            RecJobJnl.Validate(RecJobJnl."Job No.");
            Evaluate(RecJobJnl."Job Task No.", GetValueAtIndex(RowNo, 8));
            RecJobJnl.Validate(RecJobJnl."Job Task No.");
            Evaluate(RecJobJnl."No.", GetValueAtIndex(RowNo, 3));
            RecJobJnl.Validate("No.", RecJobJnl."No.");
            Evaluate(RecJobJnl."Document No.", GetValueAtIndex(RowNo, 9));

            Evaluate(TxtDate, GetValueAtIndex(RowNo, 10));
            //CopyStr function used to convert Document Date Text value to a Date value
            Evaluate(DocumentDate, CopyStr(TxtDate, 1, 2) + CopyStr(TxtDate, 4, 2) + CopyStr(TxtDate, 7, 4));
            RecJobJnl.Validate("Document Date", DocumentDate);
            RecJobJnl.Validate("Posting Date", DocumentDate);
            Evaluate(RecJobJnl.Description, GetValueAtIndex(RowNo, 11));
            Evaluate(RecJobJnl.Quantity, GetValueAtIndex(RowNo, 12));
            RecJobJnl.Validate(RecJobJnl.Quantity);
            RecJobJnl.Insert();

            LineNo := LineNo + 10000;

        end;
        Message('Import Completed');
    end;

    local procedure GetValueAtIndex(RowNo: Integer; ColNo: Integer): Text
    var
        Rec_ExcelBuffer: Record "Excel Buffer";
    begin
        Rec_ExcelBuffer.Reset();
        If Rec_ExcelBuffer.Get(RowNo, ColNo) then
            exit(Rec_ExcelBuffer."Cell Value as Text");
    end;

    local procedure FindLineNo(): Integer

    var
        JobJnl: Record "Job Journal Line";
        LineNo: Integer;

    begin
        JobJnl.Reset();
        JobJnl.SetFilter("Journal Template Name", 'JOB');
        JobJnl.SetCurrentKey(JobJnl."Line No.");
        If JobJnl.FindLast() then
            LineNo := JobJnl."Line No." + 10000
        else
            LineNo := 10000;
        Message('Line number %1', LineNo);
        exit(LineNo);

    end;

    var
        Rec_ExcelBuffer: Record "Excel Buffer";
        Rows: Integer;
        Columns: Integer;
        Fileuploaded: Boolean;
        UploadIntoStream: InStream;
        FileName: Text;
        Sheetname: Text;
        UploadResult: Boolean;
        DialogCaption: Text;
        Name: Text;
        NVInStream: InStream;

        RecJobJnl: Record "Job Journal Line";
        RowNo: Integer;
        TxtDate: Text;
        DocumentDate: Date;

        LineNo: Integer;


}
Categories:
  • Suggested answer
    Community Member Profile Picture
    on at

    Try to use below code :

    Field(FileName;FileName)

    {

      ApplicationArea = All;

      Trigger OnLookup(VAR Text : Text) : Boolean

      Begin

        FileName :=

              FileManagement.OpenFileDialog('Import File','','Word Files (*.XML)|*.XML|All Files (*.*)|*.*');

      End;

    }

    Var

    FileMgmt : Codeunit “File Management”;

  • Hasnain Ali Profile Picture
    136 on at

    Hi Ashish Kokney,

    Thanks for sharing.

    But at line "Sheetname := Rec_ExcelBuffer.SelectSheetsNameStream(NVInStream)"

    If the file extension is not "xlsx" then it throws an error.

    Excel file could be also of extension "xls"

    error message:

    A call to Microsoft.Dynamics.Nav.OpenXml.Spreadsheet.WorkbookReader.Open failed with this message: File contains corrupted data.



    If got this error in all SaaS OnPrem and NAV

    In NAV we convert file from xls to xlsx. It saves the file on server, but this cannot be done on SaaS.

    Can you help with this?



  • Suggested answer
    IB-29041624-0 Profile Picture
    1,191 Moderator on at

    I think your options is to either upgrade to Excel 2007 or later and start using xlsx files or you can store your files on Azure Blog storage, convert them there and read them from the blob storage into BC Online.

  • Suggested answer
    YUN ZHU Profile Picture
    104,123 Super User 2026 Season 2 on at

    Hi, hope the following helps as well.

    https://yzhums.com/4223/

    Thanks.

    ZHU

  • Hasnain Ali Profile Picture
    136 on at

    Hi yzhums 

    Thanks for sharing your blog.

    But I'm issue converting xls file to xlsx when importing Excel file.

    Code throws an error: 

    A call to Microsoft.Dynamics.Nav.OpenXml.Spreadsheet.WorkbookReader.Open failed with this message: File contains corrupted data.


    On line:

    Screenshot-2023_2D00_02_2D00_21-at-11.20.45-PM.png

    The only way I've tackled that is by adding filter of file extension that can be imported!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Business Central

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans