导出到excel system.outofmemoryexception [英] Export to excel system.outofmemoryexception

查看:209
本文介绍了导出到excel system.outofmemoryexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 Sub ExportExcel()
使用wb作为新XLWorkbook()
' 循环遍历GridView页面。

对于i As Integer = 1到GridView2.PageCount - 1
Me.BindGrid()
'
设置当前页面。
GridView2.PageIndex = i
' 使用与GridView列相同的模式创建DataTable。
Dim dt As New DataTable(Page_&(i + 1))
For Each cell As TableCell In GridView2.HeaderRow.Cells
dt.Columns.Add(cell.Text)
下一页

'
循环和添加来自 GridView到DataTable。
For Each row As GridViewRow In GridView2.Rows
dt.Rows.Add()
for j As Integer = 0 To row .Cells.Count - 1
dt.Rows(dt.Rows.Count - 1 ) (j)= row.Cells(j).Text
下一个
下一个

' 将DataTable添加为工作表。
wb.Worksheets.Add(dt)
下一个

'
导出Excel文件。
Response.Clear()
Response.Buffer = True
Response.Charset =
Response.ContentType = application / vnd.openxmlformats-officedocument.spreadsheetml.sheet
Response.AddHeader( content-disposition attachment; filename = GridView.xlsx
使用MyMemoryStream作为新的MemoryStream()
wb.SaveAs(MyMemoryStream) - // 此处出错 - System.OutOfMemoryException。
MyMemoryStream.WriteTo(Response.OutputStream)
Response.Flush()
响应。[结束]()
结束使用
结束使用
结束子





我尝试过:



我试图在一张表格中导出30000条记录,它给出了一个内存异常

i尝试设置maxRequestLength =2097151但它没有工作

然后我试过如果有人知道帮助,我可以导出多张工作表,但不允许我输出超过8000条记录。

解决方案

请分享完整的堆栈跟踪。

您也可以参考将非常大的数据导出到Excel文件 [ ^

Sub ExportExcel()
        Using wb As New XLWorkbook()
            'Loop through the GridView pages.

            For i As Integer = 1 To GridView2.PageCount - 1
                Me.BindGrid()
                'Set the Current Page.
                GridView2.PageIndex = i
                'Create a DataTable with schema same as GridView columns.
                Dim dt As New DataTable("Page_" & (i + 1))
                For Each cell As TableCell In GridView2.HeaderRow.Cells
                    dt.Columns.Add(cell.Text)
                Next

                'Loop and add rows from GridView to DataTable.
                For Each row As GridViewRow In GridView2.Rows
                    dt.Rows.Add()
                    For j As Integer = 0 To row.Cells.Count - 1
                        dt.Rows(dt.Rows.Count - 1)(j) = row.Cells(j).Text
                    Next
                Next

                'Add DataTable as Worksheet.
                wb.Worksheets.Add(dt)
            Next

            'Export the Excel file.
            Response.Clear()
            Response.Buffer = True
            Response.Charset = ""
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            Response.AddHeader("content-disposition", "attachment;filename=GridView.xlsx")
            Using MyMemoryStream As New MemoryStream()
                wb.SaveAs(MyMemoryStream)--// Error here-- System.OutOfMemoryException.
                MyMemoryStream.WriteTo(Response.OutputStream)
                Response.Flush()
                Response.[End]()
            End Using
        End Using
    End Sub



What I have tried:

I tried to export 30000 records in a excel on a one sheet it gives outofmemory exception
i tried setting maxRequestLength="2097151" but it didnt work
then i tried to export in multiple sheets it worked but did not allow me to export more than 8000 records if anybody knows help.

解决方案

Please share complete stacktrace.
also you can refer Export very large data to Excel file[^]


这篇关于导出到excel system.outofmemoryexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