源程序:
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行都是同一道题
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行都是同一道题
解决方案 »
- 实时错误"-2147217900(80040e14)"From子句语法错误(急!!!)
- 为什么不能给变量赋初值呢?
- 关于ADO记录移动问题,超简单,因此我是菜鸟!!!!!!!!!!!!!
- 在允许多行显示的texT控件内,如何让文本换行显示??
- 容器有一个别名吗?像当前窗体可以用ME一样?
- 关于DDE的控件制作的问题
- 求教:能不能在listview上显示ico图片?怎样实现?
- 一个奇怪的问题(关于程序运行速度),请高手指点!!!
- 急急急急急急急急急急急急急急急急!!!!!!!!
- flytiger1977(武松!你敢来?^_^)进来领分
- 我的一个 闹钟的 shell程序怎么运行不了的?
- 类型不匹配问题?
你监控一下Adodc1.Recordset.RecordCount 看看返回的数据是否正确
xlSheet.Cells(i + 2, j + 1) = dbg1.Text
End If这句有问题吧。
'改为 For i = 0 To dbg1.ApproxCount- 1