简体   繁体   English

为什么 Microsoft.Office.Interop.Excel 需要完整路径名才能打开文件?

[英]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.我一直在尝试在 Visual Studio 2012 中创建的 C# 应用程序中打开 Excel 文件。我终于发现我需要提供 ZC1D81AF5835844B4E9D936910DED8 的完整路径名,尽管它与 FDCZZ 可执行文件存在于同一个文件夹中, . 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.但是,如果我注释掉对 Open() 的第一个调用并取消注释第二个,它只给出文件名而不是完整路径,我被告知找不到文件,即使“文件存在”消息框出现,验证了它的存在。

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

Directory.GetCurrentDirectory - 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.这里的“应用程序”是您的 C# 可执行文件:启动的 Excel 应用程序实例未在与您的 C# 可执行文件相同的上下文中运行,因此它的“当前目录”不一定相同。

If you know the full path, just pass that to Excel - no need to try to pass only the file name.如果您知道完整路径,只需将其传递给 Excel - 无需尝试仅传递文件名。

FYI - GetCurrentDirectory may not be the most robust way to locate your Excel file仅供参考 - GetCurrentDirectory可能不是找到 Excel 文件的最可靠方法

How can I get the application's path in a .NET console application? 如何在 .NET 控制台应用程序中获取应用程序的路径?

It seems it is some kind of "hidden" implementation by Microsoft, that the Workbooks.Open() requires the complete path.似乎是 Microsoft 的某种“隐藏”实现, Workbooks.Open()需要完整路径。 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):老实说,这很奇怪,因为 VBA 中的这个很好用,而 Excel object 应该是一样的(除非我没记错):

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:对于.NetFramework,我尝试在同一个文件夹中保存一个文件,其中Excel.exe是:

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

and I still got the error for file not found.我仍然收到找不到文件的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Microsoft.Office.Interop.Excel损坏了我的Excel文件? - Why does Microsoft.Office.Interop.Excel corrupts my excel file? 应用程序使用 Microsoft.Office.Interop.Excel 打开 Excel 文件与服务器上的 RDP 会话冲突 - Application using Microsoft.Office.Interop.Excel to open excel file conflict with RDP sessions on the server Microsoft.Office.Interop.Excel 打开和工作表错误 - Microsoft.Office.Interop.Excel Open and Sheet error Microsoft.Office.Interop.Excel或EPPlus,用于读取巨大(或不巨大)的Excel文件 - Microsoft.Office.Interop.Excel or EPPlus for read a huge (or not) Excel file 组件名称列表中缺少Microsoft.Office.Interop.Excel - Microsoft.Office.Interop.Excel missing in the Component Name list Office 2007的Microsoft.Office.Interop.Excel - Microsoft.Office.Interop.Excel for Office 2007 Microsoft.Office.Interop.Excel-* .csv文件打开 - Microsoft.Office.Interop.Excel - *.csv file opening 无法加载文件或程序集“ Microsoft.Office.Interop.Excel” - Could not load file or assembly 'Microsoft.Office.Interop.Excel' “无法加载文件或程序集”- Microsoft.Office.Interop.Excel - "Cannot load file or assembly" - Microsoft.Office.Interop.Excel 通过使用Microsoft.Office.Interop.Excel将文件另存为PDF - Saving file as PDF by using Microsoft.Office.Interop.Excel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM