Example #1
0
        /**
         *
         */
        public void downloadExcel(System.Web.HttpResponseBase OResponse, System.Web.UI.WebControls.GridView OGrid, string nomeDownload)
        {
            OResponse.ClearContent();
            OResponse.Buffer = true;
            OResponse.AddHeader("content-disposition", "attachment; filename=" + nomeDownload);
            OResponse.ContentType     = "application/ms-excel";
            OResponse.ContentEncoding = Encoding.GetEncoding(28592);

            OResponse.Charset = "";
            StringWriter   sw  = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            OGrid.RenderControl(htw);

            OResponse.Output.Write(sw.ToString());
            OResponse.Flush();
            OResponse.End();
        }
Example #2
0
        /// <summary>
        /// Create an Excel file, and write it out to a MemoryStream (rather than directly to a file)
        /// </summary>
        /// <param name="ds">DataSet containing the data to be written to the Excel.</param>
        /// <param name="filename">The filename (without a path) to call the new Excel file.</param>
        /// <param name="Response">HttpResponse of the current page.</param>
        /// <returns>Either a MemoryStream, or NULL if something goes wrong.</returns>
        public static bool CreateExcelDocumentAsStream(DataSet ds, string filename, System.Web.HttpResponseBase Response)
        {
            try
            {
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                using (SpreadsheetDocument document = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook, true))
                {
                    WriteExcelFile(ds, document);
                }
                stream.Flush();
                stream.Position = 0;

                Response.ClearContent();
                Response.Clear();
                Response.Buffer  = true;
                Response.Charset = "";

                //  NOTE: If you get an "HttpCacheability does not exist" error on the following line, make sure you have
                //  manually added System.Web to this project's References.

                Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
                Response.AddHeader("content-disposition", "attachment; filename=" + filename);
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                byte[] data1 = new byte[stream.Length];
                stream.Read(data1, 0, data1.Length);
                stream.Close();
                Response.BinaryWrite(data1);
                Response.Flush();
                Response.End();

                return(true);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Failed, exception thrown: " + ex.Message);
                return(false);
            }
        }
Example #3
0
        public void ToFile(object sourceData, System.Web.HttpResponseBase Response, string reportName = "ReportsDownload", EXPORTTYPE exportType = EXPORTTYPE.EXCEL)
        {
            var grid = new GridView();

            grid.DataSource = sourceData;
            grid.DataBind();
            Response.ClearContent();
            Response.Buffer  = true;
            Response.Charset = "";
            StringWriter   sw  = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            switch (exportType)
            {
            case EXPORTTYPE.EXCEL:
            {
                Response.AddHeader("content-disposition", "attachment; filename=" + reportName + ".xls");
                Response.ContentType = "application/ms-excel";
                break;
            }

            case EXPORTTYPE.WORD:
            {
                Response.AddHeader("content-disposition", "attachment; filename=" + reportName + ".doc");
                Response.ContentType = "application/ms-word";
                break;
            }

            case EXPORTTYPE.PDF:
                break;
            }
            grid.RenderControl(htw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }