none
reading a csv file with excel from c# RRS feed

  • Question

  • Hi,

    I am trying to convert a csv and text files to an excel file from c#.

    I am trying to open them with excel, then save them.

    I find a file at the end of the execution but it's not taking into consideration the seperator.

                        Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                        app.DisplayAlerts = false;
                        string FilePathToTreat = this.Search(workingDirectoryv1, FileNameToTreat);
                        //Microsoft.Office.Interop.Excel.Workbook excelWorkbook = app.Workbooks.Open(FilePathToTreat);

                        Microsoft.Office.Interop.Excel.Workbook excelWorkbook = app.Workbooks.Open(FilePathToTreat, 0, false, Excel.XlFileFormat.xlCSV, "", "", false, Excel.XlPlatform.xlWindows, charseparator, true, false, 0, true, false, false);
                        string newFileName = pathtemp1 + Path.GetFileNameWithoutExtension(FilePathToTreat) + ".xlsx";
                        excelWorkbook.SaveAs(newFileName);
                        excelWorkbook.Close();
                        app.Quit();

    )

    Would you please help ?

    Thank you for your time

    • Moved by CoolDadTx Wednesday, November 21, 2018 3:28 PM Office related
    Wednesday, November 21, 2018 11:05 AM

Answers

All replies

  • Hello, 

    Would you please help ?

    excelWorkbook.SaveAs(newFileName);

    Take a look on parameter 2 in SaveAs

    https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.saveas


    Sincerely, Highly skilled coding monkey.

    Wednesday, November 21, 2018 11:17 AM
  • Hello,

    Tried, but didn't work ....

    I think the problem is with the open more than it is with the save

    Thank you for your time!

    Wednesday, November 21, 2018 11:45 AM
  • Hi Najeh,

    >>reading a csv file with excel from c#

    Please refer to the following links:

    Read Data From a CSV File in C#

    Reading CSV files using C#

    Read CSV files in C#

    Hopefully it helps you.

    Best Regards,

    Lina


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    • Marked as answer by Najeh Jalil Thursday, November 22, 2018 2:02 PM
    Thursday, November 22, 2018 3:06 AM
  • You could probably try Spire.XLS, it is capable of covnerting CSV and Text files to Excel documents

    how to convert CSV to Excel:

    using Spire.Xls;
    
    namespace csvtoxls
    {
        class Program
        {
            static void Main(string[] args)
            {
             Workbook workbook = new Workbook();
             workbook.LoadFromFile(@"D:\michelle\my file\csv2xls.csv", ",", 1, 1);
             Worksheet sheet = workbook.Worksheets[0];
             sheet.Name = "csv to excel";
             workbook.SaveToFile("result.xls", ExcelVersion.Version2010);        
            }
        }
    }

    Tuesday, November 27, 2018 1:55 AM