xyzio

Split an Excel file’s tabs into CSV with C#

leave a comment »

Split an Excel file into csv, with each tab going into a separate csv file, in the background using C#.


using System.IO;
using Microsoft.Office.Interop.Excel;

Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();

//Run in background
app.DisplayAlerts = false;

//Open excel file
Workbook wb = app.Workbooks.Open("path_to_excel.xls");

//Iterate through sheets in the excel file
foreach (Worksheet sheet in wb.Worksheets)
{
    string sheetName = sheet.Name;
    
    //Output file name
    string outputFilepath = sheetName + ".csv";
    
    //Save the sheet as CSV
    sheet.SaveAs(outputFilepath, XlFileFormat.xlCSVWindows);
}

//Clean-up
wb.Close();
app.Quit();


Written by M Kapoor

December 24, 2019 at 3:19 am

Posted in c#, Programming

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: