Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 a lot but cant find out a way to add extra row at top of excel sheet.Like,i want row at top of header rows for the purpose of displaying company name(It will look like 3 or 4 rows are merged)

I have tried merging headers,but then it will display only single name(not all the headers)

The code is as below.
C#
ws.Cells["A1"].LoadFromDataTable(dt, true);
ws.Cells[1, 1].Value = "report"// 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

Through this,i am able to merge headers,but my target is to display row at top of header.

My target is to accomplish this. I am not bounded to use EPPlus. If it can be done by another way than it is also welcome.

I appreciate the answers. Thanks.
Posted
Updated 4-Aug-13 20:07pm
v2
Comments
Maciej Los 5-Aug-13 2:30am    
What is EPPlus?
What LoadFromDataTable function do?
Coder_one 5-Aug-13 5:54am    
This code is inside the function which loads excel sheet from data table,that is i am trying to export data table to excel sheet.

EPPlus is a .NET library used to read/write excel sheets.

ws.Cells[1, 1].Value should reference your top right cell--is "report" appearing there successfully? If not, try skipping the merge. The merge doesn't combine the data, but rather makes multiple cells appear as one, which means you will appear to lose data that was populated in those merged cells--put your value in after the merge. Finally, are you aware that you're manually over-writing your first cell (probably first row) of data? Instead, leave a blank row for your manual header:
C#
ws.Cells["A2"].LoadFromDataTable(dt, true);
ws.Cells[1, 1, 1, dt.Columns.Count].Merge = true; //Merge columns start and end range
ws.Cells[1, 1].Value = "report"// Heading Name
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
 
Share this answer
 
Create a HTML table with all the styling you want, then give the file a file extension of ".xls".
Microsoft Excel (regardless of any version) will open the file as an usual Excel Document.

Note: Use simple styling.
background-color, font, border etc.
 
Share this answer
 
v2
Comments
Coder_one 6-Aug-13 8:44am    
Looks good. I havent tried it yet. I will shortly let you know. Thanks.
If you want to create real XLSX (Excel 2007/2010/2013) document, you can use ClosedXML to create it. It is an open source project.

Official Site: http://closedxml.codeplex.com/[^]

Sample Code:
C#
using ClosedXML.Excel;

C#
public static void PublishExcel()
{
    var workbook = new XLWorkbook();
    var worksheet = workbook.Worksheets.Add("Sample Sheet");
    worksheet.Cell("A1").Value = "Hello World!";
    worksheet.Cell(2, 1).SetValue("This is a sample Excel Document");
    workbook.SaveAs("HelloWorld.xlsx");
}

Publish through ASP.NET
Read Documentation: CloseXML - How do I deliver an Excel file in ASP.NET?[^]

For Cell Merging:
Read Documentation: CloseXML - Merging Cells[^]

You can do lots of Formatting, etc. Read the Documentation of the project.
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900