DataSet ds = new QPLFDSDB.DAL.goods().GetList(" ");
string fileName = "";
//获得excel文件名
if (new Help().ExportExcel(ds.Tables[0], ref fileName))
{
string path = Server.MapPath("/xml/") + fileName;
FileInfo DownloadFile = new FileInfo(path);
Response.Clear(); Response.ClearHeaders(); Response.Buffer = false; Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8)); Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", path));
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
//Response.ContentType = "application/octet-stream";
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
现在不是下载不下来啊 现在是 下载到本地客户端的xls文件 里面的内容居然是当前的页面 跟服务端的xls不一样 我晕 这是什么怪情况
/// 报表下载到客户端
/// </summary>
/// <param name="response"></param>
/// <param name="serverPath"></param>
/// <param name="type"></param>
public static void DownloadFile(HttpResponse response,string serverPath,string type)
{
FileStream fs = null;
try
{
fs = File.OpenRead(serverPath);
byte[] buffer = new byte[1024];
long count = 1024;
response.Buffer = true; response.Charset = "GB2312";
response.ContentEncoding = System.Text.Encoding.UTF7;
response.AppendHeader("Content-Disposition", "attachment;filename =" + HttpUtility.UrlEncode(serverPath, Encoding.UTF8).ToString());
response.ContentType = "";
switch (type)
{
case ".pdf":
response.ContentType = "application/pdf";
break;
case ".doc":
response.ContentType = "application/ms-word";
break;
case ".xls":
response.ContentType = "application/ms-excel";
break;
default:
response.ContentType = "application/all";
break;
}
while (count == 1024)
{
count = fs.Read(buffer, 0, 1024);
response.BinaryWrite(buffer);
}
}
catch {
}
finally
{
fs.Close();
}
} #endregion