⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄

📁 VB的文本资料,需要时有帮助
💻
字号:
引用
Public Function ExporToExcel(strOpen As String)
'*********************************************************
'* 名称:ExporToExcel
'* 功能:导出数据到EXCEL
'* 用法:ExporToExcel(sql查询字符串)
'*********************************************************
   On Error Resume Next
    Dim cn As New ADODB.Connection
    Dim Rs_Data As New ADODB.Recordset
    Dim Irowcount As Integer
    Dim Icolcount As Integer

    Dim xlApp As New Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    Dim xlQuery As Excel.QueryTable
    
    Dim line As Integer, M As Integer, n As Integer
    
    Dim savepath As String  '定義保存路徑
    
    CommonDialog1.CancelError = True   '設置cancelError為ture
    
'    On Error GoTo errhandler
    CommonDialog1.Flags = cdlOFNHideReadOnly
    
    
    CommonDialog1.FileName = "Report"
    
    CommonDialog1.DefaultExt = ".xls"
    
    CommonDialog1.Filter = "Excel(*.xls)|*.xls|Text(*.txt)|*.txt"
    
    CommonDialog1.FilterIndex = 1
    
    CommonDialog1.Flags = &H2
    
    CommonDialog1.ShowSave
    
    If Err.Number = cdlCancel Then
        MsgBox "按取消按扭将取消本次操作!", vbInformation + vbOKOnly, "提示"
        Exit Function

    End If
    
    savepath = CommonDialog1.FileName
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source ='" + App.Path & "\info.mdb" + "'  ;Persist Security Info=False"
    With Rs_Data
        If .State = adStateOpen Then
            .Close
        End If
        .ActiveConnection = cn
        .CursorLocation = adUseClient
        .CursorType = adOpenStatic
        .LockType = adLockReadOnly
        .Source = strOpen
        .Open
    End With
  '  Rs_Data.Open strOpen, Cn, adOpenStatic, adLockReadOnly
    With Rs_Data
      '  .MoveFirst
        If .RecordCount < 1 Then
            MsgBox ("没有记录!")
            Exit Function
        End If
        '记录总数
        Irowcount = .RecordCount
        '字段总数
        Icolcount = .Fields.Count
    End With
    Set xlApp = CreateObject("Excel.Application")
    Set xlBook = Nothing
    Set xlSheet = Nothing
    Set xlBook = xlApp.Workbooks().Add
    Set xlSheet = xlBook.Worksheets("sheet1")
    xlApp.Visible = False
    '添加查询语句,导入EXCEL数据
    Set xlQuery = xlSheet.QueryTables.Add(Rs_Data, xlSheet.Range("a1"))

    With xlQuery
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = True
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
    End With

    xlQuery.FieldNames = True '显示字段名
    xlQuery.Refresh

    With xlSheet
        .Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Name = "黑体"
        '设标题为黑体字
        .Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Bold = True
        '标题字体加粗
        .Range(.Cells(1, 1), .Cells(Irowcount + 1, Icolcount)).Borders.LineStyle = xlContinuous
        '设表格边框样式
    End With
    ActiveWorkbook.SaveAs FileName:=savepath, FileFormat:=xlNormal, _
    PassWord:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
    CreateBackup:=False
    ActiveWorkbook.Saved = True   '保存到Excel
   ' MsgBox "保存成功!", vbOKOnly, "信息"
    '結束EXcel進程
    xlApp.Quit  '
    Set xlSheet = Nothing
    Set xlBook = Nothing
    Set xlApp = Nothing
End Function


'將數據導出 mdb﹐成為Excel文件
Private Sub Command3_Click()
 conn.Execute "select * into [Excel 8.0;database=e:\test\test4.xls].[test4] from Phonebook"
 Unload Me
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -