华 英雄

重剑无锋 为网络贡献更多实用的文章资料

VB操作Excel大全

--------简单的读取-------------------
    Public xlApp As Excel.Application
    Public xlBook As Excel.Workbook
    Public xlSheet As Excel.Worksheet

Public Sub OpenExcel()
    Set xlApp = CreateObject("Excel.Application")
    Set xlBook = xlApp.Workbooks.Open("C:/Calender.xls")
    Set xlSheet = xlBook.Worksheets(1)
End Sub

Public Sub OpenExcel()
ReadedString =xlSheet.Cells(x, y)
End Sub

Public Sub CloseExcel()
    xlBook.Close (False)
    xlApp.Application.Quit
    Set xlApp = Nothing
End Sub

--------以下为导出(包括各种判断)------------------
g_ExcelSavePath ' 保存路径
g_ExcelFileName '文件名 eg:xx.csv xxx.xls

"判断是否存在,覆盖?
If Dir(g_ExcelSavePath & g_ExcelFileName) <> "" Then
    If MsgBox("文件:" & "  " & g_ExcelSavePath & g_ExcelFileName & "  " & "既在。覆盖?", vbQuestion + vbOKCancel + vbDefaultButton2, "XXXシステム") = vbCancel Then
        Exit Sub
    Else
        Kill (g_ExcelSavePath & g_ExcelFileName)
    End If
End If

"判断是否打开
If ExcelIsOpen(g_ExcelFileName) = True Then                 'Excel is Open
    modGlobal.MsgBoxShow.ShowReturnFalse "MakerPrint", "6009"
    Exit Sub
End If

'定义
    Dim CurrentApp          As Excel.Application
    Dim CurrentBook         As Excel.Workbook
    Dim CurrentSheet        As Excel.Worksheet
    Dim CurrentQuery        As Excel.QueryTable

    strExcelFielName = g_ExcelSavePath & g_ExcelFileName
   
    If CurrentApp Is Nothing Then
        Set CurrentApp = CreateObject("Excel.Application")
    End If
   
   
    If Dir(strExcelFielName) = "" Then
        Set CurrentBook = CurrentApp.Workbooks.Add
        CurrentBook.SaveAs strExcelFielName
    Else
        Kill (strExcelFielName)
        Set CurrentBook = CurrentApp.Workbooks.Add
        CurrentBook.SaveAs strExcelFielName
    End If
   
    CurrentApp.Visible = False
    CurrentApp.DisplayAlerts = False '保存,退出时候不弹出警告

    CurrentSheet.Name = strMakerName

    CurrentBook.Close True
'    CurrentBook.Save
    CurrentApp.DisplayAlerts = True
    CurrentApp.Quit
   
    Set CurrentQuery = Nothing
    Set CurrentSheet = Nothing
    Set CurrentBook = Nothing
    Set CurrentApp = Nothing

阅读更多
文章标签: excel vb kill c
个人分类: VB
上一篇[推荐]设计模式
下一篇vb对XML的读取
想对作者说点什么? 我来说一句

VB操作EXCEL大全,已经过测试

2011年12月16日 3KB 下载

VB操作EXCEL技巧

2008年03月05日 21KB 下载

没有更多推荐了,返回首页

关闭
关闭