现在已经将要查询的内容显示到datagrid中,现在想把已经查询好的在datagrid中的内容导入excel中,请问这部分的代码该如何写????

解决方案 »

  1.   

    引用Excel对象!open开一个Xls文件,写入,Close就行。
    csdn上很多,找找
      

  2.   

    CdlZPCP.Filter = "Excel(*.xls)|*.xls"
                CdlZPCP.ShowSave
                If CdlZPCP.FileName = "" Then Exit Sub
                FileName = CdlZPCP.FileName
                Set xlApp = CreateObject("Excel.Application")
                Set xlBook = xlApp.Workbooks.Add
                Set xlSheet = xlApp.Worksheets(1)
                With msgZPCPIn
                ReDim DBArray(.Rows - 1, .Cols - 1)
                        frmPercent.iPerc = .Rows
                        frmPercent.Show
                        xlSheet.Cells(2, 1).Value = Label2(0).Caption
                        xlSheet.Cells(2, 2).Value = "'" & txtZPCP(0).Text
                        xlSheet.Cells(2, 2).Select
                        For intCount = 1 To 5
                            xlSheet.Cells(intCount + 2, 1) = Label2(intCount).Caption
                            xlSheet.Cells(intCount + 2, 2) = "'" & txtZPCP(intCount).Text
                        Next intCount
                        xlSheet.Range(xlSheet.Cells(2, 2), xlSheet.Cells(7, 2)).Select
                        UnderLine
                        For intCount = 6 To 10
                            xlSheet.Cells(intCount - 3, 3) = Label2(intCount).Caption
                            xlSheet.Cells(intCount - 3, 4) = "'" & txtZPCP(intCount).Text
                        Next intCount
                        xlSheet.Range(xlSheet.Cells(3, 4), xlSheet.Cells(7, 4)).Select
                        UnderLine
                        For intCount = 11 To 15
                            xlSheet.Cells(intCount - 8, 5) = Label2(intCount).Caption
                            xlSheet.Cells(intCount - 8, 6) = "'" & txtZPCP(intCount).Text
                            xlSheet.Cells(intCount - 8, 6).Select
                        Next intCount
                        xlSheet.Range(xlSheet.Cells(3, 6), xlSheet.Cells(7, 6)).Select
                        UnderLine
                        For iRow = 0 To .Rows - 1
                            For iCol = 0 To .Cols - 1
                                frmPercent.Show
                                frmPercent.lblPercent = CStr(Round(((iRow + 1) / .Rows) * 100)) & "%"
                                frmPercent.lblPercent.Refresh
                                frmPercent.PrgBar.Value = iRow + 1
                                DBArray(iRow, iCol) = "'" & .TextMatrix(iRow, iCol)
                            Next iCol
                        Next iRow
                        xlSheet.Cells(9, 1).Resize(.Rows, .Cols).Value = DBArray
                        xlSheet.Range(xlSheet.Cells(9, 1), xlSheet.Cells(.Rows + 9, .Cols)).Select
                        
                        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
                        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
                        With Selection.Borders(xlEdgeLeft)
                            .LineStyle = xlContinuous
                            .Weight = xlMedium
                            .ColorIndex = xlAutomatic
                        End With
                        With Selection.Borders(xlEdgeTop)
                            .LineStyle = xlContinuous
                            .Weight = xlMedium
                            .ColorIndex = xlAutomatic
                        End With
                        With Selection.Borders(xlEdgeBottom)
                            .LineStyle = xlContinuous
                            .Weight = xlMedium
                            .ColorIndex = xlAutomatic
                        End With
                        With Selection.Borders(xlEdgeRight)
                            .LineStyle = xlContinuous
                            .Weight = xlMedium
                            .ColorIndex = xlAutomatic
                        End With
                        With Selection.Borders(xlInsideVertical)
                            .LineStyle = xlContinuous
                            .Weight = xlThin
                            .ColorIndex = xlAutomatic
                        End With
                        With Selection.Borders(xlInsideHorizontal)
                            .LineStyle = xlContinuous
                            .Weight = xlThin
                            .ColorIndex = xlAutomatic
                        End With
                        xlSheet.Cells(.Rows + 10, 1) = Label2(16).Caption
                        xlSheet.Cells(.Rows + 10, 2) = "'" & txtZPCP(16).Text
                        xlSheet.Cells(.Rows + 10, 3) = Label2(17).Caption
                        xlSheet.Cells(.Rows + 10, 4) = "'" & txtZPCP(17).Text
                        Unload frmPercent
                        xlSheet.Columns.AutoFit
                        xlSheet.Cells(1, 1).Value = Label1.Caption
                        xlSheet.Cells(1, 1).Font.Name = "宋体"
                        xlSheet.Cells(1, 1).Font.Size = 22
                        xlSheet.Cells(1, 1).Font.Bold = True
                        xlSheet.Cells(1, 1).Select
                        xlSheet.SaveAs FileName
                        CdlZPCP.FileName = ""
                        xlSheet.Application.Quit
                        Set xlBook = Nothing
                        Set xlSheet = Nothing
                End With
      

  3.   

    使用循环的话 ,海量数据插入excel就很慢
    考虑一下这样的sql
    sql = "SELECT * INTO [Excel 8.0;DATABASE=" & xPath & "FROM [" & tableNameDLTDLT & "] where " & DateField & " between #" & CDate(startDate) & "# and #" & CDate(endDate) & "# order by " & DateField & " desc"