mac无法复制文件到u盘:VB里DataGird控件中的数据导出到Excel文件详细的代码以及所用到的控件,部件

来源:百度文库 编辑:中科新闻网 时间:2024/05/05 21:47:56

用到一个公共对话框,一个datagrid控件
Private Sub ExportDetail(strFileName As String)
Dim iRow As Long
Dim myExcel As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

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) = "下料明细表【" & treeCatalog.SelectedItem.Text & "】"
xlSheet.Range("A1:Q1").MergeCells = True
xlSheet.Range("I2:Q2").MergeCells = True
xlSheet.Range("A2:A3").MergeCells = True
xlSheet.Range("E2:E3").MergeCells = True
xlSheet.Range("F2:F3").MergeCells = True
xlSheet.Range("G2:G3").MergeCells = True
xlSheet.Range("H2:H3").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) = "工艺流程"
xlSheet.Cells(2, 10) = "工艺流程"
xlSheet.Cells(2, 11) = "工艺流程"
xlSheet.Cells(2, 12) = "工艺流程"
xlSheet.Cells(2, 13) = "工艺流程"
xlSheet.Cells(2, 14) = "工艺流程"
xlSheet.Cells(2, 15) = "工艺流程"
xlSheet.Cells(2, 16) = "工艺流程"
xlSheet.Cells(2, 17) = "工艺流程"
xlSheet.Cells(3, 1) = "序 号"
xlSheet.Cells(3, 2) = "零件名称"
xlSheet.Cells(3, 3) = "属装序号"
xlSheet.Cells(3, 4) = "规格尺寸"
xlSheet.Cells(3, 5) = "数量"
xlSheet.Cells(3, 6) = "工艺分工"
xlSheet.Cells(3, 7) = "备注"
xlSheet.Cells(3, 8) = "分类名称"
xlSheet.Cells(3, 9) = "1"
xlSheet.Cells(3, 10) = "2"
xlSheet.Cells(3, 11) = "3"
xlSheet.Cells(3, 12) = "4"
xlSheet.Cells(3, 13) = "5"
xlSheet.Cells(3, 14) = "6"
xlSheet.Cells(3, 15) = "7"
xlSheet.Cells(3, 16) = "8"
xlSheet.Cells(3, 17) = "9"

For iRow = 2 To grdDetail.Rows - 1

If iRow Mod 2 = 0 Then
xlSheet.Cells(iRow + 2, 1) = "'" & grdDetail.TextMatrix(iRow, 0)
xlSheet.Range(xlSheet.Cells(iRow, 1), xlSheet.Cells(iRow + 1, 1)).MergeCells = True
End If
xlSheet.Cells(iRow + 2, 2) = "'" & grdDetail.TextMatrix(iRow, 1)
xlSheet.Cells(iRow + 2, 3) = "'" & grdDetail.TextMatrix(iRow, 2)
xlSheet.Cells(iRow + 2, 4) = "'" & grdDetail.TextMatrix(iRow, 3)
xlSheet.Cells(iRow + 2, 5) = grdDetail.TextMatrix(iRow, 4)
xlSheet.Cells(iRow + 2, 6) = "'" & grdDetail.TextMatrix(iRow, 5)
xlSheet.Cells(iRow + 2, 7) = "'" & grdDetail.TextMatrix(iRow, 6)
xlSheet.Cells(iRow + 2, 8) = "'" & grdDetail.TextMatrix(iRow, 7)
xlSheet.Cells(iRow + 2, 9) = "'" & grdDetail.TextMatrix(iRow, 8)
xlSheet.Cells(iRow + 2, 10) = "'" & grdDetail.TextMatrix(iRow, 9)
xlSheet.Cells(iRow + 2, 11) = "'" & grdDetail.TextMatrix(iRow, 10)
xlSheet.Cells(iRow + 2, 12) = "'" & grdDetail.TextMatrix(iRow, 11)
xlSheet.Cells(iRow + 2, 13) = "'" & grdDetail.TextMatrix(iRow, 12)
xlSheet.Cells(iRow + 2, 14) = "'" & grdDetail.TextMatrix(iRow, 13)
xlSheet.Cells(iRow + 2, 15) = "'" & grdDetail.TextMatrix(iRow, 14)
xlSheet.Cells(iRow + 2, 16) = "'" & grdDetail.TextMatrix(iRow, 15)
xlSheet.Cells(iRow + 2, 17) = "'" & grdDetail.TextMatrix(iRow, 16)

Next

xlSheet.Rows(1).HorizontalAlignment = 3
xlSheet.Rows(2).HorizontalAlignment = 3
xlSheet.Rows(3).HorizontalAlignment = 3
xlSheet.Columns(1).HorizontalAlignment = 3
xlSheet.Columns(3).HorizontalAlignment = 3
xlSheet.Columns(5).HorizontalAlignment = 3
xlSheet.Columns(6).HorizontalAlignment = 3
xlSheet.Columns.AutoFit

xlSheet.Rows(1).Font.Bold = True

xlBook.SaveAs strFileName
myExcel.Quit
Set myExcel = Nothing

End Sub

见这里
http://www.lihuasoft.net/article/show.php?id=129

需要的组件是ADODB
其他组件不需要,直接从数据库导出即可
没必要舍近求远去用DataGrid