VB6.0 excel 導入和導出


 在工程中引用Microsoft Excel類型庫

因為office 版本的不同,在代碼寫完之后,去掉引用 Microsoft Excel 9.0 Object Library(EXCEL2000

調用 excel 對象之前先創建

    比如:

   Dim xlApp As Object
    Set xlApp = CreateObject("Excel.Application")

 

這樣就可以避免因為版本的不同,出現問題了

---------------------------------------

------數據庫導出EXCEL-------------

   On Error GoTo handles

      conn.ConnectionString = sqlconn '使用連接
       conn.CursorLocation = adUseClient
       conn.Open
       Set rst = conn.Execute(sqlstr)

     
'    Dim xlApp As Excel.Application
'
'    Dim xlbook As Excel.Workbook
'
'    Dim xlsheet As Excel.Worksheet
    Dim xlApp As Object
    Dim xlbook As Object
    Dim xlsheet As Object
   
   
    Set xlApp = CreateObject("Excel.Application")
    Set xlbook = xlApp.Workbooks.Add 'Excel文件路徑及文件名
    Set xlsheet = xlbook.Worksheets(1)

      If rst.RecordCount > 1 Then
       
        '獲取字段名
        For i = 1 To rs.Fields.Count
       
          xlsheet.Cells(1, i) = rst.Fields(i - 1).Name
       
        Next i
       
        rst.MoveFirst '指針移動到第一條記錄
        xlsheet.Range("A2").CopyFromRecordset rst '復制全部數據
       
        '釋放結果集,命令對象 和連接對象
        Set rst = Nothing
        Set comm = Nothing
        Set conn = Nothing
       
       xlApp.DisplayAlerts = False
       xlApp.Save
       xlApp.Quit   '關閉Excel
       MsgBox "數據導出完畢!", vbInformation, "金蝶提示"
     
      End If
     
     

    Exit Sub
     
handles:

     If Err.Number = 1004 Then
         xlApp.Quit   '關閉Excel
        Exit Sub
    Else
       If Err.Number <> 32577 Then
               MsgBox "ErrCode:" & Err & " ErrDescription:" & Err.Description
       End If
       Exit Sub

    End If

----------------------------------------

 

 

''' Excel表格導出功能
Private Sub Command2_Click()

   On Error GoTo handles
  
    Dim xlApp As Object
    Set xlApp = CreateObject("Excel.Application")
    Set exlBook = xlApp.Workbooks.Add 'Excel文件路徑及文件名
   
   
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer

        With VSFlexGrid1

            For i = 0 To .Rows - 1  '共有多少行
              j = 0
               For j = 0 To .Cols - 1 '共有多少列

                      xlApp.Sheets(1).Cells(i + 1, j + 1) = .TextMatrix(i, j)
                
              Next j
            Next i

        End With
       
       

    xlApp.DisplayAlerts = False
    'exlBook.Close True  '先保存修改再關閉工作簿
    xlApp.Save
    exlBook.Close True
    xlApp.Quit   '關閉Excel
    Exit Sub
   
handles:

     If Err.Number = 1004 Then
         xlApp.Quit   '關閉Excel
        Exit Sub
    Else
       If Err.Number <> 32577 Then
               MsgBox "ErrCode:" & Err & " ErrDescription:" & Err.Description
       End If
       Exit Sub
      
    End If

End Sub

'''EXCEL表格 導入功能

Private Sub Command3_Click()
'On Error Resume Next
 Dim fileadd As String

 CommonDialog1.Filter = "xls文件(*.xls)|*.xls" '選擇你要的文件
 CommonDialog1.ShowOpen
 fileadd = CommonDialog1.FileName

 If fileadd <> "" Then '判斷是否選擇文件
    
    Dim xlApp1 As Object
    Dim xlSheet1 As Object
   
    Set xlApp1 = CreateObject("Excel.Application") '創建excel程序
    Set xlBook1 = xlApp1.Workbooks.Open(fileadd) '打開存在的Excel表格
    Set xlSheet1 = xlBook1.Worksheets(1) '設置活動工作表

    Dim lastCol As Integer
    Dim lastRow As Integer
   
    lastCol = xlSheet1.UsedRange.Columns.Count 'excel 表格列數
    lastRow = xlSheet1.UsedRange.Rows.Count 'Excel 表格行數

    '根據 EXCEL 表格中的行列數 確定 vsflexgrid 表的行列數
    VSFlexGrid1.Cols = lastCol + 1
    VSFlexGrid1.Rows = lastRow + 1


    For i = 0 To lastRow - 1

        For j = 1 To lastCol

             VSFlexGrid1.Cell(flexcpText, i, j) = xlSheet1.Cells(i + 1, j).Value

        Next j

    Next i

    VSFlexGrid1.Refresh
    MsgBox "數據導入完畢", vbInformation, "提示"
   
 Else
 
    MsgBox "請選擇文件", vbExclamation, "提示"

 End If
     VSFlexGrid1.Redraw = False '關閉表格重畫,加快運行速度
 


End Sub


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM