Spreadsheetgear.IWorkbook/工作簿对象。如何检索工作簿的长度?

发布于 2024-08-14 03:21:57 字数 443 浏览 7 评论 0原文

开发平台:.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淑女气质 2024-08-21 03:21:57

如果未实现 Length 属性,您可以将整个流复制到 MemoryStream,获取其长度,然后从内存流发送该流。

这样做的缺点是您必须等待整个副本完成才能知道长度,这会破坏流传输方面。因此,您可以实现一个新的流,该流对通过的字节进行计数,并保留您可以在最后请求的运行总数。以下是实现它的方法:

public class LengthLoggingStream : Stream
{
    private Stream stream;
    public long TotalBytesRead { get; private set; }
    public long TotalBytesWritten { get; private set; }

    public LengthLoggingStream(Stream stream)
    {
        this.stream = stream;
    }

    public override bool CanRead
    {
        get { return stream.CanRead; }
    }

    public override bool CanSeek
    {
        get { return stream.CanSeek; }
    }

    public override bool CanWrite
    {
        get { return stream.CanWrite; }
    }

    public override void Flush()
    {
        stream.Flush();
    }

    public override long Length
    {
        get { return stream.Length; }
    }

    public override long Position
    {
        get
        {
            return stream.Position;
        }
        set
        {
            stream.Position = value;
        }
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        int bytesRead = stream.Read(buffer, offset, count);

        if (bytesRead > 0)
            TotalBytesRead += bytesRead;

        return bytesRead;
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        return stream.Seek(offset, origin);
    }

    public override void SetLength(long value)
    {
        stream.SetLength(value);
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        TotalBytesWritten += count;

        stream.Write(buffer, offset, count);
    }
}

您可以使用此类包装 Response.OutputStream,刷新它,然后查看写入了多少字节:

    LengthLoggingStream loggingStream = new LengthLoggingStream(Response.OutputStream);
    workbook.SaveToStream(loggingStream, /*other parameters */);
    log("Bytes written: " + loggingStream.TotalBytesWritten);

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:

public class LengthLoggingStream : Stream
{
    private Stream stream;
    public long TotalBytesRead { get; private set; }
    public long TotalBytesWritten { get; private set; }

    public LengthLoggingStream(Stream stream)
    {
        this.stream = stream;
    }

    public override bool CanRead
    {
        get { return stream.CanRead; }
    }

    public override bool CanSeek
    {
        get { return stream.CanSeek; }
    }

    public override bool CanWrite
    {
        get { return stream.CanWrite; }
    }

    public override void Flush()
    {
        stream.Flush();
    }

    public override long Length
    {
        get { return stream.Length; }
    }

    public override long Position
    {
        get
        {
            return stream.Position;
        }
        set
        {
            stream.Position = value;
        }
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        int bytesRead = stream.Read(buffer, offset, count);

        if (bytesRead > 0)
            TotalBytesRead += bytesRead;

        return bytesRead;
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        return stream.Seek(offset, origin);
    }

    public override void SetLength(long value)
    {
        stream.SetLength(value);
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        TotalBytesWritten += count;

        stream.Write(buffer, offset, count);
    }
}

You can wrap the Response.OutputStream with this class, flush it, and then see how many bytes were written:

    LengthLoggingStream loggingStream = new LengthLoggingStream(Response.OutputStream);
    workbook.SaveToStream(loggingStream, /*other parameters */);
    log("Bytes written: " + loggingStream.TotalBytesWritten);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文