Imports System.Runtime.InteropServices
Imports Word
Imports Excel
#Region " 测试excel的程序"
Dim doc As word.Document
Dim word As New word.Application
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If Me.OpenFileDialog1.ShowDialog() = DialogResult.OK Then
word.Visible = True
Try
doc = word.Documents.Open(Me.OpenFileDialog1.FileName)
doc.Activate()
Catch ex As COMException
MessageBox.Show("出错了,请查找原因!")
End Try
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If Not IsNumeric(TextBox1.Text) Or Len(TextBox3.Text) = 0 Or Len(TextBox3.Text) = 0 Then
MsgBox("输入不正确!")
Else
Try
Dim myExcel As Object
Dim myBook As Object
Dim mySheet As Object
Dim x As Integer
Dim y, z As String
z = Me.ComboBox3.Text
'创建一个新的excel电子表格。
myExcel = CreateObject("Excel.Application")
myBook = myExcel.Workbooks.Add
'向创建的文件里添加数据。
mySheet = myBook.Worksheets(Val(ComboBox2.Text))
x = Val(TextBox1.Text)
y = ComboBox1.Text
mySheet.Range(y & x).Value = TextBox3.Text
'保存创建的文件,并销毁对象。
Dim weizhi As String
weizhi = Me.TextBox4.Text
myBook.SaveAs(z & weizhi & ".xls")
mySheet = Nothing
myBook = Nothing
myExcel.Quit()
myExcel = Nothing
GC.Collect()
MsgBox("文件已经创建成功!")
Catch ex As Exception
MsgBox("程序发生异常,请检查代码。")
End Try
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.doc.Close()
Me.word.Visible = False
End Sub
#End Region