多种下载文件方式 Response.BinaryWrite(byte[] DocContent);Response.WriteFile(System.IO.FileInfo DownloadFile .FullName);Response.Write(string html2Excel);
通过html给xls赋值,并下载xls文件
一、this.Response.Write(sw.ToString());System.IO.StringWriter sw = new System.IO.StringWriter();
this.Response.Clear();
string strFileName;
strFileName = "报表" + ".xls";
Response.Buffer = true;
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Accept-Language", "zh-cn");
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(strFileName));
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(sw);
this.gvMutiQueResult.AllowPaging = false;
if (Session["QueryResult"] != null)
{
this.gvMutiQueResult.DataSource = (DataTable)Session["QueryResult"];
this.gvMutiQueResult.DataBind();
}
else
{
BindGV(); 绑定数据
}
this.gvMutiQueResult.RenderControl(htmlWrite);
this.Response.Write(sw.ToString());
this.Response.End();
Response.Write("<script type=‘text/javascript‘>window.history.back();</script>");
二、Response.Write(html2Excel);string html2Excel
string strHtml; string fileName;
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(fileName) + ".xls\"");
Response.ContentType = "application/ms-excel";
string html2Excel = "<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\"><head><meta content=\"text/html;charset=UTF-8\" http-epuiv=\"content-type\">"
+
string.Format(@"<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>{0}</x:Name>
<x:WorksheetOptions>
<x:Print>
<x:ValidPrinterInfo />
</x:Print>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml>
<![endif]-->", fileName)
+ "</head><body>"
+ strHtml +
"</body></html>";
Response.Write(html2Excel);
Response.End();
三、
//获取数据
IDataBase sDB = DBFactory.GetDBInstance();
DataTable dt = sDB.GetDataTable("select * from 表 where ID=‘" + ID.ToString() + "‘");
//输出
if (dt.Rows.Count > 0)
{
string DocFileName = dt.Rows[0]["DocFileName"].ToString();
string DocSize = dt.Rows[0]["DocSize"].ToString();
byte[] DocContent = (byte[])dt.Rows[0]["DocContent"];
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
//string ContentType = dt.Rows[0]["DocType"].ToString();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(DocFileName, System.Text.Encoding.UTF8) + "\"");
Response.AddHeader("Content-Length", DocSize.ToString());
Response.BinaryWrite(DocContent);
Response.Flush();
Response.End();
}
四、Response.WriteFile(DownloadFile.FullName); System.IO.FileInfo DownloadFile = new System.IO.FileInfo(strFileName);
//下载开机提醒安装程序 rar
private void DownloadDocument()
{
try
{
string strFileName = Server.MapPath("../../help/BootAlert.rar");
System.IO.FileInfo DownloadFile = new System.IO.FileInfo(strFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/ms-excel";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
catch (Exception err)
{
throw err;
}
}
五、 byte[] binaryContent Response.BinaryWrite(binaryContent);
newFileName = newFileName.Replace("\\\\", "\\").Replace("\\", "/");
string outputFileName = newFileName.Substring(newFileName.LastIndexOf(‘/‘) + 1);
byte[] binaryContent = System.IO.File.ReadAllBytes(newFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(outputFileName, System.Text.Encoding.UTF8) + "\"");
Response.AddHeader("Content-Length", binaryContent.Count().ToString());
Response.BinaryWrite(binaryContent);
Response.Flush();
Response.End();
六、Response.TransmitFile(strFilePath, 0, fileSize);long fileSize = info.Length
FileInfo info = new FileInfo(strFilePath);
long fileSize = info.Length;
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Accept-Language", "zh-cn");
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(info.Name));
//不指明Content-Length用Flush的话不会显示下载进度
Response.AddHeader("Content-Length", fileSize.ToString());
Response.TransmitFile(strFilePath, 0, fileSize);
Response.Flush();
Response.Close();
info.Delete();
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。