Created
December 28, 2025 16:24
-
-
Save mustafabutt-dev/0d135219f444fdbb3cd22971b27e2c3e to your computer and use it in GitHub Desktop.
Convert CSV to Excel in C# with Aspose.cells
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.IO; | |
| using Aspose.Cells; | |
| namespace CsvToExcelDemo | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| // Validate arguments | |
| if (args.Length != 2) | |
| { | |
| Console.WriteLine("Usage: CsvToExcelDemo <inputCsvPath> <outputExcelPath>"); | |
| return; | |
| } | |
| string csvPath = args[0]; | |
| string excelPath = args[1]; | |
| // Ensure the CSV file exists | |
| if (!File.Exists(csvPath)) | |
| { | |
| Console.WriteLine($"Error: CSV file not found at '{csvPath}'."); | |
| return; | |
| } | |
| try | |
| { | |
| // Open CSV stream | |
| using FileStream csvStream = new FileStream(csvPath, FileMode.Open, FileAccess.Read); | |
| // Create workbook and import CSV | |
| Workbook workbook = new Workbook(); | |
| Worksheet sheet = workbook.Worksheets[0]; | |
| sheet.Cells.ImportCsv(csvStream, true); // true = first row has headers | |
| // Optional: auto‑fit columns for better layout | |
| sheet.AutoFitColumns(); | |
| // Save as XLSX | |
| workbook.Save(excelPath, SaveFormat.Xlsx); | |
| Console.WriteLine($"CSV successfully converted to Excel at '{excelPath}'."); | |
| } | |
| catch (Exception ex) | |
| { | |
| Console.WriteLine("Conversion failed: " + ex.Message); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment