Hello
I am looking for an application that can convert a file in csv format to excel
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Ramesh MaruthiPosted Dec 17, 2014, 8:54 AM
You can go with the Microsoft.Office.Interop.Excel assembly. Just reference it in your project and you can use it to manipulate Excel documents in any way you want.
Here's a very simple example of how to open a txt file and save it to Excel. You can enhance it to open a csv file by paying attention to the .Workbooks.OpenText() procedure. I haven't had the chance to test this but let me know if it helped you out.
//Open the .txt file in excel as a csv and save it as an excel document
Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.ApplicationClass();
try
{
Microsoft.Office.Interop.Excel.Workbook wb;
Object mv = System.Reflection.Missing.Value;//Represents the null parameters
xl.Workbooks.OpenText(Server.MapPath("~/Uploads/" + strRandomName + ".txt"), mv, 1, Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited, Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierDoubleQuote, mv, mv, mv, true, mv, mv, mv, mv, mv, mv, mv, mv, mv);//Open the excel sheet
wb = xl.Workbooks[1];//Get the only workbook that was just created by the OpenText command
wb.SaveAs(Server.MapPath("~/Uploads/" + strRandomName + ".xls"), Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel5, mv, mv, mv, mv, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, mv, mv, mv, mv);//Save as an EXCEL file
wb.Close(false, false, false);//This is done to avoid the message "Do you want to save the changes to myfile.xls? Microsoft Office Excel recalculates formulas when opening files last saved by an earlier version of Excel" even though the file was opened in readonly mode and no changes happened.
}
catch (Exception ex)
{
//Do what you have to do with the exception
}
finally
{
xl.Quit();
}
c this link :
https://www.linkedin.com/groups/How-can-i-convert-csv-40949.S.129008529
Hope this helps you :)