protected void AddChart(int chartNumber, string[,] data)
        {
            Word.InlineShape inlineShape = _document.Content.InlineShapes[chartNumber];

            object oRngTarget = _wordApp.Selection.Range;
            object oOLEClass  = "Excel.Sheet.12";

            Word.InlineShape ils = _document.InlineShapes.AddOLEObject(ref oOLEClass, ref missing, ref missing,
                                                                       ref missing, ref missing, ref missing, ref missing, ref oRngTarget);
            Word.OLEFormat olef = ils.OLEFormat;

            //if (inlineShape.OLEFormat.ProgID == "Excel.Chart.12")
            //{
            //inlineShape.Activate();
            Excel.Workbook  wb = (Excel.Workbook)olef.Object;
            Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];

            for (int i = 0; i < data.GetLength(0); i++)
            {
                for (int j = 0; j < data.GetLength(1); j++)
                {
                    ((Excel.Range)ws.Cells[i + 1, j + 1]).Value = data[i, j];
                }
            }
            //}
            //ReplaceText("$picture1.1$", inlineShape);
            Marshal.ReleaseComObject(ils);
        }
Exemplo n.º 2
0
        public static void UpdateGraph2(string outputPath, string[] names, double[] currentValues, int graphIndex)
        {
            Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
            app.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
            app.Visible       = false;
            object missing      = System.Reflection.Missing.Value;
            object templateName = outputPath;                                   //最终的word文档需要写入的位置
            object ModelName    = outputPath;                                   //word  模板的位置
            object count        = 1;
            object WdLine       = Microsoft.Office.Interop.Word.WdUnits.wdLine; //换一行;


            Microsoft.Office.Interop.Word.Document wordDoc = app.Documents.Open(ref ModelName, ref missing, ref missing,
                                                                                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                                                                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                                                                ref missing);//打开word模板


            try
            {
                int fieldCount = names.Length;
                Microsoft.Office.Interop.Word.InlineShape shape     = wordDoc.InlineShapes[graphIndex];
                Microsoft.Office.Interop.Word.OLEFormat   oleformat = shape.OLEFormat;
                oleformat.Open();
                Microsoft.Office.Interop.Graph.Chart chart = (Microsoft.Office.Interop.Graph.Chart)(oleformat.Object);
                chart.Application.DataSheet.Cells.ClearContents();
                chart.Application.DataSheet.Cells.set_Item(1, "", "名称");
                for (int i = 0; i < names.Length; i++)
                {
                    chart.Application.DataSheet.Cells.set_Item(i + 2, "", names[i]);
                    chart.Application.DataSheet.Cells.set_Item(i + 2, "A", currentValues[i]);
                }
                chart.Application.Update();
                chart.Application.DisplayAlerts = false;
                chart.Application.Quit();

                object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault;//保存格式
                wordDoc.SaveAs(ref templateName, ref format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            }
            catch
            {
            }
            finally
            {
                //关闭wordDoc,wordApp对象
                object SaveChanges    = Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges;
                object OriginalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;
                object RouteDocument  = false;
                wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
                app.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
            }
        }