TimberLandChapelのTech Blog

TimberLandChapel が提供する技術Tips,思いつきメモ,雑感ブログです。
Office 互換リボン Release 1.0 をリリースしました。

購読

今期のアンケート

INETAJ

Microsoft

SQL Server

TLC.com

クリエイティブ・コモンズ

情報処理関係官公庁

【VB.NET】VB.NET から Excel の COM オブジェクトを制御する基本セット
シナリオと要件
VB.NET から Excel の COM オブジェクトを操作する場合の注意点
http://blogs.timberlandchapel.com/blogs/timberlandchapel/articles/1149.aspx
で紹介したように,VB.NET から Excel の COM オブジェクトを操作する際には,.NET のマネージコードから COM を操作するための一風変わった作法が必要になります。

今回は,Excel のブックに罫線を描画する方法を紹介します。

Excel ブックを開いて,罫線を描画する

前回の「Excel COM オブジェクトを正しく解放するためのコツ」で紹介した4つのコツを頭に入れて丁寧にコードを書きます。

また,Excel の罫線は次の6つのパーツの組み合わせとして作成することになります。

  • 上端線
  • 下端線
  • 右端線
  • 左端線
  • 内部水平線
  • 内部垂直線

罫線の描画に関しても,[Borders] コレクションと [Border] オブジェクトが関わるため,一旦 [Borders] コレクションの参照を作成する必要があります。

罫線を描画する
Dim targetRange As Excel.Range = Nothing
Dim targetBorders As Excel.Borders = Nothing
Dim targetRightBorder As Excel.Border = Nothing
Dim targetLeftBorder As Excel.Border = Nothing
Dim targetTopBorder As Excel.Border = Nothing
Dim targetBottomBorder As Excel.Border = Nothing
Dim targetVerticalBorder As Excel.Border = Nothing
Dim targetHorizontalBorder As Excel.Border = Nothing
 
Try
    targetRange = [worksheet].Range("A3:C5")
 
 
    targetBorders = targetRange.Borders
    targetRightBorder = targetBorders.Item(Excel.XlBordersIndex.xlEdgeRight)
    targetLeftBorder = targetBorders.Item(Excel.XlBordersIndex.xlEdgeLeft)
    targetTopBorder = targetBorders.Item(Excel.XlBordersIndex.xlEdgeTop)
    targetBottomBorder = targetBorders.Item(Excel.XlBordersIndex.xlEdgeBottom)
    targetVerticalBorder = targetBorders.Item(Excel.XlBordersIndex.xlInsideVertical)
    targetHorizontalBorder = targetBorders.Item(Excel.XlBordersIndex.xlInsideHorizontal)
 
    With targetRightBorder
        .LineStyle = Excel.XlLineStyle.xlContinuous
        .Weight = Excel.XlBorderWeight.xlThin
        .ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic
    End With
    With targetLeftBorder
        .LineStyle = Excel.XlLineStyle.xlContinuous
        .Weight = Excel.XlBorderWeight.xlThin
        .ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic
    End With
    With targetTopBorder
        .LineStyle = Excel.XlLineStyle.xlContinuous
        .Weight = Excel.XlBorderWeight.xlThin
        .ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic
    End With
    With targetBottomBorder
        .LineStyle = Excel.XlLineStyle.xlContinuous
        .Weight = Excel.XlBorderWeight.xlThin
        .ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic
    End With
    With targetVerticalBorder
        .LineStyle = Excel.XlLineStyle.xlContinuous
        .Weight = Excel.XlBorderWeight.xlHairline
        .ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic
    End With
    With targetHorizontalBorder
        .LineStyle = Excel.XlLineStyle.xlContinuous
        .Weight = Excel.XlBorderWeight.xlHairline
        .ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic
    End With
 
Finally
    If Not targetHorizontalBorder Is Nothing Then
        Marshal.ReleaseComObject(targetVerticalBorder)
    End If
    If Not targetVerticalBorder Is Nothing Then
        Marshal.ReleaseComObject(targetVerticalBorder)
    End If
    If Not targetBottomBorder Is Nothing Then
        Marshal.ReleaseComObject(targetBottomBorder)
    End If
    If Not targetTopBorder Is Nothing Then
        Marshal.ReleaseComObject(targetTopBorder)
    End If
    If Not targetLeftBorder Is Nothing Then
        Marshal.ReleaseComObject(targetLeftBorder)
    End If
    If Not targetRightBorder Is Nothing Then
        Marshal.ReleaseComObject(targetRightBorder)
    End If
    If Not targetBorders Is Nothing Then
        Marshal.ReleaseComObject(targetBorders)
    End If
    If Not targetRange Is Nothing Then
        Marshal.ReleaseComObject(targetRange)
    End If
End Try

コード中の [worksheet] 部分には,[worksheet] オブジェクトの参照を指定してください。

それぞれの [Border] オブジェクトに対して,[LineStyle],[Weight],[ColorIndex] の3つのプロパティを指定することで基本的な罫線を描画することができます。

好みの罫線を描画するためには Excel の [マクロの記録] 機能を利用して VBA コード内にプロパティの値を記録させるとよいでしょう。

罫線を消去する

罫線を描画する操作を行えば,当然罫線を消去する操作も行う必要が出てくるでしょう。

[LineStyle] プロパティに指定する [XlLineStyle] 列挙体に,値 [xlLineStyleNone] が存在しますが,これは罫線の指定をしないことにはなりません。

罫線の指定を解除するためには [Constants] 列挙体の 値 [xlNone] を指定します。

罫線を解除する
targetBorder.LineStyle = Excel.Constants.xlNone

これで罫線の指定を解除することができます。

written by TimberLandChapel
Published 2007年9月25日 22:20 投稿者 timberlandchapel

コメント

コメントはありません