itay amit
itay amit

Reputation: 33

Importing Excel file and reading cells

I am trying to import an excel file and working on it with visual studio, c#. When I try to create an excel app so I can use it to read the file I am getting this error upon running the code: "System.IO.FileNotFoundException: 'Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. The system cannot find the file specified.'" I saw some people that had this problem and said something about excel 2013 but that's not the case here. Anyone has a solution? I'll be more than glad to hear.

Upvotes: 0

Views: 495

Answers (3)

dear_vv
dear_vv

Reputation: 2358

1.You could install nuget package Microsoft.Office.Interop.Excel first.

2.It is best for you to set Embed Interop Types to be true. here

Configuration: the latest version Microsoft.Office.Interop.Excel, Excel2016

The test code is as follows:

 class Program
{
    static void Main(string[] args)
    {
        ReadExcel("D:\\xxx.xlsx");
    }
    static void ReadExcel(string path)
    {
        Excel.Application app = new Excel.Application();
        Excel.Workbook workbook = app.Workbooks.Open(path);
        Excel.Worksheet worksheet = workbook.Worksheets[1];
        Excel.Range range = worksheet.UsedRange;
        int row = range.Rows.Count;
        int column = range.Columns.Count;
        for (int i = 1; i < column+1; i++)
        {
            for (int j = 1; j < row+1; j++)
            {
                string value = worksheet.Cells[i][j].Text;
                Console.WriteLine(value);
            }
            Console.WriteLine("*********");
        }
        Console.ReadKey();

    }
}

Excel:

here

Result:

here

Upvotes: 0

itay amit
itay amit

Reputation: 33

I actually did what akd said, I used Epplus and it worked very well. Thank you so much for the advise!

Upvotes: 0

Paulo
Paulo

Reputation: 617

you are probably trying to run the app in a PC with no office intalled. Intall this specific version of the office in the PC who will execute the app and try again.

Upvotes: 0

Related Questions