如何避免在C#,ASP.NET中更改单元格的背景后,删除Excel表格的网格线颜色(默认浅灰色)?
我的示例代码来更改单元格颜色是这样的。 范围是指小区范围。
range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
如果我尝试将单元格颜色改回白色。 我使用这个。
range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
但是这会消除我的细胞范围网格线(线的默认颜色是灰色的),一切都变成白色。 即只有“范围”网格线变成白色,其他所有单元格都具有默认的Excel网格颜色。 谢谢
System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);问题发生的原因是单元格范围的内部颜色实际上不是白色(它只是显示为白色)。 每个单元默认是实际透明的(-4142)。
所以当你想把它设置回“正常”
range.Interior.Color = -4142
编辑:最好使用常量和-4142等于xlNone。
匿名types我不明白为什么这不适合你? 你能改善你的问题吗? 我试了下面,它工作得很好..
const Int32 transparent_Color = -4142; //Set Yellow var Rng = ActiveSheet.Range["D5:E7"]; Rng.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow); //Set transparent (gridlines come back, so long as you haven't got other code that is interacting with your cells) var Rng = ActiveSheet.Range["D5:E7"]; Rng.Interior.Color = transparent_Color;
招数:打开Excel>开发工具栏>录制macros>select/高亮范围>改变背景颜色为黄色>改变背景颜色为白色。 现在将样式设置为正常 。
停止录制macros。
然后按Alt + F11进入VBA编辑器并查看模块1中的代码。
简单地将这些代码转换为C# – 它几乎是C#中与VB完全相同的对象模型。