获取C#中打开的Excel文档列表?

时间:2015-02-05 10:54:10

标签: c# winforms

这里我成功地获得了Word文档打开文件列表..

        try
        {
            Microsoft.Office.Interop.Word.Application WordObj;
            WordObj = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");

                x = "";
                for (int i = 0; i < WordObj.Windows.Count; i++)
                {
                    object idx = i + 1;
                    Microsoft.Office.Interop.Word.Window WinObj = WordObj.Windows.get_Item(ref idx);

                    // doc_list.Add(WinObj.Document.FullName);


                    x = x + "," + WinObj.Document.FullName;

                    //x = WinObj.Document.FullName;
                }                                                         

        }
        catch (Exception ex)
        {

            // No documents opened
        }

和我想要的Excel文件列表一样......

        try
        {
             Microsoft.Office.Interop.Excel.Application ExcelObj;
             ExcelObj = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
             //excel = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
                x = "";
                for (int i = 0; i < ExcelObj.Windows.Count; i++)
                {
                    object idx = i + 1;
                    Microsoft.Office.Interop.Excel.Window WinObj = ExcelObj.Windows.get_Item(idx);

                    doc_list.Add(WinObj.Document.FullName);

                  //Here is the problem ,How can i get FullName of opened excel file

                  // x = x + "," + WinObj.Activate.

                  //  x = WinObj.Document.FullName;
                }                  
        }   
        catch (Exception ex)
        {
        }

Excel文件没有...这条线.. 在WinObj.Document.Fullname中,每个word文档都很好。但是 在Excel中 x = x +“,”+ WinObj.Document.FullName; 没有Excel文件名属性的文件...如何使文件全名与单词相同..

1 个答案:

答案 0 :(得分:0)

我在stackoverflow上发现了这个

你可以尝试一下。

//Excel Application Object
Microsoft.Office.Interop.Excel.Application oExcelApp;

this.Activate ( );

//Get reference to Excel.Application from the ROT.
oExcelApp = ( Microsoft.Office.Interop.Excel.Application ) System.Runtime.InteropServices.Marshal.GetActiveObject ( "Excel.Application" );

//Display the name of the object.
MessageBox.Show ( oExcelApp.ActiveWorkbook.FullName );

//Release the reference.
oExcelApp = null;

我在这里找到了这个 How to get the current open documents in Excel using C#?

在这里

http://support2.microsoft.com/kb/316126