用vb将查询结果导出到EXCEL时,总有“内存溢出”的错误,紧接着又出现“应用程序定义或对象定义错误”的提示,然后就没反应了,请问这是什么原因?

解决方案 »

  1.   

    你试一试这里面的几种方法看看.
    http://expert.csdn.net/Expert/topic/2835/2835777.xml?temp=.8592646
      

  2.   

    Dim nRow As Integer
    Dim nCol As Integer
    On Error GoTo FillError
    ReDim asArray(100000, adoRS.Fields.Count)
    nRow = 0
        For nCol = 0 To adoRS.Fields.Count - 1
            asArray(nRow, nCol) = adoRS.Fields(nCol).Name
        Next nCol
        nRow = 1
    Do While Not adoRS.EOF
        For nCol = 0 To adoRS.Fields.Count - 1
            asArray(nRow, nCol) = adoRS.Fields(nCol).Value
        Next nCol
        adoRS.MoveNext
        nRow = nRow + 1
    Loop
    nRow = nRow + 1
    如果我用以上代码来导出的话,ReDim asArray(100000, adoRS.Fields.Count)这一句用ReDim asArray(adoRS.RecordCount + 2, adoRS.Fields.Count)代替是不是更好一些?
      

  3.   

    在Project-Reference中加入Microsoft Excel 10.0 Object Library(Office 2000)Dim uExcel     As Excel.Application
    Dim uExcelBook As Excel.WorkbookSet uExcel = New Excel.Application
            uExcel.Visible = True
            uExcel.SheetsInNewWorkbook = 1
            Set uExcelBook = uExcel.Workbooks.Add '打开Excel
            '边框设置
            With uExcel.ActiveSheet.Range("A1:K" & (intRow + 1) & "").Borders
                .LineStyle = 1
                .Weight = xlThin
                .ColorIndex = 1
            End With
            '字体设置(第一行以粗体显示) 高度设为 20
            'With uExcel.ActiveSheet.Range("A1:K1").Font
                '.Size = 14
                '.Bold = True
                '.Italic = True
                '.ColorIndex = 3
            'End With
            uExcel.ActiveSheet.Rows.HorizontalAlignment = xlVAlignCenter     '水平居中
            uExcel.ActiveSheet.Rows.VerticalAlignment = xlVAlignCenter       '垂直居中
            '设置第一行标题
            With uExcel.ActiveSheet
                .Cells(1, 1).Value = "检查编号"
                .Cells(1, 2).Value = "测试编号"
                .Cells(1, 3).Value = "测试类型"
           End With'填充数据行
        intI = 2
        Do While Not adoRec.EOF
            With uExcel.ActiveSheet
                .Cells(intI, 1).Value = adoRec("StuCode")
                .Cells(intI, 2).Value = adoRec("TestCode")
                .Cells(intI, 3).Value = adoRec("TestType")
            End With
            intI = intI + 1
            adoRec.MoveNext
        Loop
        adoRec.Close    'uExcel.ActiveSheet.PageSetup.Orientation = xlPortrait 'xlLandscape
        'uExcel.ActiveSheet.PageSetup.PaperSize = xlPaperA4 '适应于A4纸
        'uExcel.ActiveSheet.PrintOut'打印输出
        'uExcel.DisplayAlerts = False '不保存后退出
        'uExcel.Quit
        'uExcel.DisplayAlerts = True
        'uExcel.Quit
        Set uExcel = Nothing
        Set uExcelBook = Nothing