none
vsto 怎么识别某个内嵌excel表的单元格数据 RRS feed

  • 问题

  • vsto中,我能获取嵌入在word中的excel某单元格的数据,现在我的问题是假如我这里面有多个内嵌excle,我怎么去定位,或者知道是哪个excle,代码如下:
     public static void GetExcleData()
            {
                object VerbIndex = 1;
                object missing = Type.Missing;
                Microsoft.Office.Interop.Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument as Microsoft.Office.Interop.Word.Document;
                foreach (Microsoft.Office.Interop.Word.InlineShape inlineShape in doc.InlineShapes)
                {
                    if (inlineShape.OLEFormat.ProgID != null)
                    {
                        switch (inlineShape.OLEFormat.ProgID)
                        {
                            case "PowerPoint.Show.12":
                                //inlineShape.OLEFormat.DoVerb(ref VerbIndex);
                                //PowerPoint.Application ppt = Marshal.GetActiveObject("PowerPoint.Application") as PowerPoint.Application;
                                //ppt.ActivePresentation.SaveAs(@"C:\testPPT.pptx", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPresentation, Microsoft.Office.Core.MsoTriState.msoTrue);
                                //ppt.Quit();
                                break;
                            case "Excel.Sheet.12":                            
                                inlineShape.OLEFormat.Activate();//Edit///inlineShape.OLEFormat.DoVerb(ref VerbIndex);
                                Microsoft.Office.Interop.Excel.Application excel = Marshal.GetActiveObject("Excel.Application") as Microsoft.Office.Interop.Excel.Application;             
                                Excel.Worksheet activeWorksheet = ((Excel.Worksheet)excel.ActiveSheet);
                                Excel.Range firstRow = activeWorksheet.get_Range("H4", missing);//这里获取内容,但是怎么识别是哪个表
                                System.Windows.Forms.MessageBox.Show(firstRow.Text.ToString());                       
                                excel.Quit();
                                break;
                            case "Word.Document.12":
                                Microsoft.Office.Interop.Word.Document document = inlineShape.OLEFormat.Object as Microsoft.Office.Interop.Word.Document;
                                object fileName = @"C:\testDOC.docx";
                                document.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing,
                                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                ref missing, ref missing, ref missing);
                                break;
                            default:
                                break;
                        }
                    }
                }
            }

    2014年1月13日 6:46

答案

  • 你好:

    你是想判断内嵌的Excel表文件的文件名称是吧?

    可以通过以下方法:

    string fileName=inlineShape.OLEFormat.IconLabel;
    这个属性就代表着原始的嵌入Word文档中的Excel文件的名称。我已经作过测试,可以获取到。 如果你想判断Excel文件里面的Sheet的名称的话直接调用excel.Sheets[i].Name就可以了。

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2014年1月14日 7:52
    版主