Interop C# .net 6.0 Excel system.IO.FileNotFoundException could not load

Viewed 318

I have a question about .net 6.0 and Excel, specifically about interop.

I had a look at this post which seems similar but it doesn't really answer my question.

Is it possible to use .net 6.0 with Excel/Interop? if so how do I deal with the following issue;

    System.IO.FileNotFoundException     HResult=0x80070002

Message=Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. The system cannot find the file specified. Source= StackTrace

The code example im working with, rough draft

                using Microsoft.Office.Interop.Excel;

                Console.WriteLine($"Processing: {FullPathToFile}");
                string nameOfFile = FullPathToFile.Split('\\').Last();
                string newLocation = $@"{ProcessedDir}\{nameOfFile}";

                if (File.Exists(newLocation))
                    File.Delete(newLocation);

                var application = new Application();
                var workbook = application.Workbooks.Open(FullPathToFile);
                var workBookName = workbook.Name;

                int worksheetcount = workbook.Worksheets.Count;
                if (worksheetcount > 0)
                {
                    Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
                    string worksheetName = worksheet.Name;

                    var csvFileName = ($@"{PickUpFileDir}\{nameOfFile.Split('.').First()}.csv");

                    worksheet.SaveAs(csvFileName);
                    //worksheet.ExportAsFixedFormat((XlFixedFormatType)XlFileFormat.xlCSVWindows, csvFileName);

                    Console.WriteLine(worksheetName);

All I want to achieve is to open an excel/ods file, then export the first worksheet as a CSV file.

Would be more than happy with a .net core 6.0 example for the demonstrating how C# can expoert the CSV from an excel file.

Should I be using ExportAsFixedFormat() and if so how do I specify CSV. I had a look at some documentation, but didn't get far.

or should I just be using .SaveAs() and in which case, how do I specify CSV as the output format?

Thanks

0 Answers
Related