如何在Word中添加Office图表
问题描述:
大家好,这是我的第一个问题。如何在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”它给我错误。
答
哦。这里没有任何机构能为我的问题提供答案。让我帮助自己。 我已经为上述问题和它的单词编写了这些代码。希望能帮助你。
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代码为上述一个请张贴它。
它给你什么错误?结果不是你想要的结果如何? – 2010-09-10 15:04:16
嗨,感谢您的回复,这个msgraph不像Office 2007图表那么酷,错误是将对象转换为错误类型 – JSJ 2010-09-13 09:16:56