How to Create Excel Reports in C# with IronXL
Creating Excel Reports in C# with IronXL
Creating Excel reports in C# is a fundamental requirement in modern business and .NET applications. Whether you're generating financial statements, sales analytics, or inventory dashboards, automating the process to create Excel files and Excel spreadsheets saves hours of manual effort while reducing errors.
IronXL provides a powerful, intuitive solution for developers to create an Excel report in C# without requiring Microsoft Office, MS Excel, or traditional Interop dependencies. Unlike OLE Automation or approaches that rely on the Excel application, IronXL enables generating Excel workbooks and Excel data directly in code with just one line where needed. In this guide, we'll teach you how to create Excel reports with IronXL, complete with easy-to-follow sample code that you can easily implement into your own projects!
What is IronXL and Why Use It for Excel File Generation?
IronXL is a .NET Excel library that enables developers to create, read Excel spreadsheets, and manipulate Excel files directly from C# or Visual Basic source code. Unlike Microsoft Office Interop approaches that rely on the full Excel application or reverse engineering through Open XML SDK, IronXL works across Windows, Linux, macOS, and cloud environments without requiring Excel installation or third-party dependencies. This makes it ideal for server-side Excel reports, automated workflows, and web applications built on .NET Core or the .NET Framework.
For teams transitioning from manual Excel processes, older libraries, or Open XML productivity tool workflows that require navigating various XML namespaces, IronXL provides an intuitive API. It supports both legacy XLS files and modern XLSX files, which are essentially ZIP files containing XML files and various folders. Whether you want to generate a new Excel workbook, manipulate multiple worksheets, or load Excel data from external code, IronXL drastically simplifies the process without needing to understand the underlying formats.
Getting Started with IronXL to Create an Excel File
Setting up IronXL to create Excel reports takes just minutes. Install the library via NuGet Package Manager in Visual Studio:
Install-Package IronXL.Excel
Download IronXL today and start automating your Excel report generation.
Once installed, creating your first Excel report requires only a few lines of code:
using IronXL;
// Create a new Excel workbook for reports
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
// Add a worksheet for the report
WorkSheet reportSheet = workBook.CreateWorkSheet("Monthly Report");
// Add a title
reportSheet["A1"].Value = "Sales Report - January 2024";
reportSheet["A1"].Style.Font.Bold = true;
// Save the Excel report
workBook.SaveAs("MonthlyReport.xlsx");
using IronXL;
// Create a new Excel workbook for reports
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
// Add a worksheet for the report
WorkSheet reportSheet = workBook.CreateWorkSheet("Monthly Report");
// Add a title
reportSheet["A1"].Value = "Sales Report - January 2024";
reportSheet["A1"].Style.Font.Bold = true;
// Save the Excel report
workBook.SaveAs("MonthlyReport.xlsx");
Imports IronXL
' Create a new Excel workbook for reports
Dim workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
' Add a worksheet for the report
Dim reportSheet As WorkSheet = workBook.CreateWorkSheet("Monthly Report")
' Add a title
reportSheet("A1").Value = "Sales Report - January 2024"
reportSheet("A1").Style.Font.Bold = True
' Save the Excel report
workBook.SaveAs("MonthlyReport.xlsx")
This code creates a new Excel report file with a formatted title. The familiar cell reference syntax (reportSheet["A1"]) makes it easy for developers to specify exactly where data should appear, just like working with Excel manually.
Output

