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