Abby
Abby

Reputation: 149

I Want to convert a .xls or.xlsx file to .csv format using C#

I trying to write a code in C# to convert a .xls/.xlsx file to a .csv file ,I am saving the file in .Csv after opening it but the resultant file is still has all the excel properties(like merged cell , headers etc),but When I look at the properties it is showing File type as .csv.

In my code I'm opening an execl and the saving it using SaveAs.

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


namespace ConsoleApp2
{
    class Program

    {
        static void conversion(string sourcepath)
        {
            Excel.Application xlapp;
            xlapp =new Excel.Application();
            xlapp.Visible = true;
            // Excel.Workbook book = xlapp.Workbooks.Open(sourcepath);
            
            Excel.Workbook xlworkbook = xlapp.Workbooks.Open(sourcepath);
            xlworkbook= xlapp.ActiveWorkbook;
            Excel.Worksheet xlsheet = xlapp.ActiveSheet;
            xlsheet.SaveAs(@"C:\Users\<username>\Desktop\sample\new.csv");



       
        }

        static void Main(string[] args)
        {
            
            string path= Console.ReadLine();
            conversion(path);
            
        }
    }}

I want a pure .csv file that i can open in excel containg only the text and no other elements like the merged cell , headers etc..

Upvotes: 3

Views: 9978

Answers (3)

Abby
Abby

Reputation: 149

Never mind I found it,

 xlsheet.SaveAs(destination, Excel.XlFileFormat.xlCSV);
 xlworkbook.Close();

had to use the Excel.XlFileFormat.xlCSV

Upvotes: 0

Bibin
Bibin

Reputation: 542

Checkout the .SaveAs() method in Excel object.

  xlsheet.SaveAs(@"C:\Users\AbrahamSamuel\Desktop\sample\new.csv", Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV)

In the other way is given below.

public static void SaveAs()
{
    Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();
    Microsoft.Office.Interop.Excel.Workbook xlsheet= app.Workbooks.Add(Type.Missing);
    Microsoft.Office.Interop.Excel.Sheets wsSheet = xlsheet.Worksheets;
    Microsoft.Office.Interop.Excel.Worksheet CurSheet = (Microsoft.Office.Interop.Excel.Worksheet)wsSheet[1];

    Microsoft.Office.Interop.Excel.Range thisCell = (Microsoft.Office.Interop.Excel.Range)CurSheet.Cells[1, 1];

    thisCell.Value2 = "This is a test.";

    xlsheet.SaveAs(@"C:\Users\AbrahamSamuel\Desktop\sample\new.csv", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    xlsheet.SaveAs(@"C:\Users\AbrahamSamuel\Desktop\sample\new.csv", Microsoft.Office.Interop.Excel.XlFileFormat.xlCSVWindows, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

    xlsheet.Close(false, "", true);
}

Upvotes: 0

Klaus G&#252;tter
Klaus G&#252;tter

Reputation: 11977

You have to specify the FileFormat explicitely, see here:

xlsheet.SaveAs(@"C:\Users\AbrahamSamuel\Desktop\sample\new.csv", XlFileFormat.xlCSV);

Depending on your needs, instead of xlCSV, xlCSVUTF8 or xlCSVWindows might be the right choice.

Upvotes: 4

Related Questions