[英]Exporting Grid Data to Excel and saving it in zip format
嗨,我正在研究ASP網絡應用程序。 為此,我需要將Grid數據導出到Excel,最后將excel文件另存為zip文件。 我不想先將Excel文件保存在某個位置,然后使用zip功能來獲取該文件並將其轉換為Zip並保存。 我想要可以直接將Grid轉換為Excel的功能,然后將Zip最終保存。 我見過太多表格和網站,但沒有一個給出正確的答案。
您可以通過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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.