Recently I was looking for an Advance tool through which I can generate complex Excel Reports. And after going through many tools I found EP Plus. For further details see this link. Through this tool we can easily create reports with charts, graphs and other drawing objects. I have planned to shared few samples with the community, so if any one is interested in using this library he will get a good kick start.
In this sample application I have tried to explain that how you can generate a report using DataTable. I also demonstrated how to use different formatting options and formulas.
So, here is the code:-
Here is the snapshot of Excel File which is created through above code:-
Hopefully this will be useful for you, keep posting your comments.For further details and download see EP Plus home page.
In this sample application I have tried to explain that how you can generate a report using DataTable. I also demonstrated how to use different formatting options and formulas.
So, here is the code:-
/// <summary>
/// Creates the data table.
/// </summary>
/// <returns>DataTable</returns>
private static DataTable CreateDataTable()
{
DataTable dt = new DataTable();
for (int i = 0; i < 10; i++)
{
dt.Columns.Add(i.ToString());
}
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
foreach (DataColumn dc in dt.Columns)
{
dr[dc.ToString()] = i;
}
dt.Rows.Add(dr);
}
return dt;
}
private void button1_Click(object sender, EventArgs e)
{
using (ExcelPackage p = new ExcelPackage())
{
//Here setting some document properties
p.Workbook.Properties.Author = "Zeeshan Umar";
p.Workbook.Properties.Title = "Office Open XML Sample";
//Create a sheet
p.Workbook.Worksheets.Add("Sample WorkSheet");
ExcelWorksheet ws = p.Workbook.Worksheets[1];
ws.Name = "Sample Worksheet"; //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
DataTable dt = CreateDataTable(); //My Function which generates DataTable
//Merging cells and create a center heading for out table
ws.Cells[1, 1].Value = "Sample DataTable Export";
ws.Cells[1, 1, 1, dt.Columns.Count].Merge = true;
ws.Cells[1, 1, 1, dt.Columns.Count].Style.Font.Bold = true;
ws.Cells[1, 1, 1, dt.Columns.Count].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
int colIndex = 1;
int rowIndex = 2;
foreach (DataColumn dc in dt.Columns) //Creating Headings
{
var cell = ws.Cells[rowIndex, colIndex];
//Setting the background color of header cells to Gray
var fill = cell.Style.Fill;
fill.PatternType = ExcelFillStyle.Solid;
fill.BackgroundColor.SetColor(Color.Gray);
//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;
//Setting Value in cell
cell.Value = "Heading " + dc.ColumnName;
colIndex++;
}
foreach (DataRow dr in dt.Rows) // Adding Data into rows
{
colIndex = 1;
rowIndex++;
foreach (DataColumn dc in dt.Columns)
{
var cell = ws.Cells[rowIndex, colIndex];
//Setting Value in cell
cell.Value = Convert.ToInt32(dr[dc.ColumnName]);
//Setting borders of cell
var border = cell.Style.Border;
border.Left.Style =
border.Right.Style = ExcelBorderStyle.Thin;
colIndex++;
}
}
colIndex = 0;
foreach (DataColumn dc in dt.Columns) //Creating Headings
{
colIndex++;
var cell = ws.Cells[rowIndex, colIndex];
//Setting Sum Formula
cell.Formula = "Sum("+
ws.Cells[3, colIndex].Address+
":"+
ws.Cells[rowIndex-1, colIndex].Address+
")";
//Setting Background fill color to Gray
cell.Style.Fill.PatternType = ExcelFillStyle.Solid;
cell.Style.Fill.BackgroundColor.SetColor(Color.Gray);
}
//Generate A File with Random name
Byte[] bin = p.GetAsByteArray();
string file = "d:\\" + Guid.NewGuid().ToString() + ".xlsx";
File.WriteAllBytes(file, bin);
}
}
Here is the snapshot of Excel File which is created through above code:-
Hopefully this will be useful for you, keep posting your comments.For further details and download see EP Plus home page.
Eric · 711 weeks ago
Eric · 711 weeks ago
DanBar · 686 weeks ago
Björn · 676 weeks ago
Error1'OfficeOpenXml.ExcelWorksheet' does not contain a definition for 'Cells' and no extension method 'Cells' accepting a first argument of type 'OfficeOpenXml.ExcelWorksheet' could be found (are you missing a using directive or an assembly reference?)C:ProjectsConsoleExcel2007XLClasses.cs1616ConsoleExcel2007
Nirmal · 660 weeks ago
Thanks in advance..
vazhu · 650 weeks ago
Rajesh · 623 weeks ago
i want to write 90000X50 rows to a excel before i download. writing cell by cell will be very slow. is there anyway we can write a 2d array to excel directly? that will help because, i will form the 2d array seperatly and write to excel in one shot instead of writing cell by cell. this will enhance the performance.
kindly suggest.
Thanks.
Rajesh · 623 weeks ago
kavitha · 622 weeks ago
I used epplus to export my datatable as excel in C#.net.The problem here is i want to do Indent for some paticular cell.So,I have used
var indent = cell.Style.Indent;
indent = 10;
(have given values from 10 to 100 and more..).But,it is not working.Pls help
Syed Hashmi · 622 weeks ago
Dim FI As New System.IO.FileInfo(OpenFileDialog1.FileName)
Dim XP As New ExcelPackage(FI)
Dim WS As ExcelWorksheet = XP.Workbook.Worksheets("Sheet1")
' SET VALUES
WS.Cell(6, 6).Value = "6"
WS.Cell(6, 7).Value = "7"
WS.Cell(6, 8).Formula = "SUM(F6:G7)"
But I don’t get the total of the above cells with following statement:
txtResult.Text = WS.Cell(6, 8).Value
I was expecting a total of SUM as per formuls.
Can you help.
Nocturns2 · 593 weeks ago
Anvesh · 579 weeks ago
Hameed · 565 weeks ago
Nissrin · 562 weeks ago
alex · 562 weeks ago
Ciara · 532 weeks ago
Kevin · 531 weeks ago
Raja · 462 weeks ago
how i will export two different table format in same excel sheet.
Ravi · 448 weeks ago
Jack helmen · 388 weeks ago