如何在word中添加Office Graph

时间:2010-09-10 11:08:07

标签: ms-word office-interop

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

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

我正在使用Office 2007的单词,以获得更好的图表,如3D Bubble 我的任务是从sql数据库中生成图表和表格我已经使用excel图表以良好的方式完成了它,然后将图表复制为单词中的图像。

现在我想在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); } }

以下代码工作正常,但结果并不如我所愿。 如果我使用“Excel.Chart.8”测试“MSGraph.Chart.8”它给我错误。

1 个答案:

答案 0 :(得分:1)

喔。没有人在这里给出我的问题的答案。让我帮助自己。 我已经为上面的问题写了这些代码,并且它的字很好。希望也能帮到你。

     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

嗨,如果你有一些以上的bueatifull代码请发布。