3

I am using NPOI 1.2.3.0 in an ASP.NET application to export the results of a rather large SQL query to an Excel 2003 XLS file.

In short, the query results are populated into an ADO.NET DataTable. I then have a routine that loops through the rows in the DataTable and for each row adds a row to an NPOI spreadsheet. It is intelligent enough that once 65,000 rows are exceeded for a single sheet, a new sheet is created and the rows are continued there, starting at the first row in the new sheet.

This approach works well for some of my smaller database queries that include, say, 30,000 rows and 50 columns, but I have this one query that returns north of 125,000 rows and has roughly 50 columns, many of which have a good deal of text.

I am able to construct the spreadsheet without issue, but when I try to stream the generated spreadsheet down to the browser I get an OutOfMemoryException when calling the HSSFWorkbook class's Write method. (Internally, the error is happening when the Write method calls the class's GetBytes method.)

If I run the debugger and stop before the Write method is called, I see that the workbook's Size property returns a value of (roughly) 65 million.

This error is noted on the NPOI project at CodePlex - see the discussion titled Out of Memory Problems - but no resolution was found, unfortunately.

For completeness, here is the code where the exception is raised (specifically, it's raised on the workbook.Write line).

Using exportData As New MemoryStream()
    workbook.Write(exportData)

    Response.ContentType = "application/vnd.ms-excel"
    Response.AddHeader("Content-Disposition", "Attachment;Filename=" & saveAsName)
    Response.Clear()
    Response.BinaryWrite(exportData.GetBuffer())
    Response.End()
End Using

Thanks!

4

2 に答える 2

1

FileStream オブジェクトはエラーを引き起こさず、エラーは 32 ビットでの 512MB の容量制限と 64 ビットでの 2GB の制限によって引き起こされることを念頭に置いて、このシナリオで行うことは、ファイルを memoryStream に書き込んでキャッチすることです。エラーが発生した場合は、より大きなファイルの FileStream に戻ります。

ここには明らかなパフォーマンスのトレードオフがありますが、ユーザーが 2GB を超えるファイルをダウンロードしている場合は、おそらくこれが少し遅くなることを期待する必要があります :-)

これがあなたにとってうまくいくかどうか知りたいです。

ありがとう、デイブ

于 2011-06-07T22:04:50.840 に答える
0

NPOI は MemoryStream だけでなく、バ​​イト配列も使用します。主な根本原因はバイト配列です。しかし、NPOI はこれまでバイト配列を使用する必要がありました。これを変更する予定はまだありません。不便をかけてごめんなさい。

于 2012-08-04T23:33:48.430 に答える