Ms word 如何在word中添加Office图形

Ms word 如何在word中添加Office图形,ms-word,office-interop,Ms Word,Office Interop,大家好,这是我的第一个问题 我想使用C#4.0在word 2007中添加一个Office图形 我正在使用Office2007 word,以获得更好的图表,如3D Bubble 我的任务是从sql数据库中生成图形和表格。我已经使用excel图表很好地完成了这项工作,然后将图表作为图像复制到word中 现在我想在word中添加图表本身,以便用户可以更改图表或其值。 目前我正在做这个代码 object missing = Type.Missing;

大家好,这是我的第一个问题

我想使用C#4.0在word 2007中添加一个Office图形

我正在使用Office2007 word,以获得更好的图表,如3D Bubble 我的任务是从sql数据库中生成图形和表格。我已经使用excel图表很好地完成了这项工作,然后将图表作为图像复制到word中

现在我想在word中添加图表本身,以便用户可以更改图表或其值。 目前我正在做这个代码

       object missing = Type.Missing;          

        Word.Application application = new
        Microsoft.Office.Interop.Word.Application();
        application.Visible = true;
        Word.Document document = application.Documents.Add(ref missing, ref missing, ref missing,
        ref missing);
        Random rd = new Random();

        objchart = (Graph.Chart)document.Shapes.AddOLEObject("MSGraph.Chart.8").OLEFormat.Object;
        dataSheet = objchart.Application.DataSheet;
        for (int r = 1; r < 10; r++)
        { for (int c = 1; c < 5; c++) { dataSheet.Cells[r, c] = rd.Next(10, 50); } }
对象缺失=类型。缺失;
Word.Application=new
Microsoft.Office.Interop.Word.Application();
application.Visible=true;
Word.Document Document=application.Documents.Add(ref-missing,ref-missing,ref-missing,
参考号缺失);
随机rd=新随机();
objchart=(Graph.Chart)document.Shapes.AddOLEObject(“MSGraph.Chart.8”).OLEFormat.Object;
数据表=objchart.Application.dataSheet;
对于(int r=1;r<10;r++)
{for(intc=1;c<5;c++){dataSheet.Cells[r,c]=rd.Next(10,50);}
下面的代码工作正常,但结果不是我想要的那么好。
如果我使用“Excel.Chart.8”而不是“MSGraph.Chart.8”,这会给我带来错误

哦。这里没有人回答我的问题。让我自助吧。 我已经为上面的问题写了这些代码,它的单词是fine。希望能帮助你

     object missing = Type.Missing;

        Word.Application application = new Microsoft.Office.Interop.Word.Application();
        application.Visible = true;
        Word.Document document = application.Documents.Add(ref missing, ref missing, ref missing, ref missing);
        object classtype = "Excel.Chart.8";
        object oEndOfDoc = "\\endofdoc";
        Word.InlineShape wrdInlineShape = document.InlineShapes.AddOLEObject(classtype);
        if (wrdInlineShape.OLEFormat.ProgID == "Excel.Chart.8")
        {
            // Word doesn't keep all of its embedded objects in the running state all the time.
            // In order to access the interface you first have to ensure the object is in the running state,
            // ie: OLEFormat.Activate() (or something)
            object verb = Word.WdOLEVerb.wdOLEVerbHide;
            wrdInlineShape.OLEFormat.DoVerb(ref verb);
            Random rn = new Random();
            Excel.Workbook obook = (Excel.Workbook)wrdInlineShape.OLEFormat.Object;
            Excel.Worksheet sheet = (Excel.Worksheet)obook.Worksheets["Sheet1"];
            for (int i = 1; i <= 7; i++)
            {
                for (int c = 1; c <= 4; c++)
                {
                    ((Excel.Range)sheet.Cells[i, c]).Value = rn.Next(10, 50);
                    ((Excel.Range)sheet.Cells[i, c]).Value = rn.Next(10, 50);
                }
            }
            wrdInlineShape.Width = 400;

            obook.ActiveChart.ChartType = Excel.XlChartType.xlBubble3DEffect;
            Word.Range wrdRng = document.Bookmarks.get_Item(ref oEndOfDoc).Range;
            object oRng = document.Bookmarks.get_Item(ref oEndOfDoc).Range;
            wrdRng = document.Bookmarks.get_Item(ref oEndOfDoc).Range;
            sheet.UsedRange.Copy();
            document.SetDefaultTableStyle("Light List - Accent 4", false);
            for (int i = 0; i < 5; i++)
            {
                wrdRng.InsertBreak(Word.WdBreakType.wdLineBreak); 
            }
            wrdRng.PasteExcelTable(true, true, false);
            wrdInlineShape.ConvertToShape();
        }
        // quit the word
对象缺失=类型。缺失;
Word.Application Application=新的Microsoft.Office.Interop.Word.Application();
application.Visible=true;
Word.Document Document=application.Documents.Add(ref-missing,ref-missing,ref-missing,ref-missing,ref-missing,ref-missing);
对象classtype=“Excel.Chart.8”;
对象oEndOfDoc=“\\endofdoc”;
Word.InlineShape wrdInlineShape=document.InlineShapes.AddOLEObject(类类型);
如果(wrdInlineShape.OLEFormat.ProgID==“Excel.Chart.8”)
{
//Word不会使其所有嵌入对象始终处于运行状态。
//要访问接口,首先必须确保对象处于运行状态,
//ie:OLEFormat.Activate()(或其他)
宾语动词=Word.WdOLEVerb.wdoleverbide;
wrdInlineShape.OLEFormat.DoVerb(ref动词);
Random rn=新的Random();
Excel.Workbook obook=(Excel.Workbook)wrdInlineShape.OLEFormat.Object;
Excel.Worksheet sheet=(Excel.Worksheet)obook.Worksheets[“Sheet1”];

对于(int i=1;i)它给您带来了什么错误?结果如何不是您想要的?您好,感谢您的回复此msgraph没有office 2007图形那么酷,错误是将错误从对象转换为类类型