Convert CSV to Excel in C#, VB.NET

This section will show you a very simple solution to quickly convert CSV to Excel(xls/xlsx) with C#, VB.NET. The whole CSV to Excel task can be realized by only three lines' of key code via a .NET Excel component.

Spire.XLS for .NET, as a professional .NET Excel component built from C#, VB.NET, enables you to mainly convert CSV to Excel by calling two methods: One method is Spire.Xls.Workbook.LoadFromFile(string fileName, string separator, int row, int column) which is used to load CSV file from system. The other is Workbook. SaveToFile(string fileName, ExcelVersion version) to save the CSV file just loaded as Excel. In the solution, Sheet1 is named to be "csv to excel" by setting the Property: Spire.Xls.Worksheet.Name. Besides, Excel background color is added in the target Excel file by Worksheet.Range[].Style.Color.

Before displaying the whole code, please download Spire.XLS for .NET and view the target Excel file as below picture:

CSV to Excel

using Spire.Xls;

namespace csvtoxls
    class Program
        static void Main(string[] args)
         Workbook workbook = new Workbook();
         workbook.LoadFromFile(@"D:\michelle\my file\csv2xls.csv", ",", 1, 1);
         Worksheet sheet = workbook.Worksheets[0];
         sheet.Name = "csv to excel";
         //set the backgroundcolor=LightBlue of Range["A1:E1"]
         sheet.Range["A1:E1"].Style.Color = Color.Chocolate;
         //set the backgroundcolor=LightBlue of Range["A2:E6"]
         sheet.Range["A2:E6"].Style.Color = Color.SpringGreen;
         //set the backgroundcolor=Silver of Range["A7:E12"]
         sheet.Range["A7:E12"].Style.Color = Color.SkyBlue;
         //set the backgroundcolor=Silver of Range["A13:E19"]
         sheet.Range["A13:E19"].Style.Color = Color.Yellow;
         workbook.SaveToFile("result.xls", ExcelVersion.Version97to2003);        
Imports Spire.Xls

Namespace csvtoxls
    Class Program
        Private Shared Sub Main(ByVal args() As String)
            Dim workbook As Workbook = New Workbook
            workbook.LoadFromFile("D:\michelle\my file\csv2xls.csv", ",", 1, 1)
            Dim sheet As Worksheet = workbook.Worksheets(0)
            sheet.Name = "csv to excel"
            'set the backgroundcolor=LightBlue of Range["A1:E1"]
            sheet.Range("A1:E1").Style.Color = Color.Chocolate
            'set the backgroundcolor=LightBlue of Range["A2:E6"]
            sheet.Range("A2:E6").Style.Color = Color.SpringGreen
            'set the backgroundcolor=Silver of Range["A7:E12"]
            sheet.Range("A7:E12").Style.Color = Color.SkyBlue
            'set the backgroundcolor=Silver of Range["A13:E19"]
            sheet.Range("A13:E19").Style.Color = Color.Yellow
            workbook.SaveToFile("result.xls", ExcelVersion.Version97to2003)
        End Sub
    End Class
End Namespace