コード例 #1
0
        protected void btnDownload_Click(object sender, EventArgs e)
        {
            Lib.Entities.BaseForm baseForm = null;
            string excelname = "file.xlsx";

            using (Lib.Repositories.BaseFormRepository repo = new Lib.Repositories.BaseFormRepository(this.ActiveUser))
            {
                baseForm = repo.getInstanceByPeriodDate(DateTime.Now);
            }

            if (baseForm != null)
            {
                excelname = Commons.StringUtils.removeAccents(baseForm.Name) + ".xlsx";

                filePath = Server.MapPath("/App_Data/" + excelname);
                Lib.Utils.ExcelUtils utils = new Lib.Utils.ExcelUtils();

                bool save = utils.createExcel(filePath, true);

                if (save)
                {
                    System.IO.FileInfo file = new System.IO.FileInfo(filePath);

                    Response.Clear();
                    Response.Buffer = true;

                    Response.AddHeader("content-disposition", "attachment;filename=" + excelname);
                    Response.ContentEncoding = Encoding.UTF8;

                    Response.Cache.SetCacheability(HttpCacheability.Private);
                    Response.ContentType = "application/vnd.ms-excel";
                    Response.WriteFile(file.FullName);

                    Response.Flush();
                    Response.End();
                }
            }
        }