Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

How to merge cells in Excel through x++ code?

(0) ShareShare
ReportReport
Posted on by 45

Hi,

We have a requirement to generate an Excel report in d365, in report we have to merge multiple cells into one, Quick responses are appreciated.

Thanks in Advance.

  • huijij Profile Picture
    huijij 19,811 on at
    RE: How to merge cells in Excel through x++ code?

    You can try it to see if it can help:

    Worksheet.get_Cells().get_Item(1, 3);

  • RE: How to merge cells in Excel through x++ code?

    Hi Judy,

    Thanks for the Response,

    I have tried with below code but getting errors as i.e., (the name SysExcelRange does not denote a class or a table or an ExtendedDataType),

    (Method 'cells()' is not found on type 'OfficeOpenXml.ExcelWorksheet').

    #define.xlCenter(-4108)
            DocuFileSaveResult saveResult =
            DocuFileSave::promptForSaveLocation("@ApplicationPlatform:OfficeDefaultWorkbookFileName","xlsx", null, "excel create and export");
            if (saveResult&& saveResult.parmAction() != DocuFileSaveAction::Cancel)
            {
                saveResult.parmOpenParameters('web=1');
                saveResult.parmOpenInNewWindow(false);
                System.IO.Stream
                    workbookStream = new System.IO.MemoryStream();
                System.IO.MemoryStream
                    memoryStream = new System.IO.MemoryStream();
    
                using(var package = new OfficeOpenXml.ExcelPackage(memoryStream))
                {
                    var worksheets = package.get_Workbook().get_Worksheets();
                    var worksheet = worksheets.Add("Sheet1");
                    var cells = worksheet.get_Cells();
                    SysExcelRange  sysExcelRange;
                    sysExcelRange   = workSheet.cells().range('E18:F18');
                    sysExcelRange.comObject().MergeCells(1);
                    SysExcelRange.horizontalAlignment(#xlCenter);
                    package.Save();
                }

  • Suggested answer
    Deepak Agarwal Profile Picture
    Deepak Agarwal 8,564 on at
    RE: How to merge cells in Excel through x++ code?

    You can concanate your values in a variable (within x++ code) and assign than to an excel cell.

  • Suggested answer
    huijij Profile Picture
    huijij 19,811 on at
    RE: How to merge cells in Excel through x++ code?

    Hi Sowmya170106,

    Please try the code segment to merge cells through X :

    SysExcelRange  sysExcelRange;
    sysExcelRange   = workSheet.cells().range('E18:F18');
    sysExcelRange.comObject().MergeCells(1);
    SysExcelRange.horizontalAlignment(#xlCenter);

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

Announcing Our 2025 Season 1 Super Users!

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

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,979 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,848 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans