TPJ.Excel is a small helper library for generating Excel files in three different ways:
ExcelDocumentfor quick exports from a list,DataTable, orDataSetEPPlusHelperwhen you want custom headers and simple formattingAddWorksheet/ExcelWorksheetExtensionwhen you want full control while still writing concise EPPlus code
If you want to see working examples, check TPJ.ExcelTest.
- Create a workbook directly from a
List<T>,DataTable, orDataSet - Column names come from property names or table columns
- Good for quick exports where default formatting is enough
- Set worksheet names
- Choose your own headers
- Format dates and numbers
- Good for most business export scenarios
- Write exactly where you want in the sheet
- Move cell by cell using
CurrentCell(),NextColumn(), andNextRow() - Good for reports, grouped sections, totals, and custom styling
All examples below use this simple model:
public class Staff
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public DateTime StartDate { get; set; }
public decimal Salary { get; set; }
}
var staff = new List<Staff>
{
new() { Id = 1, Name = "Jeff Bob", StartDate = new DateTime(2024, 1, 15), Salary = 32500m },
new() { Id = 2, Name = "Thomas James", StartDate = new DateTime(2022, 6, 1), Salary = 41000m },
new() { Id = 3, Name = "Sophie Hall", StartDate = new DateTime(2025, 2, 10), Salary = 29500m }
};ExcelDocument is the quickest way to generate an Excel file.
using TPJ.Excel;
ExcelDocument.Create(staff, @"C:\Test\Staff.xlsx");You can also pass any IEnumerable<T> and choose the worksheet name:
using TPJ.Excel;
ExcelDocument.Create(staff.Where(x => x.Salary >= 30000m), @"C:\Test\StaffFiltered.xlsx", "Staff");using TPJ.Excel;
byte[] excelBytes = ExcelDocument.Create(staff);- Each public property becomes a column
- Property names are used as the header row
- Simple scalar values such as
string,int,decimal, andDateTimeare exported as a singleValuecolumn - You can also create files from
DataTableandDataSet
Example with a DataTable:
using System.Data;
using TPJ.Excel;
var table = new DataTable("Staff");
table.Columns.Add("Staff #", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Start Date", typeof(DateTime));
table.Rows.Add(1, "Jeff Bob", new DateTime(2024, 1, 15));
table.Rows.Add(2, "Thomas James", new DateTime(2022, 6, 1));
ExcelDocument.Create(table, @"C:\Test\StaffFromTable.xlsx");Use EPPlusHelper when you want a simple API but need more control over how the sheet looks.
For most cases, use the typed builder overloads:
using TPJ.Excel;
EPPlusHelper.Create(
staff,
"Staff",
@"C:\Test\StaffFormatted.xlsx",
columns => columns
.Add("Staff #", x => x.Id)
.Add("Name", x => x.Name)
.Add("Start Date", x => x.StartDate, dateFormat: "dd/mm/yyyy")
.Add("Salary", x => x.Salary, numberFormat: "#,##0.00"));You can still use EPPlusHelper.Column<T>(...) if you prefer building the column list yourself.
If you want to build multiple typed worksheets before creating the workbook, use EPPlusHelper.Worksheet(...):
using TPJ.Excel;
var summarySheet = EPPlusHelper.Worksheet(
staff.Select(x => new { Label = x.Name, Created = DateTime.Today }),
"Summary",
columns => columns
.Add("Label", x => x.Label)
.Add("Created", x => x.Created, dateFormat: "dd/mm/yyyy"));
var staffSheet = EPPlusHelper.Worksheet(
staff,
"Staff",
columns => columns
.Add("Staff #", x => x.Id)
.Add("Name", x => x.Name)
.Add("Start Date", x => x.StartDate, dateFormat: "dd/mm/yyyy")
.Add("Salary", x => x.Salary, numberFormat: "#,##0.00"));
EPPlusHelper.Create(new[] { summarySheet, staffSheet }, @"C:\Test\StaffReport.xlsx");If you want to build the worksheet model yourself, these are the core types:
EPPlusWorksheetName: worksheet nameHeaders: column headersRows: row data
EPPlusRowData: the cells for that row, in column order
EPPlusDataValue: cell valueDateFormat: Excel date format stringNumberFormat: Excel number format string
Formatting options are passed straight to Excel cell formatting. Common examples include dd/mm/yyyy and #,##0.00.
using TPJ.Excel;
using TPJ.Excel.Models;
var worksheet = new EPPlusWorksheet
{
Name = "Staff",
Headers =
[
"Staff #",
"Name",
"Start Date",
"Salary"
],
Rows = staff.Select(x => new EPPlusRow
{
Data =
[
new EPPlusData { Value = x.Id },
new EPPlusData { Value = x.Name },
new EPPlusData
{
Value = x.StartDate,
DateFormat = "dd/mm/yyyy"
},
new EPPlusData
{
Value = x.Salary,
NumberFormat = "#,##0.00"
}
]
})
};
EPPlusHelper.Create(worksheet, @"C:\Test\StaffFormatted.xlsx");using TPJ.Excel;
byte[] excelBytes = EPPlusHelper.Create(worksheet);using TPJ.Excel;
using TPJ.Excel.Models;
var summarySheet = new EPPlusWorksheet
{
Name = "Summary",
Headers = ["Report", "Created"],
Rows =
[
new EPPlusRow
{
Data =
[
new EPPlusData { Value = "Staff Export" },
new EPPlusData { Value = DateTime.Today, DateFormat = "dd/mm/yyyy" }
]
}
]
};
EPPlusHelper.Create(new[] { summarySheet, worksheet }, @"C:\Test\StaffReport.xlsx");If you need a more custom workbook, use EPPlus directly and let TPJ.Excel help with navigation.
AddWorksheet returns an ExcelWorksheetExtension, which tracks the current row and column for you.
It also includes Write(...), WriteNext(...), WriteHeader(...), and WriteRow(...) helpers to cut down repetitive code.
using OfficeOpenXml;
using TPJ.Excel;
using var package = new ExcelPackage();
var ws = package.Workbook.AddWorksheet("Staff");
ws.WriteHeader("Staff #", "Name", "Start Date", "Salary");
foreach (var item in staff)
{
ws.WriteNext(item.Id)
.WriteNext(item.Name)
.WriteNext(item.StartDate, dateFormat: "dd/mm/yyyy")
.Write(item.Salary, numberFormat: "#,##0.00");
ws.NextRow();
}
package.SaveAs(new FileInfo(@"C:\Test\StaffAdvanced.xlsx"));CurrentCell()gets the current cellNextColumn()moves one column to the rightNextRow()moves to the next row and resets the column back to the first columnPreviousColumn()andPreviousRow()let you move backwardsReset()returns to cellA1
- Use
ExcelDocumentfor the fastest export - Use
EPPlusHelperfor clean, readable exports with headers and formatting - Use full EPPlus support when you need a custom layout or advanced workbook logic
For complete working examples, see TPJ.ExcelTest\Program.cs.