2014-02-28 44 views
0

您好我正在處理asp網絡應用程序。在這裏,我需要將網格數據導出到Excel,最後將excel文件保存爲zip文件。我不想先將Excel文件保存在某個位置,然後使用zip功能獲取該文件並將其轉換爲Zip並保存。我想要直接將網格轉換爲Excel然後Zip然後最終保存它的功能。我見過這麼多形式和網站,但沒有給出正確答案。將網格數據導出到Excel並將其保存爲zip格式

+0

什麼版本的Excel中,你定位? – MikeH

+0

Excel 97-2003格式 – user2864496

回答

1

您可以通過dot.net郵編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); 
     } 
相關問題