3
3
using System . Linq ;
4
4
using System . Text ;
5
5
using System . Threading . Tasks ;
6
+ using System . Drawing ;
6
7
using MigraDoc . DocumentObjectModel ;
7
8
using MigraDoc . Rendering ;
8
9
using MigraDoc . DocumentObjectModel . Tables ;
@@ -18,7 +19,7 @@ namespace kibom
18
19
{
19
20
class Output
20
21
{
21
- public static void OutputXLSX ( List < DesignatorGroup > groups , HeaderBlock header , string file , string template )
22
+ public static void OutputXLSX ( string path , List < DesignatorGroup > groups , HeaderBlock header , string file , string template , string footer , bool nfl )
22
23
{
23
24
ExcelPackage p = null ;
24
25
ExcelWorksheet ws ;
@@ -40,11 +41,11 @@ public static void OutputXLSX(List<DesignatorGroup> groups, HeaderBlock header,
40
41
table_x -- ;
41
42
foreach ( DesignatorGroup g in groups )
42
43
{
43
- // check for groups that are entire "no part"
44
+ // check for groups that are entirly "no part" or "no fit "
44
45
bool all_no_part = true ;
45
46
foreach ( Component c in g . comp_list )
46
47
{
47
- if ( ! c . no_part )
48
+ if ( ! c . no_part && ! c . no_fit )
48
49
all_no_part = false ;
49
50
}
50
51
if ( all_no_part )
@@ -64,11 +65,7 @@ public static void OutputXLSX(List<DesignatorGroup> groups, HeaderBlock header,
64
65
ws . Cells [ row , table_x + 1 ] . Value = g . designator ;
65
66
ws . Cells [ row , table_x + 2 ] . Value = g . comp_list . Count . ToString ( ) + " value(s)" ;
66
67
}
67
- ws . Cells [ row , table_x + 1 ] . Style . Font . Bold = true ;
68
- r = ws . Cells [ row , table_x + 1 , row , table_x + 6 ] ;
69
- r . Style . Fill . PatternType = ExcelFillStyle . Solid ;
70
- r . Style . Fill . BackgroundColor . SetColor ( System . Drawing . Color . LightGray ) ;
71
- r . Style . Border . BorderAround ( ExcelBorderStyle . Thin , System . Drawing . Color . LightGray ) ;
68
+ XLXSStyleHeader ( row , table_x , ref ws ) ;
72
69
row ++ ;
73
70
74
71
// component list
@@ -100,10 +97,129 @@ public static void OutputXLSX(List<DesignatorGroup> groups, HeaderBlock header,
100
97
r . Style . VerticalAlignment = ExcelVerticalAlignment . Top ;
101
98
r . Style . WrapText = true ;
102
99
100
+ if ( nfl ) // no fit part list
101
+ XLXSNoFitList ( ref row , table_x , ref ws , groups ) ;
102
+
103
+ XLXSFooter ( path , footer , ref ws , ref row ) ;
104
+
105
+ // generate output file
103
106
byte [ ] bin = p . GetAsByteArray ( ) ;
104
107
File . WriteAllBytes ( file , bin ) ;
105
108
}
106
109
110
+ // Excel has a limitation where it can't do auto cell sizing with merged cells, so we have to calculate it manually
111
+ // https://stackoverflow.com/questions/41639278/autofit-row-height-of-merged-cell-in-epplus
112
+ private static double XLXSMeasureTextHeight ( string text , ExcelFont font , double width )
113
+ {
114
+ if ( string . IsNullOrEmpty ( text ) )
115
+ return 0.0 ;
116
+ var bitmap = new Bitmap ( 1 , 1 ) ;
117
+ var graphics = Graphics . FromImage ( bitmap ) ;
118
+
119
+ var pixelWidth = Convert . ToInt32 ( width * 7.5 ) ; // 7.5 pixels per excel column width
120
+ var drawingFont = new System . Drawing . Font ( font . Name , font . Size ) ;
121
+ var size = graphics . MeasureString ( text , drawingFont , pixelWidth ) ;
122
+
123
+ // 72 DPI and 96 points per inch. Excel height in points with max of 409 per Excel requirements.
124
+ return Math . Min ( Convert . ToDouble ( size . Height ) * 72 / 96 , 409 ) ;
125
+ }
126
+
127
+ private static void XLXSStyleHeader ( int row , int table_x , ref ExcelWorksheet ws )
128
+ {
129
+ ExcelRange r ;
130
+ ws . Cells [ row , table_x + 1 ] . Style . Font . Bold = true ;
131
+ r = ws . Cells [ row , table_x + 1 , row , table_x + 6 ] ;
132
+ r . Style . Fill . PatternType = ExcelFillStyle . Solid ;
133
+ r . Style . Fill . BackgroundColor . SetColor ( System . Drawing . Color . LightGray ) ;
134
+ r . Style . Border . BorderAround ( ExcelBorderStyle . Thin , System . Drawing . Color . LightGray ) ;
135
+ }
136
+
137
+ private static void XLXSNoFitList ( ref int row , int table_x , ref ExcelWorksheet ws , List < DesignatorGroup > groups )
138
+ {
139
+ row ++ ;
140
+ // header
141
+ ws . Cells [ row , table_x + 1 ] . Value = "No fit" ;
142
+ XLXSStyleHeader ( row , table_x , ref ws ) ;
143
+ row ++ ;
144
+
145
+ StringBuilder sb = new StringBuilder ( ) ;
146
+ foreach ( DesignatorGroup g in groups )
147
+ {
148
+ bool no_fit_found = false ;
149
+ bool newline = true ;
150
+ foreach ( Component c in g . comp_list )
151
+ {
152
+ if ( c . no_fit )
153
+ {
154
+ no_fit_found = true ;
155
+ if ( ! newline )
156
+ sb . Append ( ", " ) ;
157
+ newline = false ;
158
+ sb . Append ( c . reference ) ;
159
+ }
160
+ }
161
+ if ( no_fit_found )
162
+ sb . Append ( Environment . NewLine ) ;
163
+ }
164
+
165
+ string s = sb . ToString ( ) ;
166
+ if ( string . IsNullOrEmpty ( s ) )
167
+ s = "None" ;
168
+
169
+ ws . Cells [ row , table_x + 1 , row , table_x + 6 ] . Merge = true ;
170
+ ws . Cells [ row , table_x + 1 ] . Style . WrapText = true ;
171
+ double width = 0 ;
172
+ for ( int i = table_x + 1 ; i < table_x + 6 ; i ++ )
173
+ width += ws . Column ( i ) . Width ;
174
+ double height = XLXSMeasureTextHeight ( s + Environment . NewLine + "." , ws . Cells [ row , table_x + 1 ] . Style . Font , width ) ;
175
+ ws . Row ( row ) . Height = height ;
176
+ ws . Cells [ row , table_x + 1 ] . Value = s ;
177
+ }
178
+
179
+ private static void XLXSFooter ( string path , string footer , ref ExcelWorksheet ws , ref int row )
180
+ {
181
+ // footer
182
+ if ( ! string . IsNullOrEmpty ( footer ) )
183
+ {
184
+ //ws.Row(row).PageBreak = true;
185
+ row ++ ;
186
+
187
+ ExcelPackage footer_p = null ;
188
+ ExcelWorksheet footer_ws ;
189
+
190
+ if ( ! File . Exists ( footer ) )
191
+ {
192
+ footer = path + footer ;
193
+ if ( ! File . Exists ( footer ) )
194
+ throw new Exception ( "File not found " + footer ) ;
195
+ }
196
+ FileInfo fi = new FileInfo ( footer ) ;
197
+ footer_p = new ExcelPackage ( fi ) ;
198
+ footer_ws = footer_p . Workbook . Worksheets [ 1 ] ;
199
+
200
+ int footer_row = 1 ;
201
+ int footer_empty_coint = 0 ;
202
+ do
203
+ {
204
+ bool empty_row = true ;
205
+ for ( int x = 1 ; x < 6 ; x ++ )
206
+ {
207
+ if ( footer_ws . Cells [ footer_row , x ] . Value != null )
208
+ empty_row = false ;
209
+ }
210
+ footer_ws . Cells [ footer_row , 1 , footer_row , 6 ] . Copy ( ws . Cells [ row , 1 , row , 6 ] ) ;
211
+ footer_row ++ ;
212
+ row ++ ;
213
+
214
+ if ( empty_row )
215
+ footer_empty_coint ++ ;
216
+ else
217
+ footer_empty_coint = 0 ;
218
+
219
+ } while ( footer_empty_coint < 2 ) ;
220
+ }
221
+ }
222
+
107
223
private static void XLXSLoadSheet ( string template , ref ExcelPackage p , HeaderBlock header , out ExcelWorksheet ws , out int table_x , out int table_y )
108
224
{
109
225
if ( ! File . Exists ( template ) )
@@ -125,6 +241,9 @@ private static void XLXSLoadSheet(string template, ref ExcelPackage p, HeaderBlo
125
241
case "(title)" :
126
242
ws . Cells [ y , x ] . Value = header . title ;
127
243
break ;
244
+ case "(number)" :
245
+ ws . Cells [ y , x ] . Value = header . comment1 ;
246
+ break ;
128
247
case "(documentnumber)" :
129
248
ws . Cells [ y , x ] . Value = "not implemented" ;
130
249
break ;
@@ -199,8 +318,8 @@ private static void XLSXCreateDefaultSheet(ref ExcelPackage p, HeaderBlock heade
199
318
r . Style . Fill . BackgroundColor . SetColor ( System . Drawing . Color . Black ) ;
200
319
r . Style . Font . Color . SetColor ( System . Drawing . Color . White ) ;
201
320
}
202
-
203
- public static void OutputTSV ( List < DesignatorGroup > groups , HeaderBlock header , string file )
321
+
322
+ public static void OutputTSV ( string path , List < DesignatorGroup > groups , HeaderBlock header , string file )
204
323
{
205
324
Console . WriteLine ( "Generating " + file + "..." ) ;
206
325
using ( StreamWriter sw = new StreamWriter ( file ) )
@@ -308,7 +427,7 @@ private static void PrettyMeasureWidths(List<DesignatorGroup> groups,
308
427
precision_width = Math . Min ( precision_width , 20 ) ;
309
428
}
310
429
311
- public static void OutputPretty ( List < DesignatorGroup > groups , HeaderBlock header , string file )
430
+ public static void OutputPretty ( string path , List < DesignatorGroup > groups , HeaderBlock header , string file )
312
431
{
313
432
Console . WriteLine ( "Generating " + file + "..." ) ;
314
433
using ( StreamWriter sw = new StreamWriter ( file ) )
@@ -408,7 +527,7 @@ public static void OutputPretty(List<DesignatorGroup> groups, HeaderBlock header
408
527
}
409
528
}
410
529
411
- public static void OutputPDF ( List < DesignatorGroup > groups , HeaderBlock header , string file , bool rtf = false )
530
+ public static void OutputPDF ( string path , List < DesignatorGroup > groups , HeaderBlock header , string file , bool rtf = false )
412
531
{
413
532
Console . WriteLine ( "Generating " + file + "..." ) ;
414
533
0 commit comments