I am trying to build a high(er) performance Excel exporter for large datasets for my ASP.Net application. It's coming along ok, but I have hit an internal error that I do not understand.
Dim rnd As New Random Dim sTempFileName As String = Server.MapPath("Reports") & "\" & rnd.Next(1, 10000000) & ".xlsx" Dim dr As SqlDataReader = SqlHelper.ExecuteReader(GetSQL()) Dim wkb As New Infragistics.Documents.Excel.Workbook(Infragistics.Documents.Excel.WorkbookFormat.Excel2007) Dim wks As Infragistics.Documents.Excel.Worksheet = wkb.Worksheets.Add("Report") AddSqlDataReader(dr, wks) wkb.Save(sTempFileName)
This throws a cryptic error on wkb.Save:
Server Error in '/publisher_dev' Application. Can not access a closed Stream. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ObjectDisposedException: Can not access a closed Stream.Source Error: Line 189: AddSqlDataReader(dr, wks) Line 190: Line 191: wkb.Save(sTempFileName) Line 192: Line 193: Source File: D:\Webfolders\publisher_dev\CustomReports40.aspx.vb Line: 191 Stack Trace: [ObjectDisposedException: Can not access a closed Stream.] System.IO.Compression.DeflateStream.Flush() +6164822 MS.Internal.IO.Packaging.CompressStream.Flush() +29 MS.Internal.IO.Zip.ProgressiveCrcCalculatingStream.Flush() +23 MS.Internal.IO.Zip.ZipIOLocalFileBlock.FlushExposedStreams() +16 MS.Internal.IO.Zip.ZipIOLocalFileBlock.UpdateReferences(Boolean closingFlag) +95 MS.Internal.IO.Zip.ZipIOBlockManager.SaveContainer(Boolean closingFlag) +182 MS.Internal.IO.Zip.ZipIOBlockManager.SaveStream(ZipIOLocalFileBlock blockRequestingFlush, Boolean closingFlag) +80 MS.Internal.IO.Zip.ZipIOModeEnforcingStream.Dispose(Boolean disposing) +94 System.IO.Stream.Close() +20 System.IO.Packaging.PackagePart.Close() +139 System.IO.Packaging.Package.DoClose(PackagePart p) +194 System.IO.Packaging.Package.DoOperationOnEachPart(PartOperation operation) +321 System.IO.Packaging.Package.System.IDisposable.Dispose() +183 Infragistics.Documents.Excel.PackageWrapper.Dispose() +13 Infragistics.Documents.Excel.Serialization.Excel2007.Excel2007WorkbookSerializationManager.Dispose(Boolean disposing) +13 Infragistics.Documents.Excel.Serialization.WorkbookSerializationManager.System.IDisposable.Dispose() +27 Infragistics.Documents.Excel.Workbook.SaveXLSXFile(Stream stream, IPackageFactory packageFactory) +135 Infragistics.Documents.Excel.Workbook.SaveHelper(Stream stream, IPackageFactory packageFactory) +178 Infragistics.Documents.Excel.Workbook.Save(String fileName, IPackageFactory packageFactory) +80 CustomReports40.btnDownload_Click(Object sender, EventArgs e) in D:\Webfolders\publisher_dev\CustomReports40.aspx.vb:191 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563 Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.237
Line 189: AddSqlDataReader(dr, wks) Line 190: Line 191: wkb.Save(sTempFileName) Line 192: Line 193:
[ObjectDisposedException: Can not access a closed Stream.] System.IO.Compression.DeflateStream.Flush() +6164822 MS.Internal.IO.Packaging.CompressStream.Flush() +29 MS.Internal.IO.Zip.ProgressiveCrcCalculatingStream.Flush() +23 MS.Internal.IO.Zip.ZipIOLocalFileBlock.FlushExposedStreams() +16 MS.Internal.IO.Zip.ZipIOLocalFileBlock.UpdateReferences(Boolean closingFlag) +95 MS.Internal.IO.Zip.ZipIOBlockManager.SaveContainer(Boolean closingFlag) +182 MS.Internal.IO.Zip.ZipIOBlockManager.SaveStream(ZipIOLocalFileBlock blockRequestingFlush, Boolean closingFlag) +80 MS.Internal.IO.Zip.ZipIOModeEnforcingStream.Dispose(Boolean disposing) +94 System.IO.Stream.Close() +20 System.IO.Packaging.PackagePart.Close() +139 System.IO.Packaging.Package.DoClose(PackagePart p) +194 System.IO.Packaging.Package.DoOperationOnEachPart(PartOperation operation) +321 System.IO.Packaging.Package.System.IDisposable.Dispose() +183 Infragistics.Documents.Excel.PackageWrapper.Dispose() +13 Infragistics.Documents.Excel.Serialization.Excel2007.Excel2007WorkbookSerializationManager.Dispose(Boolean disposing) +13 Infragistics.Documents.Excel.Serialization.WorkbookSerializationManager.System.IDisposable.Dispose() +27 Infragistics.Documents.Excel.Workbook.SaveXLSXFile(Stream stream, IPackageFactory packageFactory) +135 Infragistics.Documents.Excel.Workbook.SaveHelper(Stream stream, IPackageFactory packageFactory) +178 Infragistics.Documents.Excel.Workbook.Save(String fileName, IPackageFactory packageFactory) +80 CustomReports40.btnDownload_Click(Object sender, EventArgs e) in D:\Webfolders\publisher_dev\CustomReports40.aspx.vb:191 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
Here is the code I use to append the SQLDataReader to the worksheet. I have similar code for appending a datatable to the worksheet. Both end up with the same error.
Shared Sub AddSqlDataReader(ByVal dr As SqlDataReader, ByRef sheetToAddTo As Infragistics.Documents.Excel.Worksheet) Dim i As Integer = 0 Dim iColCount As Integer = dr.FieldCount 'add the column names from the datareader For i = 0 To iColCount - 1 sheetToAddTo.Rows(0).Cells(i).Value = dr.GetName(i) Next Dim iRow As Integer = 0 Dim iCol As Integer = 0 While dr.Read For iCol = 0 To iColCount - 1 Try sheetToAddTo.Rows(iRow + 1).Cells(iCol).Value = dr(iCol).ToString Catch ex As ArgumentException End Try Next iRow += 1 End While dr.Close() End Sub
The size of the worksheet seems to have something to do with it. If it is a smaller worksheet, it doesn't return the error. However, larger worksheets over the Excel 97/2003 row cap throw the error.
When the error occurs, a file actually is written to the target location... however, the file is only 2kb in size, and cannot be opened in Excel..
Hello Rob Hudson,
For the error message "System.ObjectDisposedException: Can not access a closed Stream." I would need a sample for reproducing this in particular on my local machine for determining any cause or bug. As far the exception type is a general exception thrown when accessing a stream that was already closed: http://msdn.microsoft.com/en-us/library/1aayk129.aspx.
Let me know if you have any questions with this matter. Thank you.
Thank you, unfortunately, I have been unable to duplicate this error anywhere other than my production server.
Thanks, -Rob
This is just a follow up on the thread if you have any questions in regards to excel exporting or if you require any further assistance with using the WebExcelExporter control.
If you want to use the compression logic in CLR3, it will have to be installed. If you want to save XLSX files, you will need some form of compression logic. If upgrading to CLR3 is not an option, your only other option is to implement the IPackageFactory interface and all related interfaces to do your own compression. However, this is not an easy thing to do and I wouldn't recommend it if upgrading to CLR3 is possible.
Yes I am sure; Our application is using CLR 2..
alos as mentioned in below url
http://community.infragistics.com/forums/t/22602.aspx
Is Infragistics2 and Infragistics3 mean ".NET 2.0" and ".NET 3.0" ?
Basically, yes. To allow for writing to Excel 2007 format, we took advantage of compression logic that is provided by version 3.0 of the .NET Framework. Since this framework version becomes a prerequisite, and since we want to have the ability to continue to write to Excel 2003 format when using version 2.0 of the .NET Framework, we separated this into two assemblies.
Is it mandetory to installe .net version 3.0 or we do have some other work around..
Thanks,
Infragistics3.Excel.v9.2 was built against CLR3. Are you sure you're using CLR2? If you right click on the project in the solution explorer and select "Add Reference...", you should see WindowsBase as one of the available references. If not, can you upgrade the project to CLR3?
Also we have .net framework 2.0 installed on our server..
Hi ,
Can you please give me high level idea; how to add reference to WindowsBase in your project.
My Application in a website ..
Thanks