private void templatesList_SelectedIndexChanged(object sender, EventArgs e) { if (templatesList.SelectedValue == "-1" || selectedID == "") { return; } var report = GetSingleReport(); if (report == null) { return; } var reportBuilder = new WordReportBuilder(); var response = Page.Response; response.Clear(); response.AddHeader("Content-Type", "application/octet-stream"); response.AddHeader("Content-Disposition", "attachment;filename=" + templatesList.SelectedItem.Text + ".docx"); reportBuilder.Create(response.OutputStream, report); response.End(); }
public void builder_test_report() { Report report; ReportDirector director = new ReportDirector(); #region Excel { ExcelReportBuilder builder = new ExcelReportBuilder(); report = director.CreateReport(builder); Assert.That(report.ReportType == ReportType.Excel); Assert.That(report.ReportHeader == HeaderType.ExcelHeader); Assert.That(report.ReportContent == ContentType.ExcelContent); Assert.That(report.ReportFooter == FooterType.ExcelFooter); } #endregion #region PDF { PDFReportBuilder builder = new PDFReportBuilder(); report = director.CreateReport(builder); Assert.That(report.ReportType == ReportType.PDF); Assert.That(report.ReportHeader == HeaderType.PDFHeader); Assert.That(report.ReportContent == ContentType.PDFContent); Assert.That(report.ReportFooter == FooterType.PDFFooter); } #endregion #region Word { WordReportBuilder builder = new WordReportBuilder(); report = director.CreateReport(builder); Assert.That(report.ReportType == ReportType.Word); Assert.That(report.ReportHeader == HeaderType.WordHeader); Assert.That(report.ReportContent == ContentType.WordContent); Assert.That(report.ReportFooter == FooterType.WordFooter); } #endregion #region Powerpoint { PowerPointReportBuilder builder = new PowerPointReportBuilder(); report = director.CreateReport(builder); Assert.That(report.ReportType == ReportType.PowerPoint); Assert.That(report.ReportHeader == HeaderType.PowerPointHeader); Assert.That(report.ReportContent == ContentType.PowerPointContent); Assert.That(report.ReportFooter == FooterType.PowerPointFooter); } #endregion }
private string PrepareReport(int templateID) { var extension = GetReportExtension(templateID); var guid = Guid.NewGuid(); var link = string.Format(@"~\temp_data\reports\{0}{1}", guid, extension); var reportPath = HttpContext.Current.Server.MapPath(link); FileStream fileStream; try { var dir = Path.GetDirectoryName(reportPath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } fileStream = File.Create(reportPath); } catch { throw new Exception(string.Format("Не удалось создать файл {0} с расширением {1}, полный путь {2}", guid, extension, reportPath)); } var reportBuilder = new WordReportBuilder(); var report = GetSingleReport(); if (report != null) { reportBuilder.Create(fileStream, report); } fileStream.Flush(); fileStream.Close(); return(link); }