2010-09-10 56 views
3

大家好,這是我的第一個問題。如何在Word中添加Office圖表

我想在2007字加一個辦公室圖形使用C#4.0,

我使用Office 2007的話,對於喜歡3D泡泡更好的圖 我的任務是Genrate圖和從SQL數據庫表我已經做了它使用excel圖表,然後將圖表複製爲圖像。

現在我想在詞中添加圖表本身,以便用戶可以更改圖表或其值。 目前我在做這個代碼。

 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」它給我錯誤。

+0

它給你什麼錯誤?結果不是你想要的結果如何? – 2010-09-10 15:04:16

+0

嗨,感謝您的回覆,這個msgraph不像Office 2007圖表那麼酷,錯誤是將對象轉換爲錯誤類型 – JSJ 2010-09-13 09:16:56

回答

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代碼爲上述一個請張貼它。

相關問題