简体   繁体   English

获取Excel文件的名称

[英]Getting the name of a excel file

Let me first tell that it's the first time I'm doing this. 首先,我要说的是这是我第一次这样做。

I'm developing a add-in in c# for excel where a user is suposed to open a file and then press a start button in the add-in to start the all process. 我正在为excel用c#开发一个插件,建议用户打开文件,然后在插件中按开始按钮以启动所有过程。

Now, what I want to know is how to get the name of the open file. 现在,我想知道的是如何获取打开文件的名称。 I don't know where the file is located, so I i can't use FileInfo. 我不知道文件在哪里,所以我不能使用FileInfo。

The file name is of this type "Cxxx_Curr_REC_surplus.xls". 文件名的类型为“ Cxxx_Curr_REC_surplus.xls”。

I've been trying, but all I get is the name of the active workbook. 我一直在尝试,但是我得到的只是活动工作簿的名称。

I'd like to know if this is possible. 我想知道是否可行。

Try this: 尝试这个:

Microsoft.Office.Interop.Excel.Application myExcel;
this.Activate ( );
myExcel = ( Microsoft.Office.Interop.Excel.Application ) System.Runtime.InteropServices.Marshal.GetActiveObject ( "Excel.Application" )
MessageBox.Show ( myExcel.ActiveWorkbook.FullName ); // gives full path
MessageBox.Show ( myExcel.ActiveWorkbook.Name ); // gives file name

Reference: 参考:
How to use Visual C# to automate a running instance of an Office program 如何使用Visual C#自动化Office程序的运行实例
MSDN MSDN

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

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