Loading Data from Multiple Sources
Real-world Excel reports rarely use static data. IronXL excels at integrating Excel data from various formats, APIs, new DataTable sources, or even several different XML files. This makes it perfect for dynamic C# Excel report generation in server-side or web applications.
Database Integration
For database-driven Excel reports, IronXL seamlessly works with ADO.NET DataTables:
using System.Data;
using System.Data.SqlClient;
// Fetch data from database
string connectionString = "Server=localhost;Database=Sales;Integrated Security=true;";
DataTable salesData = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter(
"SELECT ProductName, Quantity, Revenue FROM MonthlySales", conn);
adapter.Fill(salesData);
}
// Create Excel report from DataTable
WorkBook workBook = WorkBook.Create();
WorkSheet sheet = workBook.CreateWorkSheet("Sales Data");
// Add headers
sheet["A1"].Value = "Product";
sheet["B1"].Value = "Quantity";
sheet["C1"].Value = "Revenue";
// Populate data
int row = 2;
foreach (DataRow dataRow in salesData.Rows)
{
sheet[$"A{row}"].Value = dataRow["ProductName"];
sheet[$"B{row}"].Value = dataRow["Quantity"];
sheet[$"C{row}"].Value = dataRow["Revenue"];
row++;
}
using System.Data;
using System.Data.SqlClient;
// Fetch data from database
string connectionString = "Server=localhost;Database=Sales;Integrated Security=true;";
DataTable salesData = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter(
"SELECT ProductName, Quantity, Revenue FROM MonthlySales", conn);
adapter.Fill(salesData);
}
// Create Excel report from DataTable
WorkBook workBook = WorkBook.Create();
WorkSheet sheet = workBook.CreateWorkSheet("Sales Data");
// Add headers
sheet["A1"].Value = "Product";
sheet["B1"].Value = "Quantity";
sheet["C1"].Value = "Revenue";
// Populate data
int row = 2;
foreach (DataRow dataRow in salesData.Rows)
{
sheet[$"A{row}"].Value = dataRow["ProductName"];
sheet[$"B{row}"].Value = dataRow["Quantity"];
sheet[$"C{row}"].Value = dataRow["Revenue"];
row++;
}
Imports System.Data
Imports System.Data.SqlClient
' Fetch data from database
Dim connectionString As String = "Server=localhost;Database=Sales;Integrated Security=true;"
Dim salesData As New DataTable()
Using conn As New SqlConnection(connectionString)
Dim adapter As New SqlDataAdapter("SELECT ProductName, Quantity, Revenue FROM MonthlySales", conn)
adapter.Fill(salesData)
End Using
' Create Excel report from DataTable
Dim workBook As WorkBook = WorkBook.Create()
Dim sheet As WorkSheet = workBook.CreateWorkSheet("Sales Data")
' Add headers
sheet("A1").Value = "Product"
sheet("B1").Value = "Quantity"
sheet("C1").Value = "Revenue"
' Populate data
Dim row As Integer = 2
For Each dataRow As DataRow In salesData.Rows
sheet($"A{row}").Value = dataRow("ProductName")
sheet($"B{row}").Value = dataRow("Quantity")
sheet($"C{row}").Value = dataRow("Revenue")
row += 1
Next
This approach loads sales data directly from SQL Server into your Excel report. The DataTable integration means you can use existing data access code without modification. For more complex scenarios, check out how to load Excel from SQL databases.
Working with Collections
For in-memory data, Excel data from API responses, or a new DataTable, collections can populate Excel worksheets easily without Microsoft Excel or third-party dependencies:
var salesRecords = new List<SalesRecord>
{
new SalesRecord { Product = "Widget A", Units = 150, Price = 29.99m },
new SalesRecord { Product = "Widget B", Units = 82, Price = 49.99m }
};
// Convert collection to Excel
for (int i = 0; i < salesRecords.Count; i++)
{
sheet[$"A{i+2}"].Value = salesRecords[i].Product;
sheet[$"B{i+2}"].Value = salesRecords[i].Units;
sheet[$"C{i+2}"].Value = salesRecords[i].Price;
}
var salesRecords = new List<SalesRecord>
{
new SalesRecord { Product = "Widget A", Units = 150, Price = 29.99m },
new SalesRecord { Product = "Widget B", Units = 82, Price = 49.99m }
};
// Convert collection to Excel
for (int i = 0; i < salesRecords.Count; i++)
{
sheet[$"A{i+2}"].Value = salesRecords[i].Product;
sheet[$"B{i+2}"].Value = salesRecords[i].Units;
sheet[$"C{i+2}"].Value = salesRecords[i].Price;
}
Imports System.Collections.Generic
Dim salesRecords As New List(Of SalesRecord) From {
New SalesRecord With {.Product = "Widget A", .Units = 150, .Price = 29.99D},
New SalesRecord With {.Product = "Widget B", .Units = 82, .Price = 49.99D}
}
' Convert collection to Excel
For i As Integer = 0 To salesRecords.Count - 1
sheet($"A{i + 2}").Value = salesRecords(i).Product
sheet($"B{i + 2}").Value = salesRecords(i).Units
sheet($"C{i + 2}").Value = salesRecords(i).Price
Next
This method allows creating an Excel report in C# directly in a .NET application, making the process involved for generating multiple worksheets or XLSX files much simpler than manual OLE Automation or reverse engineering XML files.
Output

