Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Unanswered

Export an Excel file that contains a certain layout and design

Posted on by 82

Hello,

I am exporting an Excel to make a price comparison of certain offers and I have encountered two problems:

1) Creating the Excel from the beginning using the Excel Buffer table, it is not possible to format the cells (except bold, italic and underlined).

2) Seeing that problem 1 seems complex, I have created a template on the server that has a default layout associated with it. Well, I use the Excel Buffer table to fill in the data but the format of the cells that are written is lost... and that's where I need help. Do you know how I can solve this problem? I show you two images of a small simplified test so you can see what happens as well as the associated code:

1881.Before.PNG        4061.After.PNG

codeunit 50013 "Files Mgt2"
{
    trigger OnRun()
    begin

    end;

    procedure ExportQuotesToExcel(var Quote: Record "Purchase Header")
    var
        TempExcelBuf: Record "Excel Buffer" temporary;
    begin
        InitExcel(TempExcelBuf);
        FillExcelBuffer(TempExcelBuf, Quote);
        TempExcelBuf.CloseBook();
        TempExcelBuf.SetFriendlyFilename('test');
        TempExcelBuf.OpenExcel();
    end;

    local procedure InitExcel(var TempExcelBuf: Record "Excel Buffer" temporary)
    var
        InStr: InStream;
        Files: File;
    begin
        Files.Open('C:\Users\Public\Comparador ofertas compra.xlsx');
        Files.CreateInStream(InStr);

        TempExcelBuf.UpdateBookStream(InStr, 'Precios', false);
    end;

    local procedure FillExcelBuffer(var TempExcelBuf: Record "Excel Buffer" temporary; var Quote: Record "Purchase Header")
    var
        TempExcelBufSheet: Record "Excel Buffer" temporary;
    begin
        TempExcelBufSheet.EnterCell(TempExcelBufSheet, 2, 1, 'test', true, false, false);
        TempExcelBuf.WriteAllToCurrentSheet(TempExcelBufSheet);
    end;


}


The version of BC we use is 16.1 (we know that there are new Excel features currently available, but we are not migrating for now).

Thank you so much.

Categories:
  • MiguelRospa Profile Picture
    MiguelRospa 82 on at
    RE: Export an Excel file that contains a certain layout and design

    Thank you very much for answering Steven but I will answer the same as Inge, our version is 16.1 and in that version the features that you expose in the links are not available. Actually using Excel Buffer is not complicated, what is incredible is that it only allows you to format cells in bold, italics and underlined. And the other option that I explained at the beginning of the post does not work either because in addition to not being able to format with Excel Buffer, the format of the cells that are written from a template hosted on the server is changed.

    It does not seem that this has a solution without migrating from version...

  • MiguelRospa Profile Picture
    MiguelRospa 82 on at
    RE: Export an Excel file that contains a certain layout and design

    Thank you very much for answering Inge. In the version we are using (16.1) the options described in the link you have provided me do not appear. For example, I can't choose the "Send to > Microsoft Excel Document (data only)" option in the task 1 steps and I can't "Set Format Options to Excel" during the task 2 steps either.

  • Steven Renders Profile Picture
    Steven Renders 5,043 Moderator on at
    RE: Export an Excel file that contains a certain layout and design

    I don't recommend to use the Excel Buffer, it seems way to complex to achieve a simple Excel layout.

    Instead create a report, with a normal dataset and then add an Excel layout to it.

    Here are some examples/tutorials:

    thinkaboutit.be/.../

    thinkaboutit.be/.../

  • Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,744 Moderator on at
    RE: Export an Excel file that contains a certain layout and design

    Why cant you create a report that you define a Excel layout for?

    Then the report gives you all the raw data in Excel you can do the formatting and upload that layout so it can be reused when you run the same report again?

    learn.microsoft.com/.../ui-excel-report-layouts

  • MiguelRospa Profile Picture
    MiguelRospa 82 on at
    RE: Export an Excel file that contains a certain layout and design

    Hi, thanks for answering, but the code that appears on that page is related to problem number 1 that I put in the post, which can only be put in bold, italics and underlined. If the cell could be colored, it would be worth it as a solution, but I think it is not possible. As a result I am looking for the solution to alternative number 2, which would be to use a design established in a template, but the problem is that the format of the cells is lost when writing over them.

  • Amit_Sharma Profile Picture
    Amit_Sharma 2,545 on at
    RE: Export an Excel file that contains a certain layout and design

    Hi,

    Refer the below link for information

    https://erpconsultors.com/export-to-excel-report-in-business-central/

    Regards

    Amit Sharma

    www.erpconsultors.com

    linkedin.com/in/amit-sharma-94542440/

    Press Yes if info is useful

  • MiguelRospa Profile Picture
    MiguelRospa 82 on at
    RE: Export an Excel file that contains a certain layout and design

    Thank you very much for your interest! Unfortunately, my problem has not been resolved, since the report you indicate also deletes the design that I have set (it loses the background color, which is what interests me). You can see it in the image:

    pastedimage1677144073593v1.png

    pastedimage1677144123939v2.png

    Actually the report ends up using the same code as the EnterCell function of the Excel Buffer table. I hope someone can help me because the problem does not look good...

  • Nitin Verma Profile Picture
    Nitin Verma 20,995 Moderator on at
    RE: Export an Excel file that contains a certain layout and design

    Hi,

    You can refer the standard report to get help, and check how this report is exporting data into excel.

    report 29 "Export Acc. Sched. to Excel"

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 9th

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

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,246 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,041 Super User 2024 Season 2

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans