Example #1
0
        protected void ExportButton_Click(object sender, EventArgs e)
        {
            string strReportHtml = string.Empty;

            try
            {
                ExcelManager objExcelManager = new ExcelManager();
                //string strParametersPath = this.Page.Server.MapPath("../Reports/Templates/");

                // Pass the data and get the path for generated temporary file.

                // DataTable vdtData, string vstrfilePath, string vstrExportCaption, string vstrDBColumn, string vstrExcelColumn)


                if (null != ViewState["DataId"])
                {
                    string strRand = ViewState["DataId"].ToString();

                    if (null != this.Page.Session[strRand])
                    {

                        DataTable dt = this.Page.Session[strRand] as DataTable;

                        if ((null != dt) & (string.IsNullOrEmpty(ExportTemplatePath) == false) && (string.IsNullOrEmpty(ExportTemplate) == false) && (string.IsNullOrEmpty(DBColumn) == false) & (string.IsNullOrEmpty(ExcelColumn) == false))
                        {
                            string strFilePath = objExcelManager.GenerateExcelDataFileFromGrid(dt, this.Page.Server.MapPath(ExportTemplatePath), ExportTemplate, ExportCaption, DBColumn, ExcelColumn,
                                ExcelHeaderRow, StartRow, StartColumn, MaxLevel, SheetNumber, CurrentDateRow, CurrentDateCol, StartDateRow, StartDateCol, EndDateRow, EndDateCol, ReportStartDate, ReportEndDate);


                            System.IO.FileInfo objfInfo = new System.IO.FileInfo(strFilePath);
                            if (objfInfo.Exists)
                            {
                                this.Page.Response.Clear();
                                string attachment = "attachment; filename=" + objfInfo.Name;

                                this.Page.Response.AddHeader("content-disposition", attachment);
                                //if (vExportType == Constants.ExportType.Excel)
                                //{
                                this.Page.Response.ContentType = "application/vnd.ms-excel";
                                //}
                                //else
                                //{
                                //    Response.ContentType = "text/csv";
                                //}

                                this.Page.Response.BinaryWrite(System.IO.File.ReadAllBytes(strFilePath));

                                objfInfo.Delete(); // Once the file content is written to response stream delete it.
                                //Response.End();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw;
                SendMail.MailMessage("ExcelPackage > Error > ExportButton_Click(object sender, EventArgs e)", ex.ToString());
            }
            finally
            {
                this.Page.Response.End();
            }

        }