Formatting Professional Excel Reports
Raw data alone doesn't make a professional report. IronXL provides comprehensive cell formatting options to create polished, business-ready Excel files. The following code shows how easy this is with IronXL:
using IronXL;
using IronXL.Styles;
class Program
{
static void Main(string[] args)
{
// Create a new workbook
var workbook = WorkBook.Create(ExcelFileFormat.XLSX);
// Add a new worksheet
var sheet = workbook.CreateWorkSheet("MySheet");
// Add header values
sheet["A1"].Value = "Product";
sheet["B1"].Value = "Quantity";
sheet["C1"].Value = "Price";
// Add sample data rows
sheet["A2"].Value = "Laptop";
sheet["B2"].Value = 5;
sheet["C2"].Value = 1299.99;
sheet["A3"].Value = "Headphones";
sheet["B3"].Value = 15;
sheet["C3"].Value = 199.50;
sheet["A4"].Value = "Keyboard";
sheet["B4"].Value = 10;
sheet["C4"].Value = 89.99;
sheet["A5"].Value = "Monitor";
sheet["B5"].Value = 7;
sheet["C5"].Value = 249.00;
// Header formatting
var headerRange = sheet["A1:C1"];
headerRange.Style.Font.Bold = true;
headerRange.Style.SetBackgroundColor("#4472C4");
headerRange.Style.Font.Color = "#FFFFFF";
headerRange.Style.BottomBorder.Type = BorderType.Thick;
// Number formatting for currency
sheet["C:C"].FormatString = "$#,##0.00";
// Alternating row colors for readability
for (int row = 2; row <= 10; row++)
{
if (row % 2 == 0)
{
sheet[$"A{row}:C{row}"].Style.SetBackgroundColor("#F2F2F2");
}
}
// Column width adjustment
sheet.Columns[0].Width = 15 * 256; // Width in 1/256th of character width
sheet.Columns[2].Width = 12 * 256;
// Add borders around data
var dataRange = sheet["A1:C10"];
dataRange.Style.TopBorder.Type = BorderType.Thin;
dataRange.Style.RightBorder.Type = BorderType.Thin;
dataRange.Style.BottomBorder.Type = BorderType.Thin;
dataRange.Style.LeftBorder.Type = BorderType.Thin;
// Save the workbook to a file
workbook.SaveAs("MyWorkbook.xlsx");
}
}
using IronXL;
using IronXL.Styles;
class Program
{
static void Main(string[] args)
{
// Create a new workbook
var workbook = WorkBook.Create(ExcelFileFormat.XLSX);
// Add a new worksheet
var sheet = workbook.CreateWorkSheet("MySheet");
// Add header values
sheet["A1"].Value = "Product";
sheet["B1"].Value = "Quantity";
sheet["C1"].Value = "Price";
// Add sample data rows
sheet["A2"].Value = "Laptop";
sheet["B2"].Value = 5;
sheet["C2"].Value = 1299.99;
sheet["A3"].Value = "Headphones";
sheet["B3"].Value = 15;
sheet["C3"].Value = 199.50;
sheet["A4"].Value = "Keyboard";
sheet["B4"].Value = 10;
sheet["C4"].Value = 89.99;
sheet["A5"].Value = "Monitor";
sheet["B5"].Value = 7;
sheet["C5"].Value = 249.00;
// Header formatting
var headerRange = sheet["A1:C1"];
headerRange.Style.Font.Bold = true;
headerRange.Style.SetBackgroundColor("#4472C4");
headerRange.Style.Font.Color = "#FFFFFF";
headerRange.Style.BottomBorder.Type = BorderType.Thick;
// Number formatting for currency
sheet["C:C"].FormatString = "$#,##0.00";
// Alternating row colors for readability
for (int row = 2; row <= 10; row++)
{
if (row % 2 == 0)
{
sheet[$"A{row}:C{row}"].Style.SetBackgroundColor("#F2F2F2");
}
}
// Column width adjustment
sheet.Columns[0].Width = 15 * 256; // Width in 1/256th of character width
sheet.Columns[2].Width = 12 * 256;
// Add borders around data
var dataRange = sheet["A1:C10"];
dataRange.Style.TopBorder.Type = BorderType.Thin;
dataRange.Style.RightBorder.Type = BorderType.Thin;
dataRange.Style.BottomBorder.Type = BorderType.Thin;
dataRange.Style.LeftBorder.Type = BorderType.Thin;
// Save the workbook to a file
workbook.SaveAs("MyWorkbook.xlsx");
}
}
Imports IronXL
Imports IronXL.Styles
Module Program
Sub Main(args As String())
' Create a new workbook
Dim workbook = WorkBook.Create(ExcelFileFormat.XLSX)
' Add a new worksheet
Dim sheet = workbook.CreateWorkSheet("MySheet")
' Add header values
sheet("A1").Value = "Product"
sheet("B1").Value = "Quantity"
sheet("C1").Value = "Price"
' Add sample data rows
sheet("A2").Value = "Laptop"
sheet("B2").Value = 5
sheet("C2").Value = 1299.99
sheet("A3").Value = "Headphones"
sheet("B3").Value = 15
sheet("C3").Value = 199.5
sheet("A4").Value = "Keyboard"
sheet("B4").Value = 10
sheet("C4").Value = 89.99
sheet("A5").Value = "Monitor"
sheet("B5").Value = 7
sheet("C5").Value = 249.0
' Header formatting
Dim headerRange = sheet("A1:C1")
headerRange.Style.Font.Bold = True
headerRange.Style.SetBackgroundColor("#4472C4")
headerRange.Style.Font.Color = "#FFFFFF"
headerRange.Style.BottomBorder.Type = BorderType.Thick
' Number formatting for currency
sheet("C:C").FormatString = "$#,##0.00"
' Alternating row colors for readability
For row As Integer = 2 To 10
If row Mod 2 = 0 Then
sheet($"A{row}:C{row}").Style.SetBackgroundColor("#F2F2F2")
End If
Next
' Column width adjustment
sheet.Columns(0).Width = 15 * 256 ' Width in 1/256th of character width
sheet.Columns(2).Width = 12 * 256
' Add borders around data
Dim dataRange = sheet("A1:C10")
dataRange.Style.TopBorder.Type = BorderType.Thin
dataRange.Style.RightBorder.Type = BorderType.Thin
dataRange.Style.BottomBorder.Type = BorderType.Thin
dataRange.Style.LeftBorder.Type = BorderType.Thin
' Save the workbook to a file
workbook.SaveAs("MyWorkbook.xlsx")
End Sub
End Module
These formatting options transform basic data into professional reports. The style API covers everything from fonts and colors to borders and alignment, giving complete control over the Excel report appearance. For advanced formatting needs, explore conditional formatting to highlight important data automatically.

