Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I debug,str is not null and has data,but when I use the code(File(str, "attachment;filename=" + "TempFile.xls")) ,it did not Pop-up a window.
C#
 public ActionResult ExportData()
        {
 //根据传过来的参数查询要到处的数据
             System.Data.DataTable dtData = stateService.Getexcetdata(fundtype);
             System.Web.UI.WebControls.DataGrid dgExport = null;
             // 当前对话 
             System.Web.HttpContext curContext = System.Web.HttpContext.Current;
             // IO用于导出并返回excel文件 
             System.IO.StringWriter strWriter = null;
             System.Web.UI.HtmlTextWriter htmlWriter = null;
             string filename = DateTime.Now.Month + "_" + DateTime.Now.Day + "_"
                 + DateTime.Now.Hour + "_" + DateTime.Now.Minute;
             byte[] str = null;
 
            if (dtData != null)
             {
                 // 设置编码和附件格式 
                 curContext.Response.ContentType = "application/vnd.ms-excel";
                 curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
                 curContext.Response.Charset = "gb2312";
 
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
                 // 导出excel文件 
                 strWriter = new System.IO.StringWriter();
                 htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
 
                // 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid 
                 dgExport = new System.Web.UI.WebControls.DataGrid();
                 dgExport.DataSource = dtData.DefaultView;
                 dgExport.AllowPaging = false;
                 dgExport.DataBind();
                 dgExport.RenderControl(htmlWriter);
         // 返回客户端 
                 str = System.Text.Encoding.UTF8.GetBytes(strWriter.ToString());
File(str, "attachment;filename=" + "TempFile.xls")
success=true;
 return  Json(new {success},JsonRequestBehavior.AllowGet );
}


I try to update the codes,but still fail

C#
Response.Clear();
                Response.Charset = "GB2312";

                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
                Response.AddHeader("Content-Length", file.Length.ToString());

                Response.ContentType = "application/ms-excel";

                Response.WriteFile(file.FullName);

                Response.Flush();


                Response.End();
                return File(str, "attachment;filename=" + "TempFile.xls");
Posted
Updated 30-Mar-13 4:17am
v2
Comments
PEIYANGXINQU 30-Mar-13 10:17am    
I change to return JSon(File(str, "attachment;filename=" + "TempFile.xls"},JsonRequestBehavior.AllowGet );
but still fail: it did not Pop-up a window.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900