【问题标题】:Color Excel Cells in one shot using two dimensional array使用二维数组一次性为 Excel 单元格着色
【发布时间】:2011-03-14 22:26:30
【问题描述】:

有没有办法将二维颜色数组直接分配给 Excel 单元格?

我们可以一次性使用Range.Value=Values[,] 分配单元格值,但我无法一次性将Colors[,] 分配给这些单元格。任何形式的帮助将不胜感激。

【问题讨论】:

    标签: .net excel export-to-excel


    【解决方案1】:

    步骤:-

    第 1 步:将 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 单元格进行颜色编码。

    【讨论】:

      【解决方案2】:

      在大多数情况下,我对单元格块使用 Copy 和 PasteSpecial 格式,但如果您需要动态和任意更改颜色,则不起作用。

      【讨论】:

      • 我只需要将放置在二维数组中的颜色一次性分配给 excel 单元格。例如:- 使用以下数组我希望在我的 excel 表中为 4 个单元格(2 行和 2 列)着色。 --> 单元格[,]{ {"Color1", "Color2"}, {"Color3, "Color4"} }
      • Excel 对象模型不允许这样做,所以我建议您在第一次需要时分别为 4 个单元格分配颜色,然后将 4 个单元格复制到剪贴板并为所有单元格使用 PasteSpecial 格式随后的 4 个细胞组。
      猜你喜欢
      • 2013-09-01
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      相关资源
      最近更新 更多