Using Formulas for Dynamic Excel Report Calculations
Excel's power lies in its formulas, and IronXL fully supports Excel formula creation:
// Add formula for row totals
sheet["D1"].Value = "Total";
sheet["D2"].Formula = "=B2*C2";
// Copy formula down the column
for (int row = 3; row <= 10; row++)
{
sheet[$"D{row}"].Formula = $"=B{row}*C{row}";
}
// Add summary formulas
sheet["A12"].Value = "Summary";
sheet["B12"].Formula = "=SUM(B2:B10)";
sheet["C12"].Formula = "=AVERAGE(C2:C10)";
sheet["D12"].Formula = "=SUM(D2:D10)";
// Add formula for row totals
sheet["D1"].Value = "Total";
sheet["D2"].Formula = "=B2*C2";
// Copy formula down the column
for (int row = 3; row <= 10; row++)
{
sheet[$"D{row}"].Formula = $"=B{row}*C{row}";
}
// Add summary formulas
sheet["A12"].Value = "Summary";
sheet["B12"].Formula = "=SUM(B2:B10)";
sheet["C12"].Formula = "=AVERAGE(C2:C10)";
sheet["D12"].Formula = "=SUM(D2:D10)";
' Add formula for row totals
sheet("D1").Value = "Total"
sheet("D2").Formula = "=B2*C2"
' Copy formula down the column
For row As Integer = 3 To 10
sheet($"D{row}").Formula = $"=B{row}*C{row}"
Next
' Add summary formulas
sheet("A12").Value = "Summary"
sheet("B12").Formula = "=SUM(B2:B10)"
sheet("C12").Formula = "=AVERAGE(C2:C10)"
sheet("D12").Formula = "=SUM(D2:D10)"
The formula support includes all standard Excel functions like SUM, AVERAGE, IF, VLOOKUP, and more. IronXL automatically handles formula dependencies and calculation order, making Excel report generation with complex calculations straightforward. Learn more about math functions in IronXL.

