源程序:
 Dim i     As Integer
  Dim j     As Integer
  Dim xlApp As excel.Application
  Set xlApp = New excel.Application
  Dim xlBook As New excel.Workbook
  Dim xlSheet As New excel.Worksheet
  Set xlApp = CreateObject("Excel.Application")
  xlApp.Visible = True
  Set xlBook = xlApp.Workbooks.Add
  xlBook.Sheets(1).name = "医药考试考生成绩表"
  xlApp.ActiveWindow.Caption = "医药考试考生成绩表"
    On Error Resume Next
  Set xlBook = xlApp.Workbooks.Open("d:\medicine.xls")
  Set xlSheet = xlBook.Worksheets(1)
  For j = 0 To dbg1.Columns.Count - 1
  xlSheet.Cells(1, j + 1) = dbg1.Columns.Item(j).Caption
  Next j
  xlSheet.Cells(0, 1) = "i"
  adodc1.recordset.movefirst
  For i = 0 To Adodc1.Recordset.RecordCount - 1
  dbg1.Row = i
  For j = 0 To dbg1.Columns.Count - 1
  dbg1.Col = j
  'MsgBox   DataGrid1.Text
    xlSheet.Cells.EntireColumn.AutoFit
  xlBook.ActiveSheet.PageSetup.CenterHeader = " 医药考试考生成绩表"
  xlSheet.Cells.HorizontalAlignment = xlCenter
  xlSheet.Cells.VerticalAlignment = xlCenter
  xlSheet.PageSetup.CenterHeader = "&""楷体_GB2312,常规""医药考试考生成绩表"
  xlSheet.PageSetup.CenterFooter = "第&P页"
  xlSheet.PageSetup.HeaderMargin = 2 / 0.035
  xlSheet.PageSetup.PrintGridlines = True
  
        If IsNull(dbg1.Text) = False Then
  xlSheet.Cells(i + 2, j + 1) = dbg1.Text
  End If
  Next j
  Next i输出全部的数据没有问题,如果使用查询按钮,查询到某一个数据的时候:
比如数据库里面有100道题,你查询其中一道题,结果excel就输出
查询到的同样的100行都是同一道题