【问题标题】:Exporting Grid Data to Excel and saving it in zip format将网格数据导出到 Excel 并以 zip 格式保存
【发布时间】:2015-09-30 15:12:52
【问题描述】:

您好,我正在开发 asp net 应用程序。在此我需要将网格数据导出到 Excel,最后将 excel 文件保存为 zip 文件。我不想先将 Excel 文件保存在某个位置,然后使用 zip 功能来获取该文件并将其转换为 Zip 并保存。我想要将 Grid 直接转换为 Excel 然后 Zip 然后最终保存它的功能。我看过很多表格和网站,但没有一个给出正确答案。

【问题讨论】:

  • 您的目标是什么版本的 excel?

标签: c# asp.net gridview zip export-to-excel


【解决方案1】:

您可以通过dot.net Zip dll 执行此操作,以下代码将为您提供帮助

gv.AllowPaging = false;
            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
            Response.ContentType = "application/zip";
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
           // byte[] toBytes = Encoding.ASCII.GetBytes(somestring);
            MemoryStream stream = new MemoryStream();
             string attachment = sw.ToString();
             byte[] data = Encoding.ASCII.GetBytes(attachment);
                stream.Write(data, 0, data.Length);
                stream.Seek(0, SeekOrigin.Begin);   // <-- must do this after writing the stream!
           //   File.WriteAllBytes(@"D:\Saurabh\Testing\inputpdf\saurabhhtest.xls", stream.GetBuffer());



        using (ZipFile zipFile = new ZipFile())
        {
            zipFile.AddEntry("saurabhtest1.xls", stream);
            zipFile.Save(Response.OutputStream);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 2023-03-30
    • 2014-02-18
    相关资源
    最近更新 更多