ExcelにRGB指定で背景色を付けるサンプルです。
サンプル
例)ExcelにRGB指定で背景色を付ける
Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices
Imports System.Drawing
Protected Sub ExcelBackgroundColorSample()
'Excel関連オブジェクトの定義
Dim app As Excel.Application = Nothing
Dim book As Excel.Workbook = Nothing
Dim sheet As Excel.Worksheet = Nothing
Try
app = New Excel.Application()
app.Workbooks.Add()
book = app.Workbooks(1)
sheet = CType(book.Worksheets(1), Excel.Worksheet)
sheet.Name = "背景色サンプル"
'A1~C2の範囲の背景色を黄色にする
sheet.Range("A1").Value = "黄色"
sheet.Range("A1", "C2").Interior.Color = Color.FromArgb(255, 255, 0)
book.SaveAs("C:\背景色サンプル.xlsx")
Catch ex As Exception
Throw ex
Finally
'解放処理(これをやらないとプロセスが残ります)
Marshal.ReleaseComObject(sheet)
book.Close()
Marshal.ReleaseComObject(book)
app.Quit()
Marshal.ReleaseComObject(app)
End Try
End Sub
(実行結果)
備考
- 事前に「Microsoft Excel x.x Object Library」に参照設定が必要です。