简体   繁体   中英

Why does Microsoft.Office.Interop.Excel require a full path name to open a file?

I have been trying for too long to open an Excel file in a C# application created in Visual Studio 2012. I finally found that I need to provide the full path name to the Excel file, even though it exists in the same folder as the executable. Why is that?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Excel = Microsoft.Office.Interop.Excel;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application ExcelApp = new Excel.Application();
            try
            {
                MessageBox.Show("Current directory: " + Directory.GetCurrentDirectory());
                if (File.Exists("PLC01.xls"))
                {
                    MessageBox.Show("Target file exists.");
                }
                else
                {
                    MessageBox.Show("Target file does not exist.");
                }
                Excel.Workbook workbook = ExcelApp.Workbooks.Open(Directory.GetCurrentDirectory() + "\\PLC01.xls");

                // Excel.Workbook workbook = ExcelApp.Workbooks.Open("PLC01.xls");
                workbook.Close();
                MessageBox.Show("Book opened and closed.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
}

This version works. But if I comment out the first call to Open() and uncomment the second, which only gives the file name and not the full path, I am told that the file can't be found, even though the "File exists" message box appears, verifying its existence.

https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.getcurrentdirectory?view=netframework-4.8

Directory.GetCurrentDirectory -

"Gets the current working directory of the application"

"The application" here is your C# executable: the launched Excel application instance is not running in the same context as your C# executable and so its "current directory" isn't necessarily the same.

If you know the full path, just pass that to Excel - no need to try to pass only the file name.

FYI - GetCurrentDirectory may not be the most robust way to locate your Excel file

How can I get the application's path in a .NET console application?

It seems it is some kind of "hidden" implementation by Microsoft, that the Workbooks.Open() requires the complete path. To be honest, this is rather strange, because this one in VBA works quite nicely and the Excel object should be the same (unless I am not mistaken):

Sub TestMe()

    Dim wb As Workbook
    Set wb = Workbooks.Open("test.xlsx")

End Sub

For.NetFramework, I have tried to save a file in the same folder, in which the Excel.exe is:

C:\Program Files (x86)\Microsoft Office\Office14

and I still got the error for file not found.

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