2011-08-08 5 views
5

Visual Basic.net을 통해 데이터베이스의 일부 데이터를 Excel에서 작성하고 있습니다. 일부 셀의 배경을 변경하고 텍스트를 굵게 만들어야합니다. 나는 그런 것을 필요로한다 :VB.NET을 통해 Excel 시트의 셀 색상 변경

xlWorkSheet.Cells(rownumber, 1).BackgroundColor = Color.Yellow 
xlWorkSheet.Cells(rownumber, 1).Font.isBold = True 

물론 위의 것들은 아무 것도 아니다. 나는 이것을 어떻게 할 수 있는가? 감사합니다 ..

답변

9

Excel.Style 개체를 만들어 범위에 적용해야합니다. 이와 같이 :

Dim style As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("NewStyle") 
style.Font.Bold = True 
style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow) 

xlWorkSheet.Cells(rownumber, 1).Style = "NewStyle" 
+0

우리는 (1,1), (5,6) 등의 색인에 따라 어떻게 할 수 있습니까? A1, B1과 같은 범위가 아닙니까? – dnur

+0

@dnur : xlWorkSheet.Cells (1,1)은 Range 유형이므로 * Controls.AddNamedRange (xlWorkSheet.Cells (rownumber, 1), "rangeStyles") *가 작동해야합니다. –

+0

"Globals.ThisWorkbook"이 포함 된 줄에 오류가 나타납니다. 내 통합 문서 이름을 "xlWorkBook"및 "Globals.xlWorkBook"으로 변경했지만 그 중 아무 것도 작동하지 않았습니다. 또한 "Microsoft.Office.Tools.Excel.NamedRange"가 포함 된 줄에서 두 번째 오류가 발생하고이를 "Microsoft.Office.Interop.Excel.ShapeRange"로 변경했습니다. 그것은 사실입니까? – dnur

4

이것은 나를 위해 완벽하게 작동했습니다.

xlsWorkSheet.Cells (행, 열) .interior.color = Color.Green

+0

아주 간단합니다! –

1

스타일로 당신을 도울 수있는 몇 선언 그게 전부 컬러 팔레트를 들어 엑셀
: http://dmcritchie.mvps.org/excel/colors.htm

   Dim xlsCell As Excel.Range 
        xlsCell = xlWorkSheet.Range("A1") 
        xlsCell.Range("A5").Value = "TEXT" 

        With xlsCell.Range("A12:J12") 
         .Merge() 
         .Borders(XlBordersIndex.xlEdgeBottom).Weight = 2 
         .Borders(XlBordersIndex.xlEdgeTop).Weight = 2 
         .Borders(XlBordersIndex.xlEdgeLeft).Weight = 2 
         .Borders(XlBordersIndex.xlEdgeRight).Weight = 2 
         .Borders(XlBordersIndex.xlInsideHorizontal).Weight = 2 
         .Borders(XlBordersIndex.xlInsideVertical).Weight = 2 
         .Interior.ColorIndex = 15       
         .WrapText = True 
         .Font.FontStyle = "Arial" 
         .VerticalAlignment = Excel.XlHAlign.xlHAlignCenter 
         .HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft 
        End With 
0
void SetCaptionStyle(ExcelStyle style) 
    { 
     style.Fill.PatternType = ExcelFillStyle.Solid; 
     style.Fill.BackgroundColor.SetColor(Color.FromArgb(184, 204, 228)); 

    } 
+1

VB.NET에서이 C# 함수를 가져오고 호출하는 방법을 설명해 주시겠습니까? – Tino

+0

이 언어는 새로운 기능입니다. 하지만 도움이되는 URL을 찾았습니다. 희망이 당신을 도울 것입니다. 1) http://vb.net-informations.com/excel-2007/vb.net_excel_page_format.htm 2) https://www.gemboxsoftware.com/spreadsheet/examples/c-sharp-vb-net-excel 스타일 서식/202 – Jagdeep