-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

  • 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. – NibblyPig Jul 21 '15 at 15:11
  • you will have to use a library to convert it to an actual excel file. – Daniel A. White Jul 21 '15 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`. – ldam Jul 21 '15 at 15:11
  • Well, of course... copying a file doesn't change its content or format. – Jashaszun Jul 21 '15 at 15:11
  • 1
    Duplicate of http://stackoverflow.com/questions/16732343/converting-excel-file-from-csv-to-xlsx – Stanislav Berkov Jul 21 '15 at 15:13

1 Answers1

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.

Community
  • 1
  • 1
Paurian
  • 1,372
  • 10
  • 18
  • 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. – Paurian Jul 21 '15 at 15:20