发布于 2010-03-14 00:53:11
2楼
谢谢斑竹,版竹的意思是要我换一种数据显示方式吧,从SQL里查数据,我只会用LISTVIEW显示。
我结合网上很多意见,把LISTVIEW显示的数据导到EXCEL里,然后进行打印,不知哪里有问题,只打印出了目录,没有数据。
下面是我的程序,请大家给指点一下:
On Error Resume Next
Dim irow, icol
Dim xlApp 'As Excel.Application
Dim xlBook 'As Excel.Workbook
Dim xlSheet 'As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Open ("d:\CANSHU.xls")
Set xlSheet = xlBook.Worksheets(1)
Dim ListView1
Set ListView1=ScreenItems("ListView1")
With ListView1
If MsgBox("您真的要将资料导出到EXCEL中吗?", vbExclamation + vbYesNo, "警告") = vbYes Then
If .ListItems.Count > 0 Then
MsgBox "&ListView1.ListItems.Count&"
xlSheet.Cells(1, 2) = "温湿度数据Excel报表"
xlApp.Range("A1:F1").MergeCells = True
xlApp.Range("A1:F1").HorizontalAlignment = xlCenter
xlSheet.Columns(1).ColumnWidth = 18
xlSheet.Cells(2, 1) = "时间"
xlSheet.Cells(2, 2) = "标签1"
xlSheet.Cells(2, 3) = "标签2"
xlSheet.Cells(2, 4) = "标签3"
xlSheet.Cells(2, 5) = "标签4"
xlSheet.Cells(2, 6) = "标签5"
For Each oItem In ListView1.ListItems
irow = irow + 1
xlSheet.Cells(irow + 2, 1).Value = oItem
xlSheet.Cells(irow + 2, 2).Value = oItem.SubItems(1)
xlSheet.Cells(irow + 2, 3).Value = oItem.SubItems(2)
xlSheet.Cells(irow + 2, 4).Value = oItem.SubItems(3)
xlSheet.Cells(irow + 2, 5).Value = oItem.SubItems(4)
xlSheet.Cells(irow + 2, 6).Value = oItem.SubItems(5)
Next
xlApp.Range("A2:F2").Columns.Interior.ColorIndex = 40
xlApp.Range("A2:F2").Borders.LineStyle = xlContinuous
xlApp.Visible = True
xlApp.Range(xlSheet.Cells(2 + .ListItems.Count + 1, 1), xlSheet.Cells(2 + .ListItems.Count + 1, 4)).Columns.Interior.ColorIndex = 40
xlApp.Range(xlSheet.Cells(2 + .ListItems.Count + 1, 1), xlSheet.Cells(2 + .ListItems.Count + 1, 4)).Borders.LineStyle = xlContinuous
Else
MsgBox "*******!", vbExclamation + vbOKOnly, "警告"
End If
Else
End If
xlBook.Save
xlSheet.PrintOut
xlApp.QUIT
Set xlApp = Nothing '交还控制给Excel
Set xlBook = Nothing
Set xlSheet = Nothing
End With