简体   繁体   English

如何更改内部而非边框的颜色

[英]How to change color of interior and not borders

I am trying to make an excel add-in with a button that tints all the cells except for the named range. 我正在尝试使用一个按钮来制作一个excel加载项,该按钮可以着色除指定范围之外的所有单元格。

I am using the TintAndShade property, setting it to -0.7. 我正在使用TintAndShade属性,将其设置为-0.7。 The problem I have is that, when I open a new worksheet, the cells interior are in "No fill" mode. 我的问题是,当我打开新工作表时,单元格内部处于“无填充”模式。 Therefore the TintAndShade property makes no difference. 因此,TintAndShade属性没有区别。

Then, I tried to check if the Interior Color is by default (ColorIndex = -4142), and if it is the case, I set the color to White (ColorIndex = 2). 然后,我尝试检查“内部颜色”是否默认为(ColorIndex = -4142),如果是这种情况,我将颜色设置为“白色”(ColorIndex = 2)。 The problem I have is that, it erases all the borders (I think the borders also are in "No Fill" mode). 我的问题是,它会擦除​​所有边框(我认为边框也处于“无填充”模式)。 I can't set them to black because it is not the default color. 我不能将它们设置为黑色,因为它不是默认颜色。

Do someone know how to change the Interior color without removing the default border color ? 有人知道如何在不删除默认边框颜色的情况下更改内部颜色吗? Or is there any other easier way to make such a button (that tints all the cells except for the named range) ? 还是有其他更简单的方法来制作这样的按钮(为命名范围以外的所有单元格着色)?

Thank you ! 谢谢 !

Here is my code if you want to check : 这是我的代码,如果您想检查:

int shadeMargin = 0;
Excel.Worksheet excelWorksheet = Globals.ThisAddIn.Application.ActiveSheet;
Excel.Names tags = Globals.ThisAddIn.Application.ActiveWorkbook.Names;

Excel.Range last = excelWorksheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
Excel.Range xlRange = excelWorksheet.get_Range("A1", last);

 for (int i = 1; i < xlRange.Rows.Count + 1 + shadeMargin; i++)
 {
     for (int j = 1; j < xlRange.Columns.Count + 1 + shadeMargin; j++)
     {
         if (xlRange.Cells[i, j].Interior.ColorIndex == -4142) // Correspond to the "No Fill" color
         {
              xlRange.Cells[i, j].Interior.ColorIndex = 2; // Replaced by WHITE color
         }
         xlRange.Cells[i, j].Interior.TintAndShade = -0.7;

      }
 }

 foreach (Excel.Name tag in tags)
 {
     Excel.Range tagRange = tag.RefersToRange;
     for (int i = 1; i < tagRange.Rows.Count + 1; i++)
     {
         for (int j = 1; j < tagRange.Columns.Count + 1; j++)
         {
             tagRange.Cells[i, j].Interior.TintAndShade = 0;
         }
      }
  }

The borders and shading are separate properties. 边框和阴影是单独的属性。 In the absence of a fill color (eg white) the gridlines are shown. 在没有填充颜色(例如白色)的情况下,显示了网格线。 You can mimic gridlines by setting the Borders to match, but it will not be perfect. 您可以通过将“边框”设置为“匹配”来模仿网格线,但这并不完美。 Use the macro recorder to get the codes for setting different borders linestyle, themecolor, tintandshade and weight, then experiment with setting them the way you want. 使用宏记录器获取用于设置不同边框线型,主题颜色,色调和阴影和权重的代码,然后尝试以所需方式设置它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM