-
Notifications
You must be signed in to change notification settings - Fork 9
/
ExportHelper.cs
95 lines (88 loc) · 4.51 KB
/
ExportHelper.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
using System;
using System.Linq;
using System.Threading.Tasks;
using DevExpress.XtraReports.UI;
using DevExtreme.AspNet.Data.ResponseModel;
using System.Drawing;
using DataSourceWebApi.Models;
using System.IO;
namespace DataSourceWebApi.Services
{
public class ExportHelper
{
private readonly string pdf = "pdf";
private readonly string xlsx = "xlsx";
private readonly string docx = "docx";
public async Task<byte[]> ExportResult(LoadResult lr, string format) {
XtraReport report = new XtraReport();
var loadedData = lr;
report.DataSource = loadedData.data.Cast<Products>();
CreateReport(report, new string[] { "ProductName", "CategoryId", "UnitPrice", "UnitsInStock", "UnitsOnOrder", "Discontinued" });
return await new TaskFactory().StartNew(() => {
report.CreateDocument();
using (MemoryStream fs = new MemoryStream()) {
if (format == pdf)
report.ExportToPdf(fs);
else if (format == xlsx)
report.ExportToXlsx(fs);
else if (format == docx)
report.ExportToDocx(fs);
return fs.ToArray();
}
});
}
private void CreateReport(XtraReport report, string[] fields) {
PageHeaderBand pageHeader = new PageHeaderBand() { HeightF = 23, Name = "pageHeaderBand" };
float tableWidth = report.PageWidth - report.Margins.Left - report.Margins.Right;
XRTable headerTable = XRTable.CreateTable(
new RectangleF(0, // rect X
0, // rect Y
tableWidth, // width
40), // height
1, // table row count
0); // table column count
headerTable.Borders = DevExpress.XtraPrinting.BorderSide.All;
headerTable.BackColor = Color.Gainsboro;
headerTable.Font = new Font("Verdana", 10, FontStyle.Bold);
headerTable.Rows.FirstRow.WidthF = tableWidth;
headerTable.BeginInit();
foreach (string field in fields) {
XRTableCell cell = new XRTableCell();
cell.Width = 100;
cell.Text = field;
cell.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
headerTable.Rows.FirstRow.Cells.Add(cell);
}
headerTable.EndInit();
headerTable.AdjustSize();
pageHeader.Controls.Add(headerTable);
DetailBand detail = new DetailBand() { HeightF = 23, Name = "detailBand" };
XRTable detailTable = XRTable.CreateTable(
new RectangleF(0, // rect X
0, // rect Y
tableWidth, // width
40), // height
1, // table row count
0); // table column count
detailTable.WidthF = tableWidth;
detailTable.Rows.FirstRow.WidthF = tableWidth;
detailTable.Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Bottom;
detailTable.BeginInit();
foreach (string field in fields) {
XRTableCell cell = new XRTableCell();
ExpressionBinding binding = new ExpressionBinding("BeforePrint", "Text", String.Format("[{0}]", field));
cell.ExpressionBindings.Add(binding);
cell.Width = 100;
cell.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft;
if (field.Contains("Date"))
cell.TextFormatString = "{0:MM/dd/yyyy}";
detailTable.Rows.FirstRow.Cells.Add(cell);
}
detailTable.Font = new Font("Verdana", 8F);
detailTable.EndInit();
detailTable.AdjustSize();
detail.Controls.Add(detailTable);
report.Bands.AddRange(new Band[] { detail, pageHeader });
}
}
}