超胆侠第2季百度云:如何在VB里面实现导出ACCESS到EXCEL

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/10 12:46:22
如何在VB里面实现导出ACCESS到EXCEL,最好有详细的代码和注释,谢谢

我这里有个从ORACLE导出到EXCEL的例子,希望有帮助
Private Function ExportDetail(strFileName As String) As Boolean

Dim iRow As Long
Dim myExcel As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

On Error GoTo ErrAction
ExportDetail = False

Set myExcel = New Excel.Application
myExcel.Visible = False
myExcel.SheetsInNewWorkbook = 1
Set xlBook = myExcel.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)

xlSheet.Columns.ClearFormats
xlSheet.Cells(1, 1) = "物料BOM【" & TreeFile.SelectedItem.Text & "】"
xlSheet.Range("A1:T1").MergeCells = True

xlSheet.Cells(2, 1) = "标题项"
xlSheet.Cells(2, 2) = "中文名称"
xlSheet.Cells(2, 3) = "英文名称"
xlSheet.Cells(2, 4) = "文档号"
xlSheet.Cells(2, 5) = "物料号"
xlSheet.Cells(2, 6) = "数量"
xlSheet.Cells(2, 7) = "总数量"
xlSheet.Cells(2, 8) = "单位"
xlSheet.Cells(2, 9) = "物料关联文档1"
xlSheet.Cells(2, 10) = "物料关联文档2"
xlSheet.Cells(2, 11) = "所属装配文档号"
xlSheet.Cells(2, 12) = "所属装配物料号"
xlSheet.Cells(2, 13) = "装配物料关联文档1"
xlSheet.Cells(2, 14) = "装配物料关联文档2"
xlSheet.Cells(2, 15) = "物料技术参数"
xlSheet.Cells(2, 16) = "物料类型"
xlSheet.Cells(2, 17) = "物料备注"
xlSheet.Cells(2, 18) = "重量"
xlSheet.Cells(2, 19) = "备注"
xlSheet.Cells(2, 20) = "清单文档号"

For iRow = 1 To DocList.ListItems.Count
xlSheet.Cells(iRow + 2, 1) = DocList.ListItems(iRow).ListSubItems(1)
xlSheet.Cells(iRow + 2, 2) = DocList.ListItems(iRow).ListSubItems(2)
xlSheet.Cells(iRow + 2, 3) = DocList.ListItems(iRow).ListSubItems(3)
xlSheet.Cells(iRow + 2, 4) = DocList.ListItems(iRow).ListSubItems(4)
xlSheet.Cells(iRow + 2, 5) = DocList.ListItems(iRow).ListSubItems(5)
xlSheet.Cells(iRow + 2, 6) = DocList.ListItems(iRow).ListSubItems(6)
xlSheet.Cells(iRow + 2, 7) = DocList.ListItems(iRow).ListSubItems(7)
xlSheet.Cells(iRow + 2, 8) = DocList.ListItems(iRow).ListSubItems(8)
xlSheet.Cells(iRow + 2, 9) = DocList.ListItems(iRow).ListSubItems(9)
xlSheet.Cells(iRow + 2, 10) = DocList.ListItems(iRow).ListSubItems(10)
xlSheet.Cells(iRow + 2, 11) = DocList.ListItems(iRow).ListSubItems(11)
xlSheet.Cells(iRow + 2, 12) = DocList.ListItems(iRow).ListSubItems(12)
xlSheet.Cells(iRow + 2, 13) = DocList.ListItems(iRow).ListSubItems(13)
xlSheet.Cells(iRow + 2, 14) = DocList.ListItems(iRow).ListSubItems(14)
xlSheet.Cells(iRow + 2, 15) = DocList.ListItems(iRow).ListSubItems(15)
xlSheet.Cells(iRow + 2, 16) = DocList.ListItems(iRow).ListSubItems(16)
xlSheet.Cells(iRow + 2, 17) = DocList.ListItems(iRow).ListSubItems(17)
xlSheet.Cells(iRow + 2, 18) = DocList.ListItems(iRow).ListSubItems(18)
xlSheet.Cells(iRow + 2, 19) = DocList.ListItems(iRow).ListSubItems(19)
xlSheet.Cells(iRow + 2, 20) = DocList.ListItems(iRow).ListSubItems(20)
Next

xlSheet.Columns.AutoFit
xlSheet.Rows(2).Font.Bold = True
xlBook.SaveAs strFileName
myExcel.Quit
Set myExcel = Nothing

ExportDetail = True
Exit Function

ErrAction:
ExportDetail = False
MsgBox "导出失败:" & Err.Description, vbCritical, "导出到EXCEL"
End Function

list控件显示的数据好像不能直接导出到excel,你可以做一个查询,然后直接导出到Excel(好像是用‘另存为’),如果你要保存在已有的Excel文档,用复制粘贴好了,也挺方便的。

用vba把数据从access添加到excel