VB.Net创建的office类

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

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

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强制返回为文本 --------------------------...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办法, Format 或者FormatDateTime 竟然结果和...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace My ‘全局错误处理,新的解决方案直接...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用的爽呀,这篇文章写与2011年,看来我以前没...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选中的单元格进行处理 Dim m As Range, t...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integ...