如何使用 C# 将单元格 A1:A5 和 B1:B5 和 C1:C5 中的颜色填充为相同的颜色?
我目前正在使用以下代码来用黄色填充 A1 到 A5 和 B1 到 B5 范围内的单元格:
chartRange1 = xlWorkSheet.get_Range("A1", "A5");
chartRange1.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
Excel.Range chartRange2;
chartRange2 = xlWorkSheet.get_Range("B1", "B5");
chartRange2.Interior.Color= System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
但需要 2 个 chartRange
对象才能执行此操作。如果我想在大范围的单元格上设置相同的颜色怎么办?
有没有办法使用单个语句为更大范围的单元格设置相同的颜色来做到这一点?
I am currently using the following code to fill cells in the range A1 to A5, and B1 to B5, with yellow:
chartRange1 = xlWorkSheet.get_Range("A1", "A5");
chartRange1.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
Excel.Range chartRange2;
chartRange2 = xlWorkSheet.get_Range("B1", "B5");
chartRange2.Interior.Color= System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
But it takes 2 chartRange
objects in order to do this. What if I want to set the same color over a wide range of cells?
Is there a way to do this using a single statement that sets the same color for a larger range of cells?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的情况下(A1:A5,B1:B5,C1:C5),您可以将单元格合并到连续范围A1:C5:
但范围不必是连续的。您还可以使用如下代码:
In your case (A1:A5, B1:B5, C1:C5) you can merge the cells into a contiguous range A1:C5:
But a range does not have to be contiguous. You can also use code like the following:
你尝试过吗
Have you tried
您是否可以将
chartRange1
对象设置为null
,然后重新初始化并设置下一个范围?Are you able to just set the
chartRange1
object tonull
, then reinitialize and set the next range?