将 Excel 文件导出为 .zip 以减小文件大小。如何在将 xmldata 发送到客户端之前对其进行压缩?

发布于 2024-09-08 21:37:05 字数 1335 浏览 4 评论 0原文

我喜欢在将数据作为 Excel 文件提供给客户端之前压缩 xmldata。我正在尝试将其压缩并作为 .zip 文件提供。它不起作用这是我的代码如下。请帮助

我尝试压缩它,将其转换为字节等。下面代码的问题是,XSL 转换没有正确发生,并且输出 excel 文件是原始 xml,最后有一些 .net 异常。 (这就是我在最后下载的 .xls 文件中看到的内容) 在开始压缩之前,我的下面的代码运行良好,可以从 xml 输入提供格式正确的 excel 文件。 excel 文件非常漂亮,您甚至无法看出它是来自 XML。 请帮忙压缩

 Dim attachment As String = "attachment; filename=DataDownload.xls"
        Response.ClearContent()
        Response.AddHeader("content-disposition", attachment)
        Response.ContentType = "application/vnd.ms-excel"
        'Response.ContentType = "text/csv"

        Response.Charset = ""
        Dim ds As New DataSet()

        Dim objXMLReader As XmlReader
        objXMLReader = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteXmlReader(ConnectionString, CommandType.StoredProcedure, "SPName")

        ds.ReadXml(objXMLReader)

        Dim xdd As New XmlDataDocument(ds)
        Dim xt As New XslCompiledTransform()
        'xt.Transform(xdd, Nothing, Response.OutputStream)

        Dim bytearr() As Byte
        bytearr = System.Text.Encoding.Default.GetBytes(xdd.OuterXml)

        Dim objMemStream As New MemoryStream()
        objMemStream.Write(bytearr, 0, bytearr.Length)
        objMemStream.WriteTo(Response.OutputStream)
        xt.Transform(xdd, Nothing, Response.OutputStream)

        Response.End()

I like to compress the xmldata before the data is provided to the client as an excel file. I am trying to compress and deliver it as a .zip file. its not working Here's my code below. Please help

I tried compressing it, converting it to bytes etc etc. The issue with below code is, the XSL transformation is not happening properly and the output excel file is raw xml with some .net exception at the end. (that's what I see on the .xls file that's downloaded at the end)
Before I started working on compression my below code was working fine that gives properly formatted excel file from the xml input. the excel file is so nice you can't even tell it was from XML.
pLEASE HELP with compression

 Dim attachment As String = "attachment; filename=DataDownload.xls"
        Response.ClearContent()
        Response.AddHeader("content-disposition", attachment)
        Response.ContentType = "application/vnd.ms-excel"
        'Response.ContentType = "text/csv"

        Response.Charset = ""
        Dim ds As New DataSet()

        Dim objXMLReader As XmlReader
        objXMLReader = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteXmlReader(ConnectionString, CommandType.StoredProcedure, "SPName")

        ds.ReadXml(objXMLReader)

        Dim xdd As New XmlDataDocument(ds)
        Dim xt As New XslCompiledTransform()
        'xt.Transform(xdd, Nothing, Response.OutputStream)

        Dim bytearr() As Byte
        bytearr = System.Text.Encoding.Default.GetBytes(xdd.OuterXml)

        Dim objMemStream As New MemoryStream()
        objMemStream.Write(bytearr, 0, bytearr.Length)
        objMemStream.WriteTo(Response.OutputStream)
        xt.Transform(xdd, Nothing, Response.OutputStream)

        Response.End()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

巴黎盛开的樱花 2024-09-15 21:37:05

为什么不使用 XLSX 格式而不是 XLS?它已经是 XML 数据压缩在 ZIP 文件中。您可以将 XLSX 文件重命名为 ZIP 来验证这一点。

更新:顺便说一句,ContentType 在这种情况下应该是“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”而不是“application/vnd.ms-excel” 。

Why you don't use XLSX-Format instead of XLS? It is already XML-Data compressed in a ZIP file. You can rename a XLSX file to ZIP to verify this.

Updated: By the way, ContentType in the case should be "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" instead of "application/vnd.ms-excel".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文