C#设置Excel图表源码
我正在尝试做一些事情,但是很难过。 想法是,我只是循环槽excel工作表,并find所有图表,设置它的来源,然后将该表移动到工作簿的末尾。
我正在使用C#和Microsoft.Office.Interop.Excel
编辑
我之所以需要foreach循环,是因为我不知道在图表上有多less图表,因为我希望允许用户只在图表中放置图表,在从SQLdynamic获取数据并将其放入另一个图表我需要更新这些图表
这是我到目前为止所尝试的
// Get source for charts Excel.Range ChartSourceRange = xlWorkSheet3.Range["A4", "G5"]; // Here becomes the problem ...I don't know how to go foreach in excel sheet foreach (Microsoft.Office.Interop.Excel.ChartObject item in xlWorkBook.Sheets[2]) { item.Chart.SetSourceData(ChartSourceRange); } int totalSheets = xlWorkBook.Sheets.Count; xlWorkBook.Sheets[2].Move(xlWorkBook.Sheets[totalSheets]);
这段代码给我例外…
Unable to cast COM object of type 'System.__ComObject' to interface type 'System.Collections.IEnumerable'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{496B0ABE-CDEE-11D3-88E8-00902754C43A}' failed due to the following error: 'No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))' and the COM component does not support IDispatch::Invoke calls for DISPID_NEWENUM.
你可以折腾我的意见,如何改变这个代码,让我的foreach可以通过Excel表?
谢谢
尝试:
foreach (xl.ChartObject item in xlWorkBook.Sheets[2].ChartObjects()) { item.Chart.SetSourceData(ChartSourceRange); }
另外,为了知道如何在C#(或VB.Net)中使用excel,我build议你看看这里 。 由于它是一个COM对象,所以你需要正确地释放它:
- 实例化新的Excel过程时出错
- 将文件夹中的图像添加到Excel工作表
- 使用CLSID {00024500-0000-0000-C000-000000000046}检索组件的COM类工厂失败,同时在任务调度程序中configurationexe
- 将工作簿名称传递给For循环C#
- 生成服务器中的Microsoft.Office.Interop.Excel参考
- 如何创buildCSV,以便数字在电子表格中显示为string?
- 在C#中用密码打开Excel,并从外部closuresExcel
- C#:如何导出到Excel电子表格? 使用XSLT / LINQ /其他方法?
- 如何强制ADO.Net在读取器TableSchema中仅使用System.String DataType