Export to Excel file as .zip to reduce file size. How to compress xmldata before sending it to the client?

二次信任 提交于 2020-01-06 14:54:14

问题


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()

回答1:


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".



来源:https://stackoverflow.com/questions/3208310/export-to-excel-file-as-zip-to-reduce-file-size-how-to-compress-xmldata-before

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!