public override void ExecuteResult(ControllerContext context)
        {
            HttpContext curContext = HttpContext.Current;

            curContext.Response.Clear();

            curContext.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);

            curContext.Response.Charset = "";

            curContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            curContext.Response.ContentType = "application/vnd.ms-excel";
            //curContext.Response.ContentType = "application/pdf";
            StringWriter   sw  = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            ExcelGridView.RenderControl(htw);

            byte[] byteArray = Encoding.ASCII.GetBytes(sw.ToString());

            MemoryStream s = new MemoryStream(byteArray);

            StreamReader sr = new StreamReader(s, Encoding.ASCII);



            curContext.Response.Write(sr.ReadToEnd());

            curContext.Response.End();
        }
Exemplo n.º 2
0
        public override void ExecuteResult(ControllerContext context)
        {
            //Create a response stream to create and write the Excel file
            HttpContext curContext = HttpContext.Current;

            curContext.Response.Clear();
            curContext.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
            curContext.Response.Charset = Encoding.Default.ToString();
            curContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            curContext.Response.ContentType = "application/vnd.ms-excel";

            //Convert the rendering of the gridview to a string representation
            StringWriter   sw  = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            ExcelGridView.RenderControl(htw);


            //Open a memory stream that you can use to write back to the response
            byte[]       byteArray = Encoding.Default.GetBytes(sw.ToString());
            MemoryStream s         = new MemoryStream(byteArray);
            StreamReader sr        = new StreamReader(s, Encoding.Default);

            //Write the stream back to the response
            curContext.Response.Write(sr.ReadToEnd());
            curContext.Response.End();
        }
Exemplo n.º 3
0
        public override void ExecuteResult(ControllerContext context)
        {
            HttpContext curContext = HttpContext.Current;

            curContext.Response.Clear();
            switch (Stamp)
            {
            case FileStamper.WithDate:
                curContext.Response.AddHeader("content-disposition", "attachment;filename=" + Path.GetFileNameWithoutExtension(FileName) + "-" + DateTime.Now.ToString("yyyyMMdd") + Path.GetExtension(FileName));
                break;

            case FileStamper.WithDateTime:
                curContext.Response.AddHeader("content-disposition", "attachment;filename=" + Path.GetFileNameWithoutExtension(FileName) + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetExtension(FileName));
                break;

            default:
                curContext.Response.AddHeader("content-disposition", "attachment;filename=" + FileName);
                break;
            }

            curContext.Response.Charset = "";
            curContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            curContext.Response.ContentType = "application/vnd.ms-excel";

            StringWriter   sw  = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            ExcelGridView.RenderControl(htw);

            byte[]       byteArray = Encoding.UTF8.GetBytes(sw.ToString());
            MemoryStream s         = new MemoryStream(byteArray);
            StreamReader sr        = new StreamReader(s, Encoding.UTF8);

            curContext.Response.Write(sr.ReadToEnd());
            curContext.Response.End();
        }