Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
您好我正在编写 C# 代码以在 Excel 表中生成图表,从 SQL 服务器获取数据。编写以下代码来声明图表。它为索引提供了例外。帮我解决这个问题。
Worksheet sheet1 = workbook.Worksheets[0]; Worksheet sheet2 = workbook.Worksheets[1]; Chart chart = sheet2.Charts[0];
例外是在图表声明中。
提前致谢
可能会发生异常,因为 sheet2 的 Charts 集合为空。在调用 Charts[0] 之前检查 sheet2.Charts 的计数是否大于零:
if (sheet2.Charts.Count > 0) { Chart chart = sheet2.Charts[0]; }