Spreadsheetgear.IWorkbook/工作簿对象。如何检索工作簿的长度?
开发平台:.NET 2.0 平台:ASP.NET 电子表格齿轮:2008 语言:C#
有没有办法检索工作簿的长度(以字节为单位)而不将其复制/保存到另一个对象?
我浏览了文档并进行了多次尝试,但没有成功。
我使用 SaveToStream 方法保存工作簿,该方法将工作簿直接写入 Page.Response.OutputStream,然后从浏览器中刷新。但由于该对象的类型是 Stream,因此该抽象类没有实现 Length 属性,并且将其转换为像 MemoryStream 这样的子类将返回 null。在这两种情况下,应用程序都会抛出异常。
我需要捕获长度(以字节为单位),以便将其记录下来,以便在我们的应用程序中进行性能审查。
我的目的是避免将该 Stream 复制到另一个对象,如果可能的话,仅使用引用强制转换,因为我们需要优化在应用程序中导出到 Excel 电子表格的模块的内存占用。
Development Platform: .NET 2.0
Platform: ASP.NET
Spreadsheetgear: 2008
Language: C#
Is there a way to retrieve the Length in bytes of a workbook without copying it/saving it to another object?
I went through the documentation and several attempts with no success.
I am saving the workbook by using the SaveToStream method which is writing the workbook directly to the Page.Response.OutputStream which then is flushed out of the browser. But since the type of this object is Stream, this abstract class doesn't implement the Length property and casting it to a child class like MemoryStream will return null. In both cases the app will throw an Exception.
I need to capture the length in bytes in order to log it for performance review purposes in our application.
My purpose is to avoid copying that Stream to another object and if possible using only reference casts because we need to optimize the memory footprint of the module that exports to Excel spreadsheets in out application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果未实现 Length 属性,您可以将整个流复制到 MemoryStream,获取其长度,然后从内存流发送该流。
这样做的缺点是您必须等待整个副本完成才能知道长度,这会破坏流传输方面。因此,您可以实现一个新的流,该流对通过的字节进行计数,并保留您可以在最后请求的运行总数。以下是实现它的方法:
您可以使用此类包装 Response.OutputStream,刷新它,然后查看写入了多少字节:
If the Length property is not implemented you can copy the entire stream to a MemoryStream, get the length of that, then send the stream from the memory stream.
The disadvantage of this is that you must wait for the entire copy to complete before you know the length, which breaks the streaming aspect. So instead, you can implement a new stream which counts the bytes as they pass through and keeps a running total which you can ask for at the end. Here's how you could implement it:
You can wrap the Response.OutputStream with this class, flush it, and then see how many bytes were written: