2011-03-10 3 views

답변

2

단계 -

1 단계 : 엑셀 셀 Colors 배열을 할당한다.

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 단계 : 매크로 4 단계

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); 

2 단계에서 작성된 실행하십시오 : Values 배열을 Excel 셀에 할당하십시오.

yourRangeObject.Value = Values; 

두 가지로 샷을하면 컬러 카드로 엑셀 셀을 만들 수 있습니다.

0

이와 같은 대부분의 경우 셀 블록에 대해 복사 및 붙여 넣기 특수 형식을 사용하지만 작동하지 않는 색상을 동적으로 변경하거나 임의로 변경해야하는 경우가 있습니다.

+0

2 차원 배열 안에 배치 된 색상을 지정하여 한 번에 셀을 처리하는 것만으로도 충분합니다. 예 : - 다음 배열 사용 Excel 시트에 4 개의 셀 (2 행 및 2 열)을 표시하려고합니다. -> 셀 [,] {{ "Color1", "Color2"}, { "Color3,"Color4 "}} –

+0

Excel 개체 모델은 이것을 허용하지 않으므로 4 개의 셀에 개별적으로 색을 지정하는 것이 좋습니다 당신이 이것을 필요로 할 때 처음으로 4 셀을 클립 보드에 복사하고 4 셀의 모든 후속 세트에 PasteSpecial 포맷을 사용하십시오. –

관련 문제