c# - 如何避免在C#、ASP.NET中更改单元格背景后删除Excel工作表的网格线颜色(默认浅灰色)?

标签 c# asp.net excel

我更改单元格颜色的示例代码是这样的。 range 是指单元格范围。

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。

匿名类型 我不明白为什么这对你不起作用?您能否完善您的问题?我尝试了以下方法,效果很好..

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;

关于c# - 如何避免在C#、ASP.NET中更改单元格背景后删除Excel工作表的网格线颜色(默认浅灰色)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25840831/

相关文章:

javascript - 回发后如何停止默认滚动?

c# - 如何在 C# 中从派生类型获取泛型类型的泛型参数类型

c# - 是否有任何基于 ASP.net C# 的开源支付网关?

excel - 复制表格范围并粘贴为值

c# - 无法将 excel 列值复制到另一个 excel 文件中的指定范围

c# - 找不到 ildasm.exe

使用接口(interface)而不是具体类进行 ASP.NET Web API 操作

asp.net - 使用 VB.NET 从纯字符串解析 URL ?a=b 参数

c# - 错误 "The type or namespace name ' Script' does not exist in the namespace 'System.Web' 背后是否有解释?

vba - “找不到文件 : VBA6. dll”错误 - 奇怪的行为