How To Convert CSV To XLSX Using C# .NET

In This Article I Will Show You How To Convert CSV File Into XLS File

So Firstly You Need To Install The Package: Microsoft.Office.Interop.Excel From Nugget Package Manager

Just use the function I provided here to convert CSV in XLSX.

using System;
using System.Collections.Generic;
using Excel = Microsoft.Office.Interop.Excel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSV_TO_XLS
{
    class Program
    {
        static void Main(string[] args)
        {
            string csv = @"D:\Files\csvfile.csv";
            string xls = @"D:\Files\xlsfile.xlsx";
            Excel.Application xl = new Excel.Application();

            //Open Excel Workbook for conversion.
            Excel.Workbook wb = xl.Workbooks.Open(csv);
            Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets.get_Item(1);
            //Select The UsedRange
            Excel.Range used = ws.UsedRange;
            //Autofit The Columns
            used.EntireColumn.AutoFit();
            //Save file as csv file
            wb.SaveAs(xls, 51);
            //Close the Workbook.
            wb.Close();
            //Quit Excel Application.
            xl.Quit();
        }
    }
}

Now Run The Project And Its Genrate XLS FILE FROM CSV FILE

 

The Below Screenshot Is Represents The CSV File Will Converted Into Excel(XLS) File By The Above Code.

CSVFILE

 

 

 

 

 

 

 

 

 

The Below Screenshot Represents The CSV File To Converted Excel .

XLSFILE

2 Comments

  1. Marc Charmois

    Congrats ! I could do what I wanted thanks to your post. Bless you 🙂

    0
    0
    Reply
  2. Kent Parker

    Great yet simple solution for this issue! I’d been trying a more complex solution I found that I couldn’t get to work. Yours worked like a charm. Great job!

    0
    0
    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories