Click here to Skip to main content
Click here to Skip to main content

How to Convert Excel to CSV using Interop

, 19 Nov 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This simple Tip will give you the Trick to convert Excel file to CSV file using Interop Services.

Introduction

At times, you might need to convert one Excel file to CSV. If you want to do this using Microsoft's Interop Services, then follow this Tip.

Background  

This little piece of code is a result of the research during development of one Windows Utility, which uploads Excel sheets to database.

Using the code 

First of all, we will check if Excel is installed on the system or not, as Interop works only when Excel is installed on the system. Follow my previous Tip -  How to Check Whether Excel is Installed in the System or Not

To save the file as CSV, we will use Workbook.SaveAs Method

Quote:
Saves changes to the workbook in a different file. 
Type officeType = Type.GetTypeFromProgID("Excel.Application");

if (officeType == null)
{
    // Excel is not installed.
    // Show message or alert that Excel is not installed.
}
else
{
    // Excel is installed.
    // Let us continue our work on Excel file conversion. 
    Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();

    // While saving, it asks for the user confirmation, whether we want to save or not.
    // By setting DisplayAlerts to false, we just skip this alert.
    app.DisplayAlerts = false;

    // Now we open the upload file in Excel Workbook. 
    Microsoft.Office.Interop.Excel.Workbook excelWorkbook = app.Workbooks.Open(openFileDialog.FileName);

    string newFileName = System.IO.Directory.GetCurrentDirectory() + "\\DataMigration.csv";

    // Now save this file as CSV file.
    excelWorkbook.SaveAs(newFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV);
    
    // Close the Workbook and Quit the Excel Application at the end. 
    excelWorkbook.Close();
    app.Quit();
}

So, the Excel file is now saved as CSV file inside the Current Directory of the Project. 

History 

  • 18 November 2013 - First version submitted for approval 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)
Software Developer Mindfire Solutions
India India
NOW I am a Conference Speaker. Please invite me to your College, Company, Event or Conference !!!
 
Proud Indian | Microsoft MVP | CodeProject MVP | Author | Blogger | Community Expert | Moderator | jsfiddler | Cricketer | Cook | Dancer
 
Currently working @Mindfire Solutions.
Love to code and help guys to Trap the Bug.

My Tech Blog

taditdash.wordpress.com

Programming Community Profiles

jsfiddle | Stack Overflow

Other Blog Profiles

Mindfire CRM Team Blog | Mindfire Blog

Social Profiles

Facebook | Twitter | LinkedIn | Google+

Awards

  1. Microsoft MVP 2014
  2. Code Project MVP 2014
  3. Star Achiever of the Month December 2013
  4. Mindfire Techno Idea Contest 2013 Winner
  5. Star of the Month July 2013
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
QuestionSee also this: PinprofessionalDietmar Schoder29-Jul-14 5:22 
AnswerRe: See also this: PinprotectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jul-14 6:42 
GeneralMy vote of 1 PinmemberAaron Sulwer25-Jul-14 10:12 
GeneralRe: My vote of 1 PinprotectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jul-14 0:25 
QuestionThis method does not work on my computer with Visual C# Express 2010 and Excel 2010 Pinmembermegreen19-Dec-13 10:05 
AnswerRe: This method does not work on my computer with Visual C# Express 2010 and Excel 2010 PinmvpTadit Dash6-Jan-14 17:57 
Questionboth case not installed? PinmemberAndrea Feduzzi19-Nov-13 2:08 
AnswerRe: both case not installed? PinprofessionalTadit Dash19-Nov-13 4:10 
GeneralRe: both case not installed? PinmemberAndrea Feduzzi19-Nov-13 4:43 
GeneralRe: both case not installed? PinprofessionalTadit Dash19-Nov-13 4:55 
GeneralVery Elegant PinprofessionalManoj Ningwani18-Nov-13 4:15 
GeneralRe: Very Elegant PinprofessionalTadit Dash18-Nov-13 5:23 
GeneralRe: Very Elegant PinmemberAaron Sulwer25-Jul-14 10:11 
GeneralRe: Very Elegant PinprotectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jul-14 0:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.1 | Last Updated 19 Nov 2013
Article Copyright 2013 by Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid