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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值