/
HomeController.cs
111 lines (100 loc) · 3.98 KB
/
HomeController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using T569785.Models;
using DevExpress.XtraReports.UI;
using T569785.Reports;
using System.IO;
using DevExpress.XtraPrinting;
namespace T569785.Controllers {
public class HomeController : Controller {
//
// GET: /Home/
private FileResult ExportDocument(byte[] document, string format, string fileName, bool isInline) {
string contentType;
string disposition = (isInline) ? "inline" : "attachment";
switch (format.ToLower()) {
case "docx":
contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
break;
case "xls":
contentType = "application/vnd.ms-excel";
break;
case "xlsx":
contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
break;
case "mht":
contentType = "message/rfc822";
break;
case "html":
contentType = "text/html";
break;
case "txt":
case "csv":
contentType = "text/plain";
break;
case "png":
contentType = "image/png";
break;
default:
contentType = String.Format("application/{0}", format);
break;
}
Response.AddHeader("Content-Disposition", String.Format("{0}; filename={1}", disposition, fileName));
return File(document, contentType);
}
public ActionResult Index() {
return View(new ExportModel() { Format = "pdf" });
}
[HttpGet]
public ActionResult Print() {
XtraReport report = new ProductsReport();
using (MemoryStream ms = new MemoryStream()) {
report.ExportToPdf(ms, new PdfExportOptions() { ShowPrintDialogOnOpen = true });
return ExportDocument(ms.ToArray(), "pdf", "Report.pdf", true);
}
}
[HttpPost]
public ActionResult Export(ExportModel model) {
XtraReport report = new ProductsReport();
string fileName = String.Format("Report.{0}", model.Format);
using (MemoryStream ms = new MemoryStream()) {
switch (model.Format) {
case "pdf":
report.ExportToPdf(ms);
break;
case "docx":
report.ExportToDocx(ms);
break;
case "xls":
report.ExportToXls(ms);
break;
case "xlsx":
report.ExportToXlsx(ms);
break;
case "rtf":
report.ExportToRtf(ms);
break;
case "mht":
report.ExportToMht(ms);
break;
case "html":
report.ExportToHtml(ms);
break;
case "txt":
report.ExportToText(ms);
break;
case "csv":
report.ExportToCsv(ms);
break;
case "png":
report.ExportToImage(ms, new ImageExportOptions() { Format = System.Drawing.Imaging.ImageFormat.Png });
break;
}
return ExportDocument(ms.ToArray(), model.Format, fileName, false);
}
}
}
}