Skip to footer content
USING IRONXL

C# CSV to XLSX: A Complete Developer Guide

Converting CSV files to XLSX format unlocks powerful spreadsheet capabilities that comma-separated values files simply cannot provide. While CSV files excel at storing raw data, the Excel XLSX format supports formulas, multiple worksheets, charts, and rich formatting that modern applications demand. Whether you are coming from another background like Java or are a seasoned .NET developer, understanding this transition is essential.

IronXL is a .NET library that makes this conversion remarkably simple. With just a few lines of code, developers can convert CSV to XLSX (or even the older XLS format) without installing Microsoft Office or relying on the Open XML SDK directly. You can find extensive documentation here to explore advanced features. Start a free trial to follow along with the code samples in this guide.

How Do You Convert CSV Files to XLSX Format in C#?

Converting a CSV file to an Excel XLSX file requires loading the source data and saving it in the new format. IronXL handles this conversion with the WorkBook.LoadCSV method, which parses comma separated values and creates a workbook ready for export.

using IronXL;
// Standard entry point using args for file paths
static void Main(string[] args)
{
    // Load CSV file and convert to XLSX format
    WorkBook workbook = WorkBook.LoadCSV("data.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ",");
    // Access the default worksheet containing CSV data
    WorkSheet worksheet = workbook.DefaultWorkSheet;
    // Save as Excel XLSX file
    workbook.SaveAs("output.xlsx");
}
using IronXL;
// Standard entry point using args for file paths
static void Main(string[] args)
{
    // Load CSV file and convert to XLSX format
    WorkBook workbook = WorkBook.LoadCSV("data.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ",");
    // Access the default worksheet containing CSV data
    WorkSheet worksheet = workbook.DefaultWorkSheet;
    // Save as Excel XLSX file
    workbook.SaveAs("output.xlsx");
}
$vbLabelText   $csharpLabel

Output

C# CSV to XLSX: A Complete Developer Guide: Image 1 - Example output for CSV to Excel conversion

The LoadCSV method accepts three key parameters: the filename containing your CSV data, the target Excel format, and the delimiter character used in your source file. This approach preserves all string data and numeric values from the original CSV sheet while creating a properly structured XLSX file.

The WorkBook class serves as the central hub for all spreadsheet operations. Once loaded, the CSV data becomes accessible through worksheets, enabling further manipulation before saving the final Excel file.

What Are the Benefits of Converting CSV to Excel Format?

The XLSX format offers significant advantages over plain CSV format for data management:

  • Multiple Worksheets: Excel files support multiple sheets within a single workbook, enabling organized data storage that CSV files cannot match
  • Formula Support: Write complex calculations, aggregations, and conditional logic directly into cells.
  • Visual Charts: Create bar charts, line charts, pie charts, and other visualizations from your data
  • Cell Formatting: Control fonts, colors, borders, and number formats for professional documents
  • Data Validation: Restrict cell input to specific values or ranges

These capabilities make the Excel format ideal for reports, dashboards, and any application requiring more than raw data storage.

How Do You Handle CSV Encoding During Conversion?

In modern apps, you often need to download a CSV from a remote server. You can use an HttpClient client to fetch data over https, then load that stream directly into IronXL. Handling encoding correctly ensures special characters and international text remain intact for the end user.

using IronXL;
using System.Text;
// Load CSV with specific encoding for international characters
var workbook = WorkBook.LoadCSV("international-data.csv",
    fileFormat: ExcelFileFormat.XLSX,
    ListDelimiter: ",");
// The workbook now contains properly encoded data
WorkSheet sheet = workbook.DefaultWorkSheet;
// Save the converted Excel file
workbook.SaveAs("encoded-output.xlsx");
using IronXL;
using System.Text;
// Load CSV with specific encoding for international characters
var workbook = WorkBook.LoadCSV("international-data.csv",
    fileFormat: ExcelFileFormat.XLSX,
    ListDelimiter: ",");
// The workbook now contains properly encoded data
WorkSheet sheet = workbook.DefaultWorkSheet;
// Save the converted Excel file
workbook.SaveAs("encoded-output.xlsx");
$vbLabelText   $csharpLabel

Output XLSX File

C# CSV to XLSX: A Complete Developer Guide: Image 2 - CSV to XLSX with all the encoding handled

IronXL automatically detects common encoding formats including UTF-8, making most conversions seamless. For files with non-standard encoding, the library provides options to specify the exact format during the load operation.

How Can You Add Charts After Converting CSV Data?

Once CSV data exists in an Excel workbook, IronXL enables creating charts directly from that data. Charts transform raw numbers into visual insights without requiring Microsoft Excel installation.

using IronXL;
using IronXL.Drawing.Charts;
// Load CSV and convert to Excel format
WorkBook workbook = WorkBook.LoadCSV("sales-data.csv",
    fileFormat: ExcelFileFormat.XLSX,
    ListDelimiter: ",");
WorkSheet worksheet = workbook.DefaultWorkSheet;
// Create a column chart from the converted CSV data
IChart chart = worksheet.CreateChart(ChartType.Column, 10, 0, 25, 10);
// Add data series from the worksheet
IChartSeries series = chart.AddSeries("A2:A10", "B2:B10");
series.Title = "Monthly Sales";
// Configure chart appearance
chart.SetTitle("Sales Performance");
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart and save
chart.Plot();
workbook.SaveAs("sales-with-chart.xlsx");
using IronXL;
using IronXL.Drawing.Charts;
// Load CSV and convert to Excel format
WorkBook workbook = WorkBook.LoadCSV("sales-data.csv",
    fileFormat: ExcelFileFormat.XLSX,
    ListDelimiter: ",");
WorkSheet worksheet = workbook.DefaultWorkSheet;
// Create a column chart from the converted CSV data
IChart chart = worksheet.CreateChart(ChartType.Column, 10, 0, 25, 10);
// Add data series from the worksheet
IChartSeries series = chart.AddSeries("A2:A10", "B2:B10");
series.Title = "Monthly Sales";
// Configure chart appearance
chart.SetTitle("Sales Performance");
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart and save
chart.Plot();
workbook.SaveAs("sales-with-chart.xlsx");
$vbLabelText   $csharpLabel

Output

C# CSV to XLSX: A Complete Developer Guide: Image 3 - Output for converting CSV file into an Excel file with a chart

The CreateChart method accepts the chart type and positioning coordinates. The AddSeries method links worksheet cell ranges to the chart axes, creating dynamic visualizations that update when the underlying data changes.

How Do You Convert CSV to DataTable and Then to Excel?

For scenarios requiring data manipulation before export, converting CSV data through a DataTable provides flexibility. This approach lets developers filter, transform, or validate data during the conversion process.

using IronXL;
using System.Data;
// Load CSV file into workbook
WorkBook workbook = WorkBook.LoadCSV("input.csv",
    fileFormat: ExcelFileFormat.XLSX,
    ListDelimiter: ",");
// Convert worksheet to DataTable for manipulation
DataTable table = workbook.DefaultWorkSheet.ToDataTable(true);
// Create new workbook from modified data
WorkBook outputWorkbook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet outputSheet = outputWorkbook.CreateWorkSheet("Processed Data");
// Import DataTable back to Excel
outputSheet.LoadFromDataTable(table, 0);
// Save the final XLSX file
outputWorkbook.SaveAs("processed-output.xlsx");
using IronXL;
using System.Data;
// Load CSV file into workbook
WorkBook workbook = WorkBook.LoadCSV("input.csv",
    fileFormat: ExcelFileFormat.XLSX,
    ListDelimiter: ",");
// Convert worksheet to DataTable for manipulation
DataTable table = workbook.DefaultWorkSheet.ToDataTable(true);
// Create new workbook from modified data
WorkBook outputWorkbook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet outputSheet = outputWorkbook.CreateWorkSheet("Processed Data");
// Import DataTable back to Excel
outputSheet.LoadFromDataTable(table, 0);
// Save the final XLSX file
outputWorkbook.SaveAs("processed-output.xlsx");
$vbLabelText   $csharpLabel

Output

C# CSV to XLSX: A Complete Developer Guide: Image 4 - CSV to DataTable to XLSX output

This sample code demonstrates the bidirectional conversion capability. The ToDataTable method exports worksheet data to a .NET DataTable, while LoadFromDataTable imports that data into a new Excel worksheet.

Conclusion

Converting C# CSV to XLSX with IronXL streamlines what would otherwise require complex code. Whether you need to save CSV data into a more robust format, prompt a user for a file path, or integrate with a web API, IronXL handles the heavy lifting.

Key capabilities covered in this guide include basic CSV to Excel conversion, encoding handling for international documents, chart creation from imported data, and DataTable integration for advanced processing scenarios.

IronXL supports Windows, Linux, macOS, Docker, and Azure deployments, making it suitable for any .NET environment. The library works with .NET Framework, .NET Core, and .NET 5+ applications.

Ready to convert CSV files in your own projects? Purchase an IronXL license for production deployment, or download a free trial to test these features in your development environment.

Get stated with IronXL now.
green arrow pointer

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he ...
Read More