Excel: can only open file if using absolute path, why?

后端 未结 4 2026
感情败类
感情败类 2021-01-12 05:11

I have some trouble to understande why I´m getting an exception. I have something like this:

string path = \"file.xls\";
if (File.Exists(path))
{
  Excel.App         


        
4条回答
  •  悲哀的现实
    2021-01-12 05:37

    I found Matthew Watson's answer very helpful. It's also useful to set a relative path for Workbooks.Add() method. Here is my code for reference:

    Excel.Application xlApp = new Excel.Application();
    
    string path = "TestTemplete.xltx";
    path = System.IO.Path.GetFullPath(path);
    Workbook wb = xlApp.Workbooks.Add(path);
    

提交回复
热议问题