看懂以下源码之后可以了解:
1、如何创建EXCEL文件,指定路径、文件名。
2、将LISTVIEW内容添加到相应的EXCLE位置。
————————————————————————————————————
须引用excel 9.0 object lib
private sub output_excel()
On Error GoTo meerror
If ListView1.ListItems.Count < 1 Then
MsgBox "没有数据!", 64, "导出数据"
Exit Sub
End If
Dim i As Integer, j As Integer
Dim myexcel As New Excel.Application
Dim mybook As New Excel.Workbook
Dim mysheet As New Excel.Worksheet
Set mybook = myexcel.Workbooks.Add
Set mysheet = mybook.Worksheets.Add
mysheet.Cells(1, 1) = "第一行某某表"
For j = 1 To ListView1.ColumnHeaders.Count -1
mysheet.Cells(2, j) = ListView1.ColumnHeaders.Item(j + 1) '添加字段
Next
For i = 1 To ListView1.ListItems.Count
'For j = 1 To ListView1.ColumnHeaders.Count
For j = 1 To 7 '我只导出7列数据
mysheet.Cells(i + 2, j) = ListView1.ListItems(i).SubItems(j)
Next j
Next i
MsgBox "成功导出公司产值统计表.xls", 64, "导出文件"
myexcel.Visible = True
mybook.SaveAs ("" & App.Path & "\公司产值统计") '保存文件 并输出文件名为
mybook.Close
myexcel.quit
Exit Sub
meerror:
MsgBox "" & Err.Description & "", 64, "导出文件"
end sub
[此贴子已经被作者于2006-1-17 11:51:56编辑过] |