In modern data-driven application development, presenting in-memory program data to users in a structured form is a common requirement. Among various approaches, exporting data from a C# List collection to an Excel file is widely used due to its readability, ease of sharing, and strong data analysis capabilities.
Whether generating statistical reports, exporting user data, performing data migration, or providing data sources for BI tools, efficient and well-formatted Excel export functionality is crucial. This article demonstrates how to use Spire.XLS for .NET to write List<T> data into an Excel file in a clear and intuitive way.
Typical Scenarios for Exporting C# Data to Excel
Common requirements in real projects include:
- Exporting order data for financial reconciliation
- Exporting customer information for sales analysis
- Exporting statistical results for management review
Excel, as a universal tool for data presentation and processing, is one of the most common output formats due to its formatting control and ease of use.
Environment Setup
Install Spire.XLS via NuGet in Visual Studio:
- Right-click your project
- Select “Manage NuGet Packages”
- Search for
Spire.XLS - Install
Once installed, you can reference it in your code:
using Spire.Xls;
`
Sample Data Model
csharp
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public DateTime ManufactureDate { get; set; }
public bool IsAvailable { get; set; }
}
Core Implementation: Writing List Data to Excel
`csharp
using Spire.Xls;
using System;
using System.Collections.Generic;
public class ExcelExporter
{
public static void ExportProductsToExcel(List products, string filePath)
{
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
sheet.Name = "Product List";
int row = 1;
// Write headers
sheet.Range[row, 1].Value2 = "ID";
sheet.Range[row, 2].Value2 = "Name";
sheet.Range[row, 3].Value2 = "Price";
sheet.Range[row, 4].Value2 = "ManufactureDate";
sheet.Range[row, 5].Value2 = "IsAvailable";
row++;
// Write data
foreach (var product in products)
{
sheet.Range[row, 1].Value2 = product.ID;
sheet.Range[row, 2].Value2 = product.Name;
sheet.Range[row, 3].Value2 = (double)product.Price;
sheet.Range[row, 4].Value2 = product.ManufactureDate;
sheet.Range[row, 5].Value2 = product.IsAvailable;
row++;
}
// Auto-fit columns
sheet.AllocatedRange.AutoFitColumns();
// Set header style
var headerRange = sheet.Range[1, 1, 1, 5];
headerRange.Style.Font.IsBold = true;
headerRange.Style.HorizontalAlignment = HorizontalAlignType.Center;
workbook.SaveToFile(filePath, ExcelVersion.Version2016);
workbook.Dispose();
}
public static void Main(string[] args)
{
List<Product> productList = new List<Product>
{
new Product { ID = 1, Name = "Laptop", Price = 8999.00M, ManufactureDate = new DateTime(2023, 1, 15), IsAvailable = true },
new Product { ID = 2, Name = "Wireless Mouse", Price = 129.50M, ManufactureDate = new DateTime(2023, 3, 10), IsAvailable = true },
new Product { ID = 3, Name = "Mechanical Keyboard", Price = 599.00M, ManufactureDate = new DateTime(2022, 11, 20), IsAvailable = false },
new Product { ID = 4, Name = "Monitor", Price = 1999.99M, ManufactureDate = new DateTime(2023, 2, 5), IsAvailable = true },
new Product { ID = 5, Name = "SSD Drive", Price = 650.00M, ManufactureDate = new DateTime(2023, 4, 1), IsAvailable = true }
};
string outputPath = "ProductsExport.xlsx";
ExportProductsToExcel(productList, outputPath);
}
}
`
Export Result Preview:
Format Control and Extensions
In real projects, you often need precise control over cell formats. For example:
Set date format:
csharp
sheet.Range[row, 4].Style.NumberFormat = "yyyy-mm-dd";
Set currency format:
csharp
sheet.Range[row, 3].Style.NumberFormat = "¥#,##0.00";
You can control the following precisely via cell objects:
- Font
- Alignment
- Number format
- Background color
- Borders
Practical Recommendations
- Call
Dispose()after processing to release resources - Use
try-catchfor exception handling - Consider batching for large data exports
- Be careful with type conversions (e.g.,
decimaltodouble)
Conclusion
By iterating through List<T> and using sheet.Range[row, column].Value2 to write data, you can implement a clear, controllable, and maintainable Excel export logic.
This approach is intuitive, easy to extend for formatting control, and suitable for most common business scenarios. Combined with the rich features of Spire.XLS, you can further implement chart generation, formula calculations, data validation, and other advanced requirements to meet complex reporting development needs.


Top comments (0)