How to Add two rows with merged cells in top of the Excel using DocumentFormat.OpenXml

Elaya Raja 96 Reputation points
2022-06-14T10:50:11.683+00:00

Hi All
I have implemented the code to create Excel from data table using DocumentFormat.OpenXml and it was working fine. But I want to add two rows in the excel top with merged cells (Ref Image) which contains the Title of the sheet with some additional information. After this two lines my table should show. Any leads to help me to solve this?

211256-image.png

In this image. First two rows I have to create and my data table should start with 3 rd row.

Thanks in advance
Jo

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,245 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
306 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,046 Reputation points Microsoft Vendor
    2022-06-15T02:19:41.203+00:00

    Hi @Elaya Raja ,

    But I want to add two rows in the excel top with merged cells (Ref Image) which contains the Title of the sheet with some additional information. After this two lines my table should show. Any leads to help me to solve this?

    You can use the MergeCells and MergeCell classes to create the merged cells you require, then insert the row at the first and second row. To populate data in the merged cells you need to add the value to the upper left most cell; any other values will be ignored.

    Refer the following sample code: 211531-sourcecode.txt

    211455-screenshot-2022-06-15-101402.png

    The generated excel file looks like this:

    211476-image.png

    And, if append the table data, the result like this:

    211420-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion


0 additional answers

Sort by: Most helpful