VB程序员博客

VB程序开发

datagrid 中数据是绑定的,最好给个例子,谢谢

僅供參考:

Public Function TDGExportOld( _
                        ByVal objDBGrid As TrueOleDBGrid70.TDBGrid _
                            )
    Dim objExcel As Excel.Application
    Dim objWorkBook As Excel.Workbook
    Dim objSheet As Excel.Worksheet

   
    Dim a As Integer, b As Integer
    Dim rs As ADODB.Recordset, rsClone As ADODB.Recordset
    Dim intColumnsCount As Integer, AryDataField() As String
    Dim strBadInfo As String
    On Error GoTo ErrHandle:
    Dim I As Integer
   
    ShowWait "戈旧い,叫祔……"
    Set rs = objDBGrid.DataSource
    If rs Is Nothing Then GoTo NoRecord
    If rs.State = adStateClosed Then GoTo NoRecord
    If rs.RecordCount = 0 Then GoTo NoRecord
    Set rsClone = rs.Clone
    rsClone.filter = rs.filter
    intColumnsCount = objDBGrid.Columns.Count
    ReDim AryDataField(0 To intColumnsCount)

    Set objExcel = New Excel.Application
    Set objWorkBook = objExcel.Workbooks.Add
    Set objSheet = objWorkBook.Worksheets.Add
    a = 1
    With objSheet
        b = 1
        For I = 0 To intColumnsCount - 1
            If objDBGrid.Columns(I).Visible = True Then
                    .Cells(a, b) = objDBGrid.Columns(I).Caption
                    AryDataField(b) = objDBGrid.Columns(I).DataField
                    b = b + 1
              End If
        Next
        intColumnsCount = b - 1
        ReDim Preserve AryDataField(0 To intColumnsCount)
        a = a + 1
           
           
        While Not rsClone.EOF
            ShowWait "タ旧材" & rsClone.AbsolutePosition & "/" & rsClone.RecordCount & ",叫祔……"
            For b = 1 To intColumnsCount
                .Cells(a, b) = ReadStrFromRs(rsClone, AryDataField(b))
            Next b
              a = a + 1
              rsClone.MoveNext
        Wend
       
    End With
    objExcel.Visible = True
    GoTo ExitFunction
   
ErrHandle:
  ShowError
  GoTo ExitFunction
 
NoRecord:
    strBadInfo = "礚祇瞷旧戈"
    GoTo ExitFunction
   
ExitFunction:
    ShowWait ""
   
    If Not rsClone Is Nothing Then
      If rsClone.State = adStateOpen Then rsClone.Close
      Set rsClone = Nothing
    End If
    Set objSheet = Nothing
    Set objWorkBook = Nothing
    Set objExcel = Nothing
    If strBadInfo <> "" Then
        MsgBox strBadInfo, vbInformation, g_strTitle
    End If
End Function

赞同 一楼方法

Dim i As Integer, r As Integer, c As Integer
  Dim newxls As New Excel.Application
  Dim newbook As New Excel.Workbook
  Dim newsheet As New Excel.Worksheet
  Set newbook = newxls.Workbooks.Add  '创建工作簿
  Set newsheet = newbook.Worksheets(1) '创建工作表
  If Sql <> "" Then
    Adodc1.RecordSource = Sql
    Adodc1.Refresh
  End If
  If Adodc1.Recordset.RecordCount > 0 Then
    For i = 0 To DataGrid1.Columns.Count - 1
        newsheet.Cells(1, i + 1) = DataGrid1.Columns(i).Caption '指定表头名称
    Next i
    '指定表格内容
    Adodc1.Recordset.MoveFirst
    Do Until Adodc1.Recordset.EOF
        r = Adodc1.Recordset.AbsolutePosition
        For c = 0 To DataGrid1.Columns.Count - 1
            DataGrid1.Col = c
            newsheet.Cells(r + 1, c + 1) = DataGrid1.Columns(c)
        Next c
        Adodc1.Recordset.MoveNext
    Loop
     
    Dim myval As Long
    Dim mystr As String
    myval = MsgBox("是否保存该Excel表?", vbYesNo, "提示窗口")
    If myval = vbYes Then
      mystr = InputBox("请输入文件名称", "输入窗口")
       
      If Len(mystr) = 0 Then
        MsgBox "系统不允许文件名称为空!", , "提示窗口"
        Exit Sub
      End If
      On Error GoTo ErrSave
      newsheet.SaveAs App.Path  & mystr & ".xls"
      Adodc1.Recordset.MoveFirst
            MsgBox "Excel文件保存成功,位置:" & App.Path  & mystr & ".xls", , "提示窗口"

      newxls.Quit
ErrSave:
      Exit Sub
      MsgBox Err.Description, , "提示窗口"
     
      Else: Adodc1.Recordset.MoveFirst
    End If
 
  End If

怎么用到了Adodc1啊,请解释下

Adodc 和 DataGrid 绑定

不过我的datagrid 是通过一条SQL语句绑定的啊,没有通过Adodc绑定

引用 7 楼 thwen0101 的回复:
不过我的datagrid 是通过一条SQL语句绑定的啊,没有通过Adodc绑定


Adodc1.Recordset

全部改为

Rs (你的记录集就行了)

学习学习!

感谢 楼上各位兄弟,问题搞定,


标签: , , ,