.NET

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.

 

 

 

 

 

 

 

 

 

The Below Screenshot Represents The CSV File To Converted Excel .

Aatisha Dalwadi

I am Aatisha Dalwadi.I Started My Journey As Asp .Net Developer At Vision Infotech.I Completed My MCA From Sarvjanik College Of Enginnering AND Technology.Good Experience In Designing And Development Of Web Applications using C# .Net ,ASP.NET , Microsoft .Net Framework.

View Comments

  • 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!

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

2 years ago