2011-08-08 229 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

A1、B1のような範囲ではなく、(1,1)、(5,6)などのようなインデックスに従う方法は? – 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"を含む行で2番目のエラーが発生し、 "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

関連する問題