如何在Word中添加Office图 [英] How to Add Office Graph in word

查看:115
本文介绍了如何在Word中添加Office图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi every one this is my first question here.

我想使用C#4.0在Word 2007中添加Office Graph,

I want to add a Office Graph in word 2007 using C# 4.0,

我正在使用Office 2007 word,以获得3D Bubble等更好的图表 我的任务是从sql数据库生成图表和表格,我已经使用Excel图表很好地做到了这一点,然后将图表复制为Word中的图像.

I am using Office 2007 word, for better charts like 3D Bubble My task is to Genrate Graph and table from sql database i have done it in good menner with using the excel chart then copy chart as image in word.

现在我想在单词中添加图表本身,以便用户可以更改图表或其值. 目前,我正在执行这段代码.

now i want to add chart itself in word so that the user can change the chart or its value. currently i m doing this code.

       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); } }

下面的代码工作正常,但结果不是我想要的好. 如果我使用"MSGraph.Chart.8"的"Excel.Chart.8",则会给我错误.

the below code is working fine but the result is not good as i want. if i use "Excel.Chart.8" insteed of "MSGraph.Chart.8" it gives me error.

推荐答案

哦.这里没有人可以回答我的问题.让我帮助自己. 我已经为上述问题及其正确的代码编写了这些代码.希望也能帮到你.

Oh. no body is here give the answer for my question. let me help my self. I have written these code for above problem and its word fine. hope help u too.

     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

您好,如果您还有其他关于以上代码的完整代码,请发布.

hi there if u have some more bueatifull code for above one please post it.

这篇关于如何在Word中添加Office图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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