VB.Net创建的office类

前端之家收集整理的这篇文章主要介绍了VB.Net创建的office类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Public Class Coffice
Public xls As Excel.Application
Public xlsworkbook As Excel.Workbook
Public xlsworksheet As Excel.Worksheet

Public doc As Word.Application
Public document As Word.Document
'************************************************************************************************************
'创建工作簿,获得第一个工作表,为需要的操作做准备

Public Function CreateXls(ByVal bool As Boolean) As Boolean
Try
xls = New Excel.Application
xls.Visible = bool
xls = CreateObject("excel.application")
xlsworkbook = xls.Application.Workbooks.Add()
xlsworksheet = xlsworkbook.Sheets(1)
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
'打开现有的工作簿,获得第一个工作表,为需要的操作做准备
Public Function OpenXls(ByVal path As String,ByVal name As String,ByVal bool As Boolean) As Boolean
Try
xls = New Excel.Application
xls.Visible = bool
xls = CreateObject("excel.application")
xlsworkbook = xls.Application.Workbooks.Open(path & name)
xlsworksheet = xlsworkbook.Sheets(1)
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
'保存原有的工作簿
Public Function SaveWorkbook() As Boolean
Try
'xlsworksheet.Save()
xlsworkbook.Save()
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
'保存新创建的工作簿
Public Function SaveWorkbook(ByVal path As String,ByVal name As String) As Boolean
Try
xlsworkbook.SaveAs(path & name)
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
'关闭工作簿
Public Function CloseWorkbook() As Boolean
Try
xlsworkbook.Close()
xls.Quit()
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
'获得工作簿行数,不能有空行
Public Function GetRows(ByVal column As Integer,ByVal sheet As Excel.Worksheet) As Integer
Dim row As Integer
Try
For row = 1 To 65536
If sheet.Cells(row,column).value <> "" Then
GetRows = row
Else
Exit For
End If
Next
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
'获得工作簿的列数,不能有空列
Public Function GetColumns(ByVal row As Integer,ByVal sheet As Excel.Worksheet) As Integer
Dim Column As Integer
Try
For Column = 1 To 255
If sheet.Cells(row,Column).value <> "" Then
GetColumns = Column
Else
Exit For
End If
Next
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
'释放excel资源
Public Function DisposExcel() As Boolean
Try
xlsworksheet = Nothing
xlsworkbook = Nothing
xls = Nothing
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
'************************************************************************************************************
'创建一个word文档

Public Function CreateDoc(ByVal bool As Boolean) As Boolean
Try
doc = New Word.Application
doc.Visible = bool
doc = CreateObject("word.application")
document = doc.Documents.Add()
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
'打开一个word文档
Public Function OpenDoc(ByVal path As String,ByVal bool As Boolean) As Boolean
Try
doc = New Word.Application
doc.Visible = bool
doc = CreateObject("word.application")
document = doc.Documents.Open(path & name)
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
'保存打开的文档
Public Function SaveDocument() As Boolean
Try
document.Save()
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
'保存新创建的文档
Public Function SaveDocument(ByVal path As String,ByVal name As String) As Boolean
Try
document.SaveAs(path & name)
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
'关闭文档
Public Function CloseDocument() As Boolean
Try
document.Close()
doc.Quit()
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
End Function
'释放word资源 Public Function DisposWord() As Boolean Try doc = Nothing document = Nothing Catch ex As Exception MsgBox(ex.Message.ToString()) End Try End FunctionEnd Class

猜你在找的VB相关文章