是否有任何方法将颜色的二维数组直接分配给Excel单元格?颜色Excel单元格使用二维数组一次拍摄
在一次拍摄中,我们可以使用Range.Value=Values[,]
指定单元格值,但我无法一次性将Colors[,]
指定给这些单元格。任何形式的帮助将不胜感激。
是否有任何方法将颜色的二维数组直接分配给Excel单元格?颜色Excel单元格使用二维数组一次拍摄
在一次拍摄中,我们可以使用Range.Value=Values[,]
指定单元格值,但我无法一次性将Colors[,]
指定给这些单元格。任何形式的帮助将不胜感激。
步骤: -
第一步:指定Colors
阵列到Excel细胞。
yourRangeObject.Value = Colors;
步骤2:在一个System.String写宏的颜色的颜色选择的范围
private static string GetMacro(int lastCellRowNum, int lastCellColNum)
{
StringBuilder sb = new StringBuilder();
sb.Append("Sub FormatSheet()" + "\n");
sb.Append(" Range(Cells(1, 1), Cells(" + lastCellRowNum + ", " + lastCellColNum + ")).Select " + "\n");
sb.Append(" For Each c In Selection" + "\n");
sb.Append(" c.Interior.Color = HEXCOL2RGB(c.Value)" + "\n");
sb.Append(" c.Borders.Color = HEXCOL2RGB(\"#FFDEDDDD\")" + "\n");
sb.Append(" Next" + "\n");
sb.Append(" Selection.ClearContents" + "\n");
sb.Append("End Sub" + "\n");
sb.Append("Public Function HEXCOL2RGB(ByVal HexColor As String) As String" + "\n");
sb.Append(" Dim Red As String, Green As String, Blue As String " + "\n");
sb.Append(" HexColor = Replace(HexColor, \"#\", \"\")" + "\n");
sb.Append(" Red = Val(\"&H\" & Mid(HexColor, 1, 2))" + "\n");
sb.Append(" Green = Val(\"&H\" & Mid(HexColor, 3, 2))" + "\n");
sb.Append(" Blue = Val(\"&H\" & Mid(HexColor, 5, 2))" + "\n");
sb.Append(" HEXCOL2RGB = RGB(Red, Green, Blue)" + "\n");
sb.Append("End Function");
return sb.ToString();
}
步骤3:运行宏写入步骤2
Microsoft.Vbe.Interop.VBComponent module = null;
module = workbook.VBProject.VBComponents.Add(Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_StdModule);
module.CodeModule.AddFromString(GetMacro(lastCellRowNum, lastCellColNum));
workbook.Application.Run("FormatSheet", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
步骤4 :将Values
数组分配给Excel单元格。
yourRangeObject.Value = Values;
多数民众赞成在两个镜头,你可以颜色代码你的Excel细胞。
在大多数情况下,我使用复制和粘贴特殊格式的单元格块,但是如果您需要动态和任意改变不起作用的颜色。
我只需要指定放置在二维数组内的颜色来突出一个镜头中的单元格。 例如: - 使用下面的数组我希望在我的Excel表单中为4个单元格(2行和2列)着色。 - > Cells [,] {{“Color1”,“Color2”},{“Color3,”Color4“}} – 2011-03-10 12:00:48
Excel对象模型不允许这样做,所以我建议您将颜色分别分配给4个单元格第一次需要时,将4个单元格复制到剪贴板,并对所有后续4个单元格使用PasteSpecial格式。 – 2011-03-10 19:10:47