久久―日本道色综合久久,亚洲欧美精品在线,狼狼色丁香久久婷婷综合五月,香蕉人人超,日本网站黄,国产在线观看不卡免费高清,无遮挡的毛片免费

2023信創(chuàng)獨角獸企業(yè)100強
全世界各行各業(yè)聯(lián)合起來,internet一定要實現(xiàn)!

用VB控制EXCEL生成報表

2004-02-20 eNet&Ciweek

  下面給出一個實例:

  首先建立一個窗體(FORM1)在窗體中加入一個DATA控件和一按鈕,引用Microsoft Excel類型庫:從"工程"菜單中選擇"引用"欄;選擇Microsoft Excel 8.0 Object Library;選擇"確定"。

  在FORM的LOAD事件中加入:

Data1.DatabaseName = 數(shù)據(jù)庫名稱
Data1.RecordSource = 表名
Data1.Refresh
  
  在按鈕的CLICK事件中加入

  Dim Irow, Icol As Integer
  Dim Irowcount, Icolcount As Integer
  Dim Fieldlen() "存字段長度值
  Dim xlApp As Excel.Application
  Dim xlBook As Excel.Workbook
  Dim xlSheet As Excel.Worksheet  
  Set xlApp = CreateObject("Excel.Application")
  Set xlBook = xlApp.Workbooks.Add
  Set xlSheet = xlBook.Worksheets(1)

  With Data1.Recordset.MoveLast

   If .RecordCount < 1 Then
    MsgBox ("Error 沒有記錄!")
    Exit Sub
   End If

   Irowcount = .RecordCount "記錄總數(shù)
   Icolcount = .Fields.Count "字段總數(shù)

   ReDim Fieldlen(Icolcount).MoveFirst

   For Irow = 1 To Irowcount + 1
    For Icol = 1 To Icolcount
     Select Case Irow
      Case 1 "在Excel中的第一行加標題
     xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1).Name
      Case 2 "將數(shù)組FIELDLEN()存為第一條記錄的字段長

       If IsNull(.Fields(Icol - 1)) = True Then
        Fieldlen(Icol) = LenB(.Fields(Icol - 1).Name)
        "如果字段值為NULL,則將數(shù)組Filelen(Icol)的值設為標題名的寬度
       Else
        Fieldlen(Icol) = LenB(.Fields(Icol - 1))
       End If

       xlSheet.Columns(Icol).ColumnWidth = Fieldlen(Icol)
        "Excel列寬等于字段長
       xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1)
        "向Excel的CellS中寫入字段值
      Case Else
       Fieldlen1 = LenB(.Fields(Icol - 1))

       If Fieldlen(Icol) < Fieldlen1 Then
        xlSheet.Columns(Icol).ColumnWidth = Fieldlen1
        "表格列寬等于較長字段長
        Fieldlen(Icol) = Fieldlen1
        "數(shù)組Fieldlen(Icol)中存放最大字段長度值
       Else
        xlSheet.Columns(Icol).ColumnWidth = Fieldlen(Icol)
       End If

       xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1)
      End Select
     Next
     If Irow <> 1 Then
      If Not .EOF Then .MoveNext
     End If
    Next
   With xlSheet
    .Range(.Cells(1, 1), .Cells(1, Icol - 1)).Font.Name = "黑體"
     "設標題為黑體字
    .Range(.Cells(1, 1), .Cells(1, Icol - 1)).Font.Bold = True
     "標題字體加粗
    .Range(.Cells(1, 1), .Cells(Irow, Icol - 1)).Borders.LineStyle = xlContinuous
    "設表格邊框樣式
   End With
   xlApp.Visible = True "顯示表格
   xlBook.Save "保存
   Set xlApp = Nothing "交還控制給Excel
  End With


  本程序在中文Windows98、中文VB5下通過。

相關(guān)頻道: eNews

您對本文或本站有任何意見,請在下方提交,謝謝!

投稿信箱:tougao@enet16.com