Click here to Skip to main content
15,867,895 members
Articles / Productivity Apps and Services / Microsoft Office / Microsoft Excel
Tip/Trick

How to Convert Excel to CSV using Interop

19 Nov 2013CPOL 80.5K   7   18
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<span>.</span>SaveAs Method

Quote:
Saves changes to the workbook in a different file. 
C#
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)


Proud Indian | Author | TEDx Speaker | Microsoft MVP | CodeProject MVP | Speaker | DZone Most Valuable Blogger| jsfiddler

My Website

taditdash.com

Programming Community Profiles

jsfiddle | Stack Overflow

Social Profiles

Facebook | Twitter | LinkedIn

Awards


  1. DZone Most Valuable Blogger
  2. Microsoft MVP 2014, 2015, 2016, 2017, 2018
  3. Code Project MVP 2014, 2015, 2016
  4. Star Achiever of the Month December 2013
  5. Mindfire Techno Idea Contest 2013 Winner
  6. Star of the Month July 2013

Comments and Discussions

 
QuestionNot working with excel having multiple sheets Pin
Member 1254697325-May-16 20:48
Member 1254697325-May-16 20:48 
GeneralMy vote of 5 Pin
Sibeesh KV13-Nov-14 17:55
professionalSibeesh KV13-Nov-14 17:55 
GeneralRe: My vote of 5 Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)17-Nov-14 3:02
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)17-Nov-14 3:02 
GeneralRe: My vote of 5 Pin
Sibeesh KV17-Nov-14 4:27
professionalSibeesh KV17-Nov-14 4:27 
QuestionSee also this: Pin
dietmar paul schoder29-Jul-14 5:22
professionaldietmar paul schoder29-Jul-14 5:22 
AnswerRe: See also this: Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jul-14 6:42
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jul-14 6:42 
GeneralMy vote of 1 Pin
Aaron Sulwer25-Jul-14 10:12
Aaron Sulwer25-Jul-14 10:12 
GeneralRe: My vote of 1 Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jul-14 0:25
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jul-14 0:25 
QuestionThis method does not work on my computer with Visual C# Express 2010 and Excel 2010 Pin
megreen19-Dec-13 10:05
megreen19-Dec-13 10:05 
AnswerRe: This method does not work on my computer with Visual C# Express 2010 and Excel 2010 Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)6-Jan-14 17:57
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)6-Jan-14 17:57 
Questionboth case not installed? Pin
Andrea Feduzzi19-Nov-13 2:08
professionalAndrea Feduzzi19-Nov-13 2:08 
AnswerRe: both case not installed? Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)19-Nov-13 4:10
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)19-Nov-13 4:10 
GeneralRe: both case not installed? Pin
Andrea Feduzzi19-Nov-13 4:43
professionalAndrea Feduzzi19-Nov-13 4:43 
I imagined... sometimes ctrl+c & ctrl+v are malefic! Laugh | :laugh:
GeneralRe: both case not installed? Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)19-Nov-13 4:55
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)19-Nov-13 4:55 
GeneralVery Elegant Pin
Manoj Ningwani18-Nov-13 4:15
Manoj Ningwani18-Nov-13 4:15 
GeneralRe: Very Elegant Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)18-Nov-13 5:23
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)18-Nov-13 5:23 
GeneralRe: Very Elegant Pin
Aaron Sulwer25-Jul-14 10:11
Aaron Sulwer25-Jul-14 10:11 
GeneralRe: Very Elegant Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jul-14 0:27
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Jul-14 0:27 

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

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