C# Microsoft.Office.Interop.Excel 修改Excel图表数据源

haizhizi2006 2014-11-18 08:12:31
C# Microsoft.Office.Interop.Excel (Dll文件)
动态修改Excel模板中的图表数据源
具体方法是:SetSourceData(range)
方法一:
//wb workbook变量
wb.ActiveChart.SetSourceData(range);

方法二:
// ws worksheet变量
ws.ChartObjects(0).Chart.SetSourceData(range);
以上两个方法都报错。comobject异常。


想知道:怎么获得当前sheet页面中的指定的图表对象?
谁处理过类似的问题,请不吝赐教。谢谢。
...全文
467 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
haizhizi2006 2014-11-19
  • 打赏
  • 举报
回复
Microsoft.Office.Interop.Excel (Dll文件) 有这个方法:SetSourceData(range) 我通过 WorkSheet.ChartObject(0).Chart.SetSourceData(range) ; 调用失败?系统包 com异常。 谁用过啊?
wanliang2001 2014-11-19
  • 打赏
  • 举报
回复
你不要直接处理EXCEL的图表对象。 图表数据的来源的EXCEL中的数据,用数组对数据进行处理,图表自动跟着变化
XiaoGang88_55 2014-11-19
  • 打赏
  • 举报
回复
复制了部分代码,供你参考: Excel.Application excelApp = null; Excel.Workbook excelWorkbook = null; string workbookPath=‘’;//excel文件名字 excelApp = new Excel.ApplicationClass(); excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); string currentSheet = "";//sheet页名字 Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelWorkbook.Sheets.get_Item(currentSheet); Excel.Range excelCell1 = (Excel.Range)excelWorksheet.get_Range("A3", "A3"); excelCell1.Cells[1, 1] ="testString"; Marshal.ReleaseComObject(excelCell1); excelCell1 = null; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); if (excelWorkbook != null) { excelWorkbook.Close(true, Type.Missing, Type.Missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook); excelWorkbook = null; } if (excelWorkbook != null) { excelWorkbook.Close(false, Type.Missing, Type.Missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook); excelWorkbook = null; } if (excelApp != null && excelApp.Workbooks.Count < 1) { excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); excelApp = null; } GC.WaitForPendingFinalizers(); GC.Collect();

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