개정판 0b49ffb8
DataExport 추가
Change-Id: I4366303f14a750556f80cc52c2f822cef2b2a514
ConvertService/ServiceBase/Markus.Service.StationController/Extensions/DataExport.cs | ||
---|---|---|
1 |
using GemBox.Spreadsheet; |
|
2 |
using Markus.Service.Interface; |
|
3 |
using Microsoft.Win32; |
|
4 |
using System; |
|
5 |
using System.Collections.Generic; |
|
6 |
using System.IO; |
|
7 |
using System.Linq; |
|
8 |
using System.Text; |
|
9 |
using System.Windows; |
|
10 |
|
|
11 |
namespace Markus.Service.StationController.Extensions |
|
12 |
{ |
|
13 |
class DataExport |
|
14 |
{ |
|
15 |
public void DataExportData(System.Collections.ObjectModel.ObservableCollection<ConvertItem> FilterConvertSource) |
|
16 |
{ |
|
17 |
SaveFileDialog saveFileDialog = new SaveFileDialog(); |
|
18 |
|
|
19 |
saveFileDialog.FileName = "Document"; // Default file name |
|
20 |
saveFileDialog.DefaultExt = ".txt"; // Default file extension |
|
21 |
saveFileDialog.Filter = "Csv documents (.Csv)|*.csv|Excel(2017~2019)Worksheets|*.xlsx"; // Filter files by extension |
|
22 |
|
|
23 |
|
|
24 |
if (saveFileDialog.ShowDialog() == true) |
|
25 |
{ |
|
26 |
|
|
27 |
|
|
28 |
if (!string.IsNullOrWhiteSpace(saveFileDialog.FileName)) |
|
29 |
{ |
|
30 |
|
|
31 |
var extension = new System.IO.FileInfo(saveFileDialog.FileName).Extension; |
|
32 |
|
|
33 |
if (extension == ".xlsx" || extension == ".csv") |
|
34 |
{ |
|
35 |
|
|
36 |
var headers = new List<HeaderMember> |
|
37 |
{ |
|
38 |
new HeaderMember{HeaderName = "ServiceID", Property = "ServiceID" }, |
|
39 |
new HeaderMember{HeaderName = "ConvertID", Property = "ConvertID" }, |
|
40 |
new HeaderMember{HeaderName = "ProjectNumber", Property = "ProjectNumber" }, |
|
41 |
new HeaderMember{HeaderName = "ConvertState", Property = "ConvertState" }, |
|
42 |
new HeaderMember{HeaderName = "CurrentPageNo", Property = "CurrentPageNo" }, |
|
43 |
new HeaderMember{HeaderName = "TotalPage", Property = "TotalPage" }, |
|
44 |
new HeaderMember{HeaderName = "OriginfilePath", Property = "OriginfilePath" }, |
|
45 |
new HeaderMember{HeaderName = "ConvertPath", Property = "ConvertPath" }, |
|
46 |
new HeaderMember{HeaderName = "CreateTime", Property = "CreateTime" }, |
|
47 |
new HeaderMember{HeaderName = "Exception", Property = "Exception" }, |
|
48 |
new HeaderMember{HeaderName = "ProcessorAffinity", Property = "ProcessorAffinity" }, |
|
49 |
new HeaderMember{HeaderName = "ReConverter", Property = "ReConverter" }, |
|
50 |
new HeaderMember{HeaderName = "UniqueKey", Property = "UniqueKey" } |
|
51 |
}; |
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
DataExportExcel(saveFileDialog.FileName, "Hello world", FilterConvertSource, headers); |
|
56 |
//_dataExport.DataExportExcel(saveFileDialog.FileName, saveFileDialog.FileName.ToString(), Projectsource, headers); |
|
57 |
//GemBoxFileSave(obj); |
|
58 |
} |
|
59 |
} |
|
60 |
} |
|
61 |
} |
|
62 |
|
|
63 |
|
|
64 |
public void DataExportExcel<T>(string SaveFile, string SheetName, IEnumerable<T> collections, List<HeaderMember> headerMembers) |
|
65 |
{ |
|
66 |
|
|
67 |
try |
|
68 |
{ |
|
69 |
SpreadsheetInfo.SetLicense("EXK0-W4HZ-N518-IMEW"); |
|
70 |
// SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");//프리버전은 15줄까지만 가능 |
|
71 |
|
|
72 |
var workbook = new ExcelFile(); |
|
73 |
var worksheet = workbook.Worksheets.Add(SheetName); |
|
74 |
|
|
75 |
// IEnumerable<T> collections를 for문 |
|
76 |
for (int rowNumber = 0; rowNumber < collections.Count(); rowNumber++)//1857 양이 너무 많아서 빨리 엑셀출력 누르면 숫자가 감소됨 차분히 기다리면 숫자 늘어남 |
|
77 |
{ |
|
78 |
// index에 해당하는 row를 가져온다. |
|
79 |
object row = collections.ElementAt(rowNumber);//우왕좌왕하게 모든 프로퍼티 있음 생성자 프로퍼티도 있음 |
|
80 |
|
|
81 |
//row의 properties를 가져옴. |
|
82 |
var rowPropInfos = row.GetType().GetProperties();//행렬로 보기좋게 프로퍼티 나옴 |
|
83 |
|
|
84 |
/// 지정된 header에 대해서 프로퍼티에 해당하는 값을 가져와서 cell에 출력 |
|
85 |
for (int i = 0; i < headerMembers.Count(); i++)//13 |
|
86 |
{ |
|
87 |
var prop = rowPropInfos.Where(x => x.Name == headerMembers[i].Property);//prop 0으로 나옴 |
|
88 |
if (headerMembers[i].Property == "CreateTime") |
|
89 |
{ |
|
90 |
worksheet.Cells[rowNumber + 1, i].Value = prop.First().GetValue(row).ToString(); |
|
91 |
} |
|
92 |
else if (prop.Count() > 0) |
|
93 |
{ |
|
94 |
worksheet.Cells[rowNumber + 1, i].Value = prop.First().GetValue(row); |
|
95 |
} |
|
96 |
} |
|
97 |
} |
|
98 |
|
|
99 |
int k = 0; |
|
100 |
foreach (var HeadInfo in headerMembers) |
|
101 |
{ |
|
102 |
worksheet.Cells[0, k].Value = HeadInfo.HeaderName; |
|
103 |
k++; |
|
104 |
} |
|
105 |
|
|
106 |
int columnCount = worksheet.CalculateMaxUsedColumns(); |
|
107 |
for (int i = 0; i < columnCount; i++) |
|
108 |
{ |
|
109 |
worksheet.Columns[i].AutoFit(); |
|
110 |
} |
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
var header = new CellStyle();//헤더 |
|
115 |
|
|
116 |
header.FillPattern.SetSolid(SpreadsheetColor.FromArgb(0, 100, 220)); |
|
117 |
header.Font.Weight = ExcelFont.BoldWeight; |
|
118 |
header.HorizontalAlignment = HorizontalAlignmentStyle.Center; |
|
119 |
header.VerticalAlignment = VerticalAlignmentStyle.Center; |
|
120 |
//worksheet.Cells.GetSubrange("A1:M1").Style = header; |
|
121 |
worksheet.Cells.GetSubrangeAbsolute(0, 0, collections.Count(), headerMembers.Count() - 1).Style = header; |
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
var style = new CellStyle(); |
|
126 |
//style.Borders.SetBorders(MultipleBorders.Right | MultipleBorders.Top | MultipleBorders.Left | MultipleBorders.Bottom, SpreadsheetColor.FromArgb(140, 120, 50), LineStyle.Thick);//바깥쪽 border 두껍게 성공 |
|
127 |
style.Borders.SetBorders(MultipleBorders.Outside, SpreadsheetColor.FromArgb(140, 120, 50), LineStyle.Thick);//바깥쪽 border 두껍게 성공 |
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
style.HorizontalAlignment = HorizontalAlignmentStyle.Center; |
|
132 |
style.VerticalAlignment = VerticalAlignmentStyle.Center; |
|
133 |
style.Font.Color = SpreadsheetColor.FromArgb(75, 60, 50);//글자 색갈 |
|
134 |
style.FillPattern.SetSolid(SpreadsheetColor.FromArgb(126, 239, 239));//cell 색갈 |
|
135 |
style.Font.Name = "맑은 고딕"; |
|
136 |
style.Font.Size = 9 * 20; |
|
137 |
worksheet.Cells.GetSubrangeAbsolute(0, 0, collections.Count(), headerMembers.Count() - 1).Style = style; |
|
138 |
//worksheet.Cells.GetSubrange("A1:M1858").Style = style; |
|
139 |
|
|
140 |
for (int line = 0; line < collections.Count() - 1; line++) //바깥border과 안쪽 얇은 border 같이 쓰면 안됨 |
|
141 |
{ |
|
142 |
//for (int j = 0; j < 14 - 1 - 1; j++) |
|
143 |
for (int j = 0; j < headerMembers.Count() - 1 - 1; j++) |
|
144 |
{ |
|
145 |
worksheet.Cells[line + 1, j + 1].Style.Borders.SetBorders(MultipleBorders.Outside, SpreadsheetColor.FromArgb(140, 120, 50), LineStyle.Thin);//안쪽 (1,1) ~ (마지막-1,마지막-1) 가운데 성공 |
|
146 |
} |
|
147 |
} |
|
148 |
|
|
149 |
|
|
150 |
for (int line = 0; line < collections.Count(); line++) //바깥border과 안쪽 얇은 border 같이 쓰면 안됨 |
|
151 |
{//Projectsource.Count() = 761개 ( 0 ~ 760 ) |
|
152 |
for (int j = 0; j < headerMembers.Count() - 1; j++) |
|
153 |
{ |
|
154 |
worksheet.Cells[0, j].Style.Borders.SetBorders(MultipleBorders.Right, SpreadsheetColor.FromArgb(140, 120, 50), LineStyle.Thin);//(0,0) ~ (0,마지막-1) 위 성공 |
|
155 |
|
|
156 |
worksheet.Cells[collections.Count(), j].Style.Borders.SetBorders(MultipleBorders.Right, SpreadsheetColor.FromArgb(140, 120, 50), LineStyle.Thin);//(마지막,0) ~ (마지막,마지막-1) 아래 성공 |
|
157 |
} |
|
158 |
|
|
159 |
worksheet.Cells[line, 0].Style.Borders.SetBorders(MultipleBorders.Bottom, SpreadsheetColor.FromArgb(140, 120, 50), LineStyle.Thin);//(0,0) ~ (마지막-1,0) 왼쪽 성공 |
|
160 |
|
|
161 |
worksheet.Cells[line, headerMembers.Count() - 1].Style.Borders.SetBorders(MultipleBorders.Bottom, SpreadsheetColor.FromArgb(140, 120, 50), LineStyle.Thin);//(0,마지막) ~ (마지막-1,마지막) 오른쪽 성공 |
|
162 |
|
|
163 |
} |
|
164 |
|
|
165 |
workbook.Save(SaveFile);//파일 열었던 파일로 저장 성공 |
|
166 |
MessageBox.Show("성공"); |
|
167 |
} |
|
168 |
|
|
169 |
catch (Exception ex) |
|
170 |
{ |
|
171 |
Console.WriteLine("예외 발생!! 메세지: {0}", ex.Message); |
|
172 |
} |
|
173 |
} |
|
174 |
|
|
175 |
} |
|
176 |
} |
|
177 |
public class HeaderMember |
|
178 |
{ |
|
179 |
public string HeaderName { get; set; } |
|
180 |
public string Property { get; set; } |
|
181 |
} |
내보내기 Unified diff