Шаги: -
Шаг 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.