Template-Based Reports
For recurring Excel reports or standardized workbooks, IronXL supports template-based generation, allowing developers to create an Excel file from a template without dealing with Open XML SDK, rels files, or various folders:
// Load existing template
Workbook templateBook = Workbook.Load("ReportTemplate.xlsx");
Worksheet templateSheet = templateBook.DefaultWorksheet;
// Find and replace template markers
foreach (var cell in templateSheet["A1:Z100"])
{
if (cell.Text.Contains("{{CompanyName}}"))
cell.Value = cell.Text.Replace("{{CompanyName}}", "Acme Corp");
if (cell.Text.Contains("{{ReportDate}}"))
cell.Value = cell.Text.Replace("{{ReportDate}}", DateTime.Now.ToString("MMMM yyyy"));
}
// Save as new report
templateBook.SaveAs($"Report_{DateTime.Now:yyyyMMdd}.xlsx");
// Load existing template
Workbook templateBook = Workbook.Load("ReportTemplate.xlsx");
Worksheet templateSheet = templateBook.DefaultWorksheet;
// Find and replace template markers
foreach (var cell in templateSheet["A1:Z100"])
{
if (cell.Text.Contains("{{CompanyName}}"))
cell.Value = cell.Text.Replace("{{CompanyName}}", "Acme Corp");
if (cell.Text.Contains("{{ReportDate}}"))
cell.Value = cell.Text.Replace("{{ReportDate}}", DateTime.Now.ToString("MMMM yyyy"));
}
// Save as new report
templateBook.SaveAs($"Report_{DateTime.Now:yyyyMMdd}.xlsx");
Imports System
' Load existing template
Dim templateBook As Workbook = Workbook.Load("ReportTemplate.xlsx")
Dim templateSheet As Worksheet = templateBook.DefaultWorksheet
' Find and replace template markers
For Each cell In templateSheet("A1:Z100")
If cell.Text.Contains("{{CompanyName}}") Then
cell.Value = cell.Text.Replace("{{CompanyName}}", "Acme Corp")
End If
If cell.Text.Contains("{{ReportDate}}") Then
cell.Value = cell.Text.Replace("{{ReportDate}}", DateTime.Now.ToString("MMMM yyyy"))
End If
Next
' Save as new report
templateBook.SaveAs($"Report_{DateTime.Now:yyyyMMdd}.xlsx")
This approach maintains consistent formatting while updating dynamic content, perfect for monthly reports or standardized documents.

Best Practices and Troubleshooting
When implementing Excel report generation, keep these tips in mind:
- Memory usage with large files: Process data in chunks rather than loading entire datasets (Microsoft's recommendations for large Excel files)
- Date formatting issues: Use
DateTime.ToOADate()for Excel-compatible dates (Excel date system explained) - File locked errors: Always properly dispose Excel objects using
usingstatements or newMemoryStreamapproaches. - Missing styles: Some style properties require setting background color first
Conclusion
IronXL transforms Excel report generation from a tedious manual process into an automated, reliable workflow. With its intuitive API, cross-platform support, and comprehensive feature set, developers can create professional Excel reports in minutes rather than hours. The combination of easy data integration, powerful formatting options, and formula support makes IronXL an essential tool for any C# developer working with Excel reports. For developers who are interested, IronXL offers a free trial and further licensing options for companies and individuals.
Frequently Asked Questions
What is IronXL used for in C#?
IronXL is a powerful library for C# that enables developers to create, edit, and read Excel files with ease. It supports generating professional Excel reports with advanced formatting, formulas, and data integration.
How can I create Excel reports using IronXL?
To create Excel reports using IronXL, you can utilize its robust API to add data, format cells, apply formulas, and integrate with databases, all within a C# environment.
What features does IronXL offer for Excel report generation?
IronXL offers features such as cell formatting, formula application, data validation, chart creation, and seamless database integration for comprehensive Excel report generation.
Does IronXL support database integration?
Yes, IronXL supports database integration, allowing you to pull data directly from databases and incorporate it into your Excel reports, streamlining data analysis and reporting.
Can IronXL handle complex Excel formulas?
IronXL can handle a wide range of Excel formulas, enabling developers to execute complex calculations and automate data processing within Excel reports.
Is it possible to format Excel cells using IronXL?
IronXL provides extensive formatting capabilities, allowing you to customize cell appearances, including font style, color, borders, and more, to create visually appealing Excel reports.
Can I create charts in Excel with IronXL?
Yes, IronXL allows you to create a variety of charts within Excel spreadsheets, helping to visually represent data and enhance the interpretability of your reports.
How does IronXL simplify the Excel report generation process?
IronXL simplifies the Excel report generation process by offering a comprehensive set of features that streamline data input, formatting, calculation, and output, all within the C# programming environment.
Can IronXL handle large datasets efficiently?
IronXL is optimized for performance, enabling it to handle large datasets efficiently, making it ideal for generating complex reports without compromising on speed or reliability.
What programming language is IronXL designed for?
IronXL is specifically designed for use with C#, providing seamless integration and functionality for developers working within the .NET framework.




