Click here to Skip to main content
11,437,397 members (34,900 online)
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 | DZone Most Valuable 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. DZone Most Valuable Blogger
  2. Microsoft MVP 2014
  3. Code Project MVP 2014
  4. Star Achiever of the Month December 2013
  5. Mindfire Techno Idea Contest 2013 Winner
  6. Star of the Month July 2013
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 Pin
Sibeesh KV13-Nov-14 18:55
professionalSibeesh KV13-Nov-14 18:55 
GeneralRe: My vote of 5 Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)17-Nov-14 4:02
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)17-Nov-14 4:02 
GeneralRe: My vote of 5 Pin
Sibeesh KV17-Nov-14 5:27
professionalSibeesh KV17-Nov-14 5:27 
QuestionSee also this: Pin
Dietmar Schoder29-Jul-14 6:22
professionalDietmar Schoder29-Jul-14 6:22 
AnswerRe: See also this: Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jul-14 7:42
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jul-14 7:42 
GeneralMy vote of 1 Pin
Aaron Sulwer25-Jul-14 11:12
memberAaron Sulwer25-Jul-14 11:12 
GeneralRe: My vote of 1 Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jul-14 1:25
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jul-14 1:25 
QuestionThis method does not work on my computer with Visual C# Express 2010 and Excel 2010 Pin
megreen19-Dec-13 11:05
membermegreen19-Dec-13 11:05 
AnswerRe: This method does not work on my computer with Visual C# Express 2010 and Excel 2010 Pin
Tadit Dash6-Jan-14 18:57
mvpTadit Dash6-Jan-14 18:57 
Questionboth case not installed? Pin
Andrea Feduzzi19-Nov-13 3:08
memberAndrea Feduzzi19-Nov-13 3:08 
AnswerRe: both case not installed? Pin
Tadit Dash19-Nov-13 5:10
professionalTadit Dash19-Nov-13 5:10 
GeneralRe: both case not installed? Pin
Andrea Feduzzi19-Nov-13 5:43
memberAndrea Feduzzi19-Nov-13 5:43 
GeneralRe: both case not installed? Pin
Tadit Dash19-Nov-13 5:55
professionalTadit Dash19-Nov-13 5:55 
Yeah, absolutely correct. Poke tongue | ;-P

GeneralVery Elegant Pin
Manoj Ningwani18-Nov-13 5:15
professionalManoj Ningwani18-Nov-13 5:15 
GeneralRe: Very Elegant Pin
Tadit Dash18-Nov-13 6:23
professionalTadit Dash18-Nov-13 6:23 
GeneralRe: Very Elegant Pin
Aaron Sulwer25-Jul-14 11:11
memberAaron Sulwer25-Jul-14 11:11 
GeneralRe: Very Elegant Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jul-14 1:27
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jul-14 1: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 | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 19 Nov 2013
Article Copyright 2013 by Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid