New project from FarawayTech: Zima Weather — Weather and calendar dashboard for busy professionals.
Formatting Excel Worksheets in C# Using EPPlus
EPPlus offers a wide range of features for formatting Excel worksheets, allowing you to create a professional, polished look for your data. In this guide, we'll cover the basics and explore some of the more advanced options available.
Let's start by creating a new Excel worksheet with EPPlus:
using (var package = new ExcelPackage(new FileInfo("Workbook.xlsx")))
{
var worksheet = package.Workbook.Worksheets.Add("Sheet1");
package.Save();
}
Now, we'll look at how to format cells, including modifying font styles and cell color.
var cell = worksheet.Cells[1, 1];
cell.Style.Font.Bold = true;
cell.Style.Fill.PatternType = ExcelFillStyle.Solid;
cell.Style.Fill.BackgroundColor.SetColor(Color.LightBlue);
cell.Value = "Hello, world!";
package.Save();
EPPlus also supports creating Excel tables, which can make data easier to read and analyze.
var range = worksheet.Cells["A1:C3"];
var table = worksheet.Tables.Add(range, "MyTable");
table.ShowHeader = true;
table.ShowFilter = true;
table.TableStyle = TableStyles.Medium9;
package.Save();
Here is how the final table in Excel would look:
Header 1 | Header 2 | Header 3 |
---|---|---|
Hello, world! | Cell 2 | Cell 3 |
Cell 4 | Cell 5 | Cell 6 |