在Word文档中编辑Excel电子表格对象(C#Interop) [英] Edit Excel Spreadsheet object in word document (C# Interop)

查看:97
本文介绍了在Word文档中编辑Excel电子表格对象(C#Interop)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用c#互操作编辑excel电子表格对象的单元格.我将其插入到Word文档中作为对象.

I am currently trying to edit cells of an excel spreadsheet object with c# interop. I inserted it in a word document as an object.

直到那里我都没有成功编程任何真正有效的东西.我能够选择组件,但无法打开它进行编辑,然后到达网格的单元格.

Until there i didn't succeed to programm anything that really works. I'm able to select the component but i can't open it to edition and then reach grid's cells.

我在自定义办公功能区中使用按钮控件来启动编辑.这是我的方法:

I use a button control in a custom office ribbon to launch edit. Here is my method:

public void EditTable(Office.IRibbonControl control)
    {
        Word.Application oWordApp = (Word.Application)Marshal.GetActiveObject("Word.Application");
        Word.Document oWordDoc = oWordApp.ActiveDocument;

        Word.Bookmark ReqsBookmark = DocumentHelper.GetBookmark("test");

        ReqsBookmark.Select();
    }

我知道通过互操作访问特定对象的唯一方法是使用书签.

The only way i know to access a specific object with interop is with bookmarks.

有人知道如何做这样的事情吗?

Does anybody have an idea of how doing such a thing?

推荐答案

在Word中,Excel工作表(工作簿)被包装"在作为InlineShapesShapes集合成员的OLE控件中.因此,您需要使用要使用的集合的AddOLEObject方法.

In Word, an Excel worksheet (workbook) is "wrapped" in an OLE control that is a member of the InlineShapes or Shapes collection. So you need the AddOLEObject method of the collection you want to use.

通过InlineShapeShapeOLEFormat属性访问OLE服务器(Excel)的对象模型.因此,您的代码将类似于下面的示例.

Access to the object model of the OLE server (Excel) is through the OLEFormat property of the InlineShape or Shape. So your code would be something like the sample below.

请注意,尽管您说这是一个VSTO项目,但显示给我们的代码不是VSTO.您正在启动Word.Application的新实例,但是VSTO加载项将在进程中运行.我的代码是VSTO代码,但是当然可以针对其他情况进行调整...

Note that although you say this is a VSTO project, the code you show us is not VSTO. You're starting up a new instance of the Word.Application, but the VSTO Add-in would be running in-process. My code is VSTO code, but can certainly be adjusted for other situations...

{
    Word.Document doc = Globals.ThisAddIn.app.ActiveDocument;
    object oRngTarget = Globals.ThisAddIn.app.Selection.Range;
    //object oRngTarget = DocumentHelper.GetBookmark("test").Range;
    object oOLEClass = "Excel.Sheet.12";
    object oFalse = false;
    Word.InlineShape ils = doc.InlineShapes.AddOLEObject(ref oOLEClass, ref missing, ref missing, 
                               ref missing, ref missing, ref missing, ref missing, ref oRngTarget);
    Word.OLEFormat olef = ils.OLEFormat;
    System.Globalization.CultureInfo  oldCI= System.Threading.Thread.CurrentThread.CurrentCulture;
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

    Excel.Workbook wb = (Excel.Workbook)olef.Object;
    Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];

    try
    {
        ws.get_Range("A1").Value2 = "New category";
        ws.get_Range("B1").Value2 = 6.8;
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.Print(ex.Message);
    }
    finally
    {
        ws = null;
        wb = null;
        ils = null;
        doc = null;
        System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
    }
}

要稍后在Word文档中使用电子表格,您基本上遵循相同的原则:声明并实例化InlineShape.OLEFormat对象,将其激活,然后将olef.Object转换为Excel.Workbook:

To later work with a spreadsheet in the Word document, you basically follow the same principle: declare and instantiate a InlineShape.OLEFormat object, Activate it, then cast olef.Object to an Excel.Workbook:

olef.Activate();
Excel.Workbook wb = (Excel.Workbook)olef.Object;

这篇关于在Word文档中编辑Excel电子表格对象(C#Interop)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