Visual Basic .NET でExcelを操作するサンプル

(VB.NET 2005 Express Beta2/Excelへの参照設定は必要)


Imports Excel

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows フォーム デザイナで生成されたコード "

Public Sub New()
MyBase.New()

' この呼び出しは Windows フォーム デザイナで必要です。
InitializeComponent()

' InitializeComponent() 呼び出しの後に初期化を追加します。

End Sub

' Form は dispose をオーバーライドしてコンポーネント一覧を消去します。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

' Windows フォーム デザイナで必要です。
Private components As System.ComponentModel.IContainer

' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
' Windows フォーム デザイナを使って変更してください。
' コード エディタは使用しないでください。
Friend WithEvents chkVisible As System.Windows.Forms.CheckBox
Friend WithEvents btnUse As System.Windows.Forms.Button
Friend WithEvents btnClose As System.Windows.Forms.Button
Friend WithEvents btnQuit As System.Windows.Forms.Button
Private Sub InitializeComponent()
Me.chkVisible = New System.Windows.Forms.CheckBox()
Me.btnUse = New System.Windows.Forms.Button()
Me.btnClose = New System.Windows.Forms.Button()
Me.btnQuit = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'chkVisible
'
Me.chkVisible.Location = New System.Drawing.Point(24, 32)
Me.chkVisible.Name = "chkVisible"
Me.chkVisible.TabIndex = 0
Me.chkVisible.Text = "Visible"
'
'btnUse
'
Me.btnUse.Location = New System.Drawing.Point(24, 64)
Me.btnUse.Name = "btnUse"
Me.btnUse.TabIndex = 1
Me.btnUse.Text = "Use"
'
'btnClose
'
Me.btnClose.Location = New System.Drawing.Point(24, 104)
Me.btnClose.Name = "btnClose"
Me.btnClose.TabIndex = 2
Me.btnClose.Text = "Close"
'
'btnQuit
'
Me.btnQuit.Location = New System.Drawing.Point(24, 144)
Me.btnQuit.Name = "btnQuit"
Me.btnQuit.TabIndex = 3
Me.btnQuit.Text = "Quit"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
Me.ClientSize = New System.Drawing.Size(136, 197)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnQuit, Me.btnClose, Me.btnUse, Me.chkVisible})
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private mexlApplication As New Application()

Private mblnVisible As Boolean = False

Private Sub chkVisible_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkVisible.CheckedChanged
If chkVisible.Checked = True Then
mblnVisible = True
Else
mblnVisible = False
End If
End Sub

Private Sub btnUse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUse.Click

Dim exlWorkbooks As Workbooks
Dim exlWorkbook As Workbook

Dim exlSheets As Sheets
Dim exlSheet As Worksheet

If mblnVisible = True Then
mexlApplication.Visible = True
Else
mexlApplication.Visible = False
End If

exlWorkbooks = mexlApplication.Workbooks
exlWorkbook = exlWorkbooks.Add(XlWBATemplate.xlWBATWorksheet)

exlSheets = exlWorkbook.Worksheets
exlSheet = exlSheets(1)
With exlSheet
.Cells(1, 2) = "書き込"
.Cells(1, 3) = "テスト"
.Cells(2, 3) = "2005.6.26"

.Range("B1").Font.Bold = True
End With

exlWorkbook.SaveAs("C:\050625.xls")



End Sub

Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click

mexlApplication.Quit()

End Sub

Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click

Me.Close()

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class