Add extra header row at top of excel sheet [EPPlus]

11,631

What you want it's merged cells. You can do it like this:

 ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");
 ws.Cells["A1:G1"].Merge = true;

And keep using EPPlus. It's very good

Other sample with formatting:

using (ExcelRange Title = Cells[1, 1, 1, dt.Columns.Count]) {
    Title.Merge = true;
    Title.Style.Font.Size = 18;
    Title.Style.Font.Bold = true;
    Title.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    Title.Style.Fill.BackgroundColor.SetColor(systemColor);
    Title.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
    Title.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
    Title.Style.TextRotation = 90;
    Title.Value = "This is my title";
}
Share:
11,631
user2645738
Author by

user2645738

Updated on June 25, 2022

Comments

  • user2645738
    user2645738 almost 2 years

    I am working with a web application. There,i am supposed to export data to excel. For that,i have made use of EPPlus.

    I searched alot but cant find out a way to add extra row at top of excel sheet. Please have a look at below image to better understand the idea.

    enter image description here

    I tried merging the header,but then i wont get other headers,so i think 'add extra row at top' wil be a better phrase for this.

    I am not bounded to use EPPlus. If there is other ways available,i will surely approach it.

    Can any one help me with this? I really appreciate the response.

  • user2645738
    user2645738 almost 11 years
    I have already done like this ws.Cells["A1"].LoadFromDataTable(dt, true); ws.Cells[1, 1].Value = "report as on date"+ DateTime.Now.ToString("dd-MM-yyyy"); // Heading Name ws.Cells[1, 1, 1, dt.Columns.Count].Merge = true; //Merge columns start and end range ws.Cells[1, 1, 1, dt.Columns.Count].Style.Font.Bold = true; //Font should be bold ws.Cells[1, 1, 1, dt.Columns.Count].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; // Aligmnet is center Hence getting error that Cells are already merged.
  • user2645738
    user2645738 almost 11 years
    Why theres no response?
  • Eduardo Molteni
    Eduardo Molteni almost 11 years
    Because you need to keep going from the answer, searching samples and in the documentation. I've added another sample with formatting that I found using google
  • curiousBoy
    curiousBoy over 4 years
    not the same thing as you can apply specific settings for the header/footer. What you are suggesting is just adding a merged row, not header.