Creating Reports in Excel 2007 using EPPlus (Header, Footer, Comments, Image, Formatting, Shape and Formula)

Introduction

EPPlus is really a powerful tool to generate excel based reports on server side and it is becoming my favorite tool as I am getting more experienced with it. Previously I wrote a post about Creating advanced Excel 2007 Reports on Server. This post is update on the last post and I am sharing more advanced feature of EP Plus.

Code which I used in this post can be downloaded from this location.

Create Excel Worksheet

First of all we have to create a worksheet, here is the function which creates a worksheet:-
private static ExcelWorksheet CreateSheet(ExcelPackage p, string sheetName)
 {
    p.Workbook.Worksheets.Add(sheetName);
    ExcelWorksheet ws = p.Workbook.Worksheets[1];
    ws.Name = sheetName; //Setting Sheet's name
    ws.Cells.Style.Font.Size = 11; //Default font size for whole sheet
    ws.Cells.Style.Font.Name = "Calibri"; //Default Font name for whole sheet

    return ws;
 }

Above code creates a worksheet and assigned name to the sheet. Also set the default font for all cells as Calibri and 11 size.

Merge Excel Columns

Now here is how you can merge several columns into one:-
//Merging cells and create a center heading for out table
ws.Cells[1, 1].Value = "Sample DataTable Export"; // 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

Excel Cell Background Color

Here is how you can modify the background color of a cell:-
//Setting the background color of header cells to Gray
var fill = cell.Style.Fill;
fill.PatternType = ExcelFillStyle.Solid;
fill.BackgroundColor.SetColor(Color.Gray);

Excel Cell Border

You can set borders to cell like this:-
//Setting Top/left,right/bottom borders.
    var border = cell.Style.Border;
    border.Bottom.Style = border.Top.Style = border.Left.Style = border.Right.Style = ExcelBorderStyle.Thin;

Excel Formula

Formulas is really powerful feature of Excel. You can apply any formula through the help of EP Plus. Here is how you can add a sum formula:-

//Setting Sum Formula
cell.Formula = "Sum(" + ws.Cells[3, colIndex].Address + ":" + ws.Cells[rowIndex - 1, colIndex].Address + ")";
Note that
ws.Cells[3, colIndex].Address
give the cell Address like A1 or C10 and same address will be used when you edit a formula in Excel.

Add Comments to Excel Cell

Here is the wrapper function through which you can add comments to a cell:-
private static void AddComment(ExcelWorksheet ws, int colIndex, int rowIndex, string comment, string author)
{
    //Adding a comment to a Cell
    var commentCell = ws.Cells[rowIndex, colIndex];
    commentCell.AddComment(comment, author);
}

I tried to enter blank 'Author Name' in comments but it failed to work. So always put a 'Author Name' in comments as well.

Add Image in Excel Sheet

With the help of this function, you can add an Image to your excel sheet:-
private static void AddImage(ExcelWorksheet ws, int columnIndex, int rowIndex, string filePath)
{
    //How to Add a Image using EP Plus
    Bitmap image = new Bitmap(filePath);
    ExcelPicture picture = null;
    if (image != null)
    {
        picture = ws.Drawings.AddPicture("pic" + rowIndex.ToString() + columnIndex.ToString(), image);
        picture.From.Column = columnIndex;
        picture.From.Row = rowIndex;
        picture.From.ColumnOff = Pixel2MTU(2); //Two pixel space for better alignment
        picture.From.RowOff = Pixel2MTU(2);//Two pixel space for better alignment
        picture.SetSize(100, 100);
    }
}

Add Custom objects to Excel Sheet


Here is the function through which you can add a custom shape to your excel sheet:-
private static void AddCustomShape(ExcelWorksheet ws, int colIndex, int rowIndex, eShapeStyle shapeStyle, string text)
{
    ExcelShape shape = ws.Drawings.AddShape("cs" + rowIndex.ToString() + colIndex.ToString(), shapeStyle);
    shape.From.Column = colIndex;
    shape.From.Row = rowIndex;
    shape.From.ColumnOff = Pixel2MTU(5);
    shape.SetSize(100, 100);
    shape.RichText.Add(text);
}

Conclusion

EPPlus is really a nice library to generate creative Excel reports for demanding industry. I have tried to share what I have learned so far. See the image below showing the output of my code.Feel free to comment. Happy coding !!!

Comments (190)

Loading... Logging you in...
  • Logged in as
Wow great tutorial
2 replies · active 354 weeks ago
It really helped me, thanks for sharing
1 reply · active 715 weeks ago
zaheer ahmed's avatar

zaheer ahmed · 715 weeks ago

really helping tutorial
1 reply · active 715 weeks ago
Wow, it really helped me, also sample is great to start development with EPPlus.
2 replies · active 576 weeks ago
Thanks
1 reply · active 711 weeks ago
Tanzila Rahman's avatar

Tanzila Rahman · 713 weeks ago

Its really helpful. Thanks for the tutorial.
1 reply · active 711 weeks ago
Thanks a lot :D
1 reply · active 703 weeks ago
one of my datacolumn contains decimal values with 10 decimal points. what should i do if i only want the outcome of the excel to reflect say two decimal instead of 10 decimal points.

using excel's formatting is no point because i really want to ensure only values with two decimal points are copied into excel cell as i am charging my clients for the extra decimal points if they subscribe to my service.
2 replies · active 704 weeks ago
I need to show screen tip on a hyperlink when hover, seems like epplus is not supporting it, do you know of any workaround?
1 reply · active 663 weeks ago
Is it possible to pass RTF string to the value property and be rendered as formatted string?
2 replies · active 704 weeks ago
Can you elaborate your problem little bit more, also I recommend you to post your query at www.forums.asp.net and you will have a better chance for resolution of the problem
I'm writing code to export a dataset to an excel file, it is doing this perfectly, the problem is in the format of the file, it saves it as Tab delimited format. In my case I need it be saved using normal excel format, because I have another functionality in my application to import the data from the excel file again, but the code I'm using for importing data is expecting the file to be in normal excel format. So, is there any way to force the export function to save the file in normal excel format

I want to use the EPPlus
Pls Help me. thank you
1 reply · active 699 weeks ago
how fill shape with gradient
2 replies · active 696 weeks ago
hey Zeeshan,nice documentation. I am trying to export a data table to excel where this data table contains date as one of the fields. But epplus converts this date into a string and then displays it . I wantred to know if there is a way where epplus detects it as a date and displays it in mm/dd/yyyy format?
1 reply · active 696 weeks ago
Thank for you create...
1 reply · active 696 weeks ago
Hey Zeeshan greetings. Will u please give an example of pivot table in asp.net.
can be used with visual studio 2010?
2 replies · active 686 weeks ago
How we can create excel charts with this tool? Can anyone give me a reference please?
great dude:
1 reply · active 678 weeks ago
After writing to Excel,is it possible to show "download" pop up,so that user will come to know that where the file is saved.
1 reply · active 616 weeks ago
Good tutorial, nice work!
1 reply · active 678 weeks ago
The tutorial helped me a lot to write my code. But now, when I copy the code to wwwroot on the same machine nothing works, simply without any exception. So, is it necessary to configure IIS 7 in a particular way? Special access permission to the destination directory or something else more exotic?
Thanks.
Marco
1 reply · active 678 weeks ago
Hi,
Good article and utility. Does this utility can read excel also?
1 reply · active 678 weeks ago

Post a new comment

Comments by