简体   繁体   中英

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.

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.

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

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. 这里

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:

这里

Result:

这里

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM