Save an excel file to a csv file

Ask general questions here.
Thet Thet
Posts: 21
Joined: Fri Jan 11, 2019 7:02 am

Save an excel file to a csv file

Post by Thet Thet » Tue Jul 23, 2019 7:51 am

Hi,

I want to save an excel file with specific wroksheet to a csv file.
How to write this?

thans.

manish
Posts: 45
Joined: Fri Aug 10, 2018 12:46 pm

Re: Save an excel file to a csv file

Post by manish » Tue Jul 23, 2019 8:22 am

Hey

You can use the below code

Code: Select all


			string inputFilepath =  @"C:\temp\testtable.xlsx";
			string outputFilepath = @"C:\Temp\output.csv";
			
			Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();
			Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(inputFilepath);
			
			wb.SaveAs(outputFilepath, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSVWindows);
			wb.Close(false);
			app.Quit();
			
This works for Office 2013 Excel Interop lib.


BR
Manish

Thet Thet
Posts: 21
Joined: Fri Jan 11, 2019 7:02 am

Re: Save an excel file to a csv file

Post by Thet Thet » Wed Jul 24, 2019 2:28 am

Hi manish,

Thanks you so much for your reply.
I also use Office 2013 Excel Interop .
I found an error [HRESULT 0x800A03EC] When I run your code.

manish
Posts: 45
Joined: Fri Aug 10, 2018 12:46 pm

Re: Save an excel file to a csv file

Post by manish » Wed Jul 24, 2019 7:39 am

Hi,

Do you use .xls or .xlsx file extension for the excel sheet you are using?

Once you save the file as .xlsx, it should work fine.

Thet Thet
Posts: 21
Joined: Fri Jan 11, 2019 7:02 am

Re: Save an excel file to a csv file

Post by Thet Thet » Wed Jul 24, 2019 8:28 am

Hi,

I save with .xlsx. I repair a little.

Code: Select all


        	string inputFile = @"C:\Users\Administrator\Desktop\TestingCopy123.xlsx";
        	string outputFile = @"C:\Users\Administrator\Desktop\TestingCopy123__Csv.csv";
        	Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();
        	Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(inputFile);
        	Microsoft.Office.Interop.Excel.Worksheet xlSheet = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets["Sheet1"]; 
        	
        	wb.SaveAs(outputFile,Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV);
        	wb.Close(false);
        	app.Quit();