如何在Word中添加Office图表

3

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

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

我正在使用Office 2007 Word,为了更好的图表,比如3D气泡。我的任务是从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); } }

下面的代码可以正常工作,但是结果不如我所愿。 如果我使用"Excel.Chart.8"而不是"MSGraph.Chart.8",它会给我一个错误。

它给你什么错误?另外,结果为什么不是你想要的? - David B Heise
嗨,感谢您的回复。这个MSGraph没有像Office 2007那样酷炫的图表,并且错误是从对象到类类型的转换错误。 - JSJ
1个回答

3

哦,没有人回答我问题。让我自己来帮助自己吧。 我已经为上述问题编写了这些代码,并且它的运行良好。希望能对你有所帮助。

     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

嗨,如果你有更漂亮的代码,请分享出来。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接