Monday, 08 October 2012 05:33

CSV to Excel- Convert CSV to Excel with C#, VB.NET

Written by  support iceblue
Rate this item
(0 votes)

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


Sample Code for Converting CSV to Excel with C#, VB.NET


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];
         workbook.SaveToFile("result.xls", ExcelVersion.Version97to2003);
         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;        
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)
            workbook.SaveToFile("result.xls", ExcelVersion.Version97to2003)
            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
        End Sub
    End Class
End Namespace


More about this .NET Excel component

Additional Info

  • tutorial_title: Convert CSV to Excel
Last modified on Tuesday, 07 January 2014 09:39