Public Sub SaveExcel(sfile As String)
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Add
'xlBook.SaveAs sfile <---------先不要调用这一句.
Set xlSheet = xlBook.Worksheets(1)
xlSheet.Cells(1, 1) = "姓名"
xlSheet.Cells(1, 2) = "收信人地址"
xlSheet.Cells(1, 3) = "收信人邮编"
xlSheet.Cells(1, 4) = "寄信人地址"
xlSheet.Cells(1, 5) = "寄信人邮编"
Dim i As Integer
For i = 0 To frmmain.list.ListItems.Count - 1
xlSheet.Range("A" & Str(i + 2)).Value = frmmain.list.ListItems(i).SubItems(2)
xlSheet.Range("B" & Str(i + 2)).Value = frmmain.list.ListItems(i).SubItems(3)
xlSheet.Range("C" & Str(i + 2)).Value = frmmain.list.ListItems(i).SubItems(4)
xlSheet.Range("D" & Str(i + 2)).Value = frmmain.list.ListItems(i).SubItems(5)
xlSheet.Range("E" & Str(i + 2)).Value = frmmain.list.ListItems(i).SubItems(6)
Next i
xlBook.SaveAs sfile '<-------------在这里调用
xlApp.Application.Quit
Set xlApp = noting
Set xlBook = noting
Set xlSheet = noting
End Sub
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Add
'xlBook.SaveAs sfile <---------先不要调用这一句.
Set xlSheet = xlBook.Worksheets(1)
xlSheet.Cells(1, 1) = "姓名"
xlSheet.Cells(1, 2) = "收信人地址"
xlSheet.Cells(1, 3) = "收信人邮编"
xlSheet.Cells(1, 4) = "寄信人地址"
xlSheet.Cells(1, 5) = "寄信人邮编"
Dim i As Integer
For i = 0 To frmmain.list.ListItems.Count - 1
xlSheet.Range("A" & Str(i + 2)).Value = frmmain.list.ListItems(i).SubItems(2)
xlSheet.Range("B" & Str(i + 2)).Value = frmmain.list.ListItems(i).SubItems(3)
xlSheet.Range("C" & Str(i + 2)).Value = frmmain.list.ListItems(i).SubItems(4)
xlSheet.Range("D" & Str(i + 2)).Value = frmmain.list.ListItems(i).SubItems(5)
xlSheet.Range("E" & Str(i + 2)).Value = frmmain.list.ListItems(i).SubItems(6)
Next i
xlBook.SaveAs sfile '<-------------在这里调用
xlApp.Application.Quit
Set xlApp = noting
Set xlBook = noting
Set xlSheet = noting
End Sub
解决方案 »
- MBP图像压缩技术,下面有段VB程序,小弟实在看不懂是怎么压缩的。求各位大哥大姐帮帮忙
- VB高手进:生成exe文件时出现:未找到方法或数据成员
- VB 与 面向对象编程。。。。
- 如何在textbox里实现自动循环读取当前数值(在线急等)
- VB中使用VC++中写的回调函数出现约定错误。。。急急急急。。。。
- 急.请问这是甚么问题
- winsock断点续传问题!!
- 急!!!!!!
- 关于ACTIVEX对象!
- 血液样本已经交给大夫了...1小时后拿决定命运的血液报告单...这一小时我该干什么去呢??
- AddNew后是否可用Cancelupdate放弃加入记录?
- 如何获取MDI窗体内的所有子窗体,并关闭它们?
Next ixlBook.SaveAs sfile ' 这一句移到xlApp.Save 的上面
xlApp.Save xlBook
Set xlApp = noting
Set xlBook = noting
Set xlSheet = noting
xlApp.Save
就可以了
不必用
SaveAs
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = FalseIf Dir(sfile) = "" Then '<----
Set xlBook = xlApp.Workbooks.Add '<----
xlBook.SaveAs sfile '<----
Else '<----
Set xlBook = xlApp.Workbooks.Open(sfile)
End IfSet xlSheet = xlBook.Worksheets(1)
xlSheet.Cells(1, 1) = "姓名"
xlSheet.Cells(1, 2) = "收信人地址"
xlSheet.Cells(1, 3) = "收信人邮编"
xlSheet.Cells(1, 4) = "寄信人地址"
xlSheet.Cells(1, 5) = "寄信人邮编"
Dim i As Integer
For i = 0 To frmmain.list.ListItems.Count - 1
xlSheet.Range("A" & Str(i + 2)).Value = frmmain.list.ListItems(i).SubItems(2)
xlSheet.Range("B" & Str(i + 2)).Value = frmmain.list.ListItems(i).SubItems(3)
xlSheet.Range("C" & Str(i + 2)).Value = frmmain.list.ListItems(i).SubItems(4)
xlSheet.Range("D" & Str(i + 2)).Value = frmmain.list.ListItems(i).SubItems(5)
xlSheet.Range("E" & Str(i + 2)).Value = frmmain.list.ListItems(i).SubItems(6)
Next ixlBook.Save '<----Set xlApp = noting
Set xlBook = noting
Set xlSheet = noting
End Sub
Print #1, "姓名,收信人地址,收信人邮编,寄信人地址,寄信人邮编"
Print #1, "纪嫣然,雅湖小筑,123456,,100001"
Close #1用Excel打开Address.CSV文件,看看能不能满足你的要求。
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = FalseIf Dir(sfile) = "" Then '<----
Set xlBook = xlApp.Workbooks.Add '<----
xlBook.SaveAs sfile '<----
Else '<----
Set xlBook = xlApp.Workbooks.Open(sfile)
End IfSet xlSheet = xlBook.Worksheets(1)
xlSheet.Cells(1, 1) = "姓名"
xlSheet.Cells(1, 2) = "收信人地址"
xlSheet.Cells(1, 3) = "收信人邮编"
xlSheet.Cells(1, 4) = "寄信人地址"
xlSheet.Cells(1, 5) = "寄信人邮编"
Dim i As Integer
For i = 0 To frmmain.List.ListItems.Count - 1
xlSheet.Range("A" & Str(i + 2)).Value = frmmain.List.ListItems(i).SubItems(2)
xlSheet.Range("B" & Str(i + 2)).Value = frmmain.List.ListItems(i).SubItems(3)
xlSheet.Range("C" & Str(i + 2)).Value = frmmain.List.ListItems(i).SubItems(4)
xlSheet.Range("D" & Str(i + 2)).Value = frmmain.List.ListItems(i).SubItems(5)
xlSheet.Range("E" & Str(i + 2)).Value = frmmain.List.ListItems(i).SubItems(6)
Next ixlBook.Save '<----xlBook.Close
xlApp.Quit'Set xlApp = noting
'Set xlBook = noting
'Set xlSheet = noting
End Sub