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(); } } }
protected void btnImport_Click(object sender, EventArgs e) { Lib.Utils.ExcelUtils utils = new Lib.Utils.ExcelUtils(); string extensionDefault = ".xlsx"; string url = ""; if (fuForm.HasFile) { string extension = System.IO.Path.GetExtension(fuForm.FileName); if (extensionDefault != extension) { hdnError.Value = "O arquivo deve ser do tipo excel (xlsx)."; } else { try { filePath = Server.MapPath("/App_Data/fileUploaded.xlsx"); fuForm.SaveAs(filePath); DataSet dataSet = utils.importExcelFile(filePath); List<string> notAnsweredQuestions = null; List<string> incorrectAnsweredQuestions = null; bool canShowButton = canShowSubmitButton(dataSet.Tables, out notAnsweredQuestions, out incorrectAnsweredQuestions); if (canShowButton) { //rptListBlocks.DataSource = dataSet.Tables; //rptListBlocks.DataBind(); long id = saveResponseForm(dataSet.Tables); url = String.Format("~/Form/View.aspx?rfid={0}", id.ToString()); } else { StringBuilder sb = new StringBuilder(); if (notAnsweredQuestions.Count > 0) { sb.AppendLine(String.Format("Não foi possível carregar o arquivo pois ainda existem perguntas sem respostas.")); sb.AppendLine(); foreach (var item in notAnsweredQuestions) { sb.AppendLine(item); } } if (incorrectAnsweredQuestions.Count > 0) { sb.AppendLine(); sb.AppendLine(); sb.AppendLine(String.Format("Perguntas com respostas incorretas: ")); sb.AppendLine(); foreach (var item in incorrectAnsweredQuestions) { sb.AppendLine(item); } } litNotAnsweredQuestions.Text = sb.ToString().Replace("\r\n", "<br/>"); } } catch { hdnError.Value = "Arquivo inválido. Exporte o questionário e preencha novamente."; } } } loadRanking(); if (!string.IsNullOrEmpty(url)) { Response.Redirect(url); } }