Skip to content

Instantly share code, notes, and snippets.

@mustafabutt-dev
Created December 28, 2025 16:41
Show Gist options
  • Select an option

  • Save mustafabutt-dev/5af88735ce3ec3d693fc4c43f8c49688 to your computer and use it in GitHub Desktop.

Select an option

Save mustafabutt-dev/5af88735ce3ec3d693fc4c43f8c49688 to your computer and use it in GitHub Desktop.
Convert CSV to Excel in C# with Aspose.cells
using System;
using Aspose.Cells;
namespace CsvToExcelDemo
{
class Program
{
static void Main(string[] args)
{
// Verify arguments
if (args.Length < 2)
{
Console.WriteLine("Usage: CsvToExcelDemo <input.csv> <output.xlsx>");
return;
}
string csvPath = args[0];
string xlsxPath = args[1];
try
{
// Load CSV into a Workbook
var workbook = new Workbook(csvPath);
// Get the first worksheet
var worksheet = workbook.Worksheets[0];
// Auto‑fit all columns for better appearance
worksheet.AutoFitColumns();
// Apply a simple style to the header row (first row)
var headerStyle = workbook.CreateStyle();
headerStyle.Font.IsBold = true;
headerStyle.ForegroundColor = System.Drawing.Color.LightGray;
headerStyle.Pattern = BackgroundType.Solid;
var headerStyleFlag = new StyleFlag();
headerStyleFlag.All = true;
// Assuming first row is header
var cells = worksheet.Cells;
var maxColumn = cells.MaxColumn;
for (int col = 0; col <= maxColumn; col++)
{
cells[0, col].SetStyle(headerStyle);
}
// Save the workbook as XLSX
workbook.Save(xlsxPath, SaveFormat.Xlsx);
Console.WriteLine($"CSV file '{csvPath}' successfully converted to Excel '{xlsxPath}'.");
}
catch (Exception ex)
{
Console.WriteLine($"Error during conversion: {ex.Message}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment