-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExportController.cs
77 lines (76 loc) · 3.08 KB
/
ExportController.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
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using Microsoft.AspNetCore.Mvc;
using ReportingAspNetCorePrintWithoutPreview.PredefinedReports;
using System.IO;
using System.Threading.Tasks;
namespace ReportingAspNetCorePrintWithoutPreview.Controllers
{
[Route("api/[controller]")]
public class ExportController : Controller
{
[HttpGet("[action]")]
public async Task<object> Print()
{
var report = new TestReport();
using (var ms = new MemoryStream())
{
await report.ExportToPdfAsync(ms, new DevExpress.XtraPrinting.PdfExportOptions { ShowPrintDialogOnOpen = true });
return File(ms.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf);
}
}
[HttpGet("[action]")]
public ActionResult Export(string format = "pdf")
{
format = format.ToLower();
XtraReport report = new TestReport();
string contentType = string.Format("application/{0}", format);
using (MemoryStream ms = new MemoryStream())
{
switch (format)
{
case "pdf":
contentType = "application/pdf";
report.ExportToPdf(ms);
break;
case "docx":
contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
report.ExportToDocx(ms);
break;
case "xls":
contentType = "application/vnd.ms-excel";
report.ExportToXls(ms);
break;
case "xlsx":
contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
report.ExportToXlsx(ms);
break;
case "rtf":
report.ExportToRtf(ms);
break;
case "mht":
contentType = "message/rfc822";
report.ExportToMht(ms);
break;
case "html":
contentType = "text/html";
report.ExportToHtml(ms);
break;
case "txt":
contentType = "text/plain";
report.ExportToText(ms);
break;
case "csv":
contentType = "text/plain";
report.ExportToCsv(ms);
break;
case "png":
contentType = "image/png";
report.ExportToImage(ms, new ImageExportOptions() { Format = System.Drawing.Imaging.ImageFormat.Png });
break;
}
return File(ms.ToArray(), contentType);
}
}
}
}