-4

I am very new to C# and hoping this is a simple question. Not finding what I need on google

I have a file C:\test\losses.csv That I want to open up then convert to an xlsx file and save in a different directory. Save to C:\test\Losses.xlsx The reason for opening the file is the move command does not convert it to xlsx, unfortunately it keeps the same structure as the csv and is unusable in that format.
File.Copy(@"C:\test\losses.csv", @"C:\test1\Losses.xlsx"); The above code works great but still is a csv file (well really a hybrid of one). That is another SAP story.
Any help will be greatly appreciated. Thanks

5
  • It's not very clear what you're asking. You start with a CSV and you want to convert it to xlsx? You'll have to use office interop to do that, copying it around won't help. By copying or moving it, at best you might accidentally change the metadata so that excel treats it differently. Commented Jul 21, 2015 at 15:11
  • you will have to use a library to convert it to an actual excel file. Commented Jul 21, 2015 at 15:11
  • Changing extension for the file does not change the /format/ of the file. A simple solution would be to open the file in excel, then save it as an xlsx. Commented Jul 21, 2015 at 15:11
  • Well, of course... copying a file doesn't change its content or format. Commented Jul 21, 2015 at 15:11
  • 1
    Duplicate of stackoverflow.com/questions/16732343/… Commented Jul 21, 2015 at 15:13

1 Answer 1

0

File.Copy only copies the file - similar to copying a file in DOS or in windows file explorer.

You'd need to translate your CSV to an XLSX file. The format should be pretty straight-forward, but you'll need to do more research:

  1. Load the CSV as a data table
  2. Use the Excel.XlFileFormat.xlOpenXMLWorkbook class to translate the file.

A different StackOverflow problem addresses how to use the xlOpenXMLWorkbook: Exporting to .xlsx using Microsoft.Office.Interop.Excel SaveAs Error

Hope this helps. Good luck.

Sign up to request clarification or add additional context in comments.

1 Comment

I like what Stas Berkov posted as a better link to address this issue - but I wanted to keep the reference to the link I posted because it highlights to new coders, as yourself, that sometimes an obvious looking solution is not the correct one. Digging around and becoming familiar with various APIs is the best method to address coding solutions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.