-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathExcelTemplateOP.cs
1229 lines (1187 loc) · 65.8 KB
/
ExcelTemplateOP.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using DBUtil;
using System.Collections;
using System.Xml;
using System.Data;
using System.Reflection;
using NPOI.SS;
using NPOI.HSSF;
using NPOI;
using NPOI.HSSF.UserModel;
using System.IO;
using NPOI.SS.UserModel;
using System.Text.RegularExpressions;
using ImageCode;
namespace ExcelCtr
{
internal class ExcelTemplateOP
{
/// <summary>使用配置文件和哈希表(携带参数)初始化</summary>
/// <param name="confPath">配置文件的绝对路径,以.xml结尾如:d:\demo.xml</param>
/// <param name="ht">携带的参数</param>
public ExcelTemplateOP(string templateConfPath, Hashtable ht)
{
this.templatePath = templateConfPath.Substring(0, templateConfPath.LastIndexOf('.')) + ".xls";
ReadConf(templateConfPath);
PrepareData(ht);
}
/// <summary>初始化配置文件</summary>
/// <param name="ht">初始化配置携带的参数</param>
private void PrepareData(Hashtable ht)
{
//先将声明好的参数传进来
this.parameters.ForEach((i) =>
{
if (ht[i.name] != null)
{
i.value = ht[i.name] ?? "";
ht.Remove(i.name);
}
});
//将未声明的参数也传递进来
ht.Keys.Cast<string>().ToList<string>().ForEach((i) =>
{
this.parameters.Add(new parameter()
{
name = i,
receive = i,
type = (ht[i] ?? "").GetType().ToString(),
value = ht[i] ?? ""
});
});
//初始化idb
this.idbs.ForEach((i) =>
{
i.connstr_value = (i.connstr_conf ?? "").Trim(' ');
if (i.connstr_value.StartsWith("parameters."))
{
i.connstr_value = this.parameters
.FirstOrDefault<parameter>(ii => ii.name == i.connstr_value.Replace("parameters.", ""))
.value.ToString();
}
i.dbtype_value = (i.dbtype_conf ?? "").Trim(' ');
if (i.dbtype_value.StartsWith("parameters."))
{
i.dbtype_value = this.parameters
.FirstOrDefault<parameter>(ii => ii.name == i.dbtype_value.Replace("parameters.", ""))
.value.ToString();
}
i.value = IDBFactory.CreateIDB(i.connstr_value, i.dbtype_value);
});
//初始化计算结果表
this.caldts.ForEach((i) =>
{
//先拿到iDb
i.useidb_conf = i.useidb_conf ?? "";
if (i.useidb_conf.StartsWith("parameters."))
{
i.useidb_value = this.parameters
.FirstOrDefault<parameter>(ii => ii.name == i.useidb_conf.Replace("parameters.", ""))
.value as IDbAccess;
}
else if (i.useidb_conf.StartsWith("idbs."))
{
i.useidb_value = this.idbs
.FirstOrDefault<idb>(ii => ii.name == i.useidb_conf.Replace("idbs.", ""))
.value as IDbAccess;
}
//获取para
i.listpara.ForEach(ii =>
{
ii.name = ii.name ?? "";
if (ii.name.StartsWith("parameters."))
{
parameter p = this.parameters.Single<parameter>(
iii => iii.name == ii.name.Replace("parameters.", ""));
ii.receive = p.receive;
ii.type = p.type;
ii.value = p.value;
}
});
//进行计算
i.value = i.useidb_value
.GetDataTable(
string.Format(i.sqltmp,
i.listpara.Select<parameter, string>(ii => (ii.value ?? "").ToString()).ToArray()));
});
//初始化计算项
this.calitems.ForEach((i) =>
{
if (string.IsNullOrWhiteSpace(i.from))
{
//根据sql语句计算
#region
//1.先拿到iDb
i.useidb_conf = i.useidb_conf ?? "";
if (i.useidb_conf.StartsWith("parameters."))
{
parameter p = this.parameters
.FirstOrDefault<parameter>(ii => ii.name == i.useidb_conf.Replace("parameters.", ""));
if (p == null) throw new Exception("未找到数据库访问对象:" + i.useidb_conf);
i.useidb_value = p.value as IDbAccess;
}
else if (i.useidb_conf.StartsWith("idbs."))
{
idb p = this.idbs
.FirstOrDefault<idb>(ii => ii.name == i.useidb_conf.Replace("idbs.", ""));
if (p == null) throw new Exception("未找到数据库访问对象:" + i.useidb_conf);
i.useidb_value = p.value as IDbAccess;
}
//2.获取para
i.listpara.ForEach(ii =>
{
ii.name = ii.name ?? "";
if (ii.name.StartsWith("parameters."))
{
parameter p = this.parameters.Single<parameter>(
iii => iii.name == ii.name.Replace("parameters.", ""));
if (p == null) throw new Exception("未找到参数:" + ii.name);
ii.receive = p.receive;
ii.type = p.type;
ii.value = p.value;
}
});
//3.进行计算
i.value = i.useidb_value
.GetFirstColumnString(
string.Format(i.sqltmp,
i.listpara.Select<parameter, string>(ii => ii.value.ToString()).ToArray()));
#endregion
}
else
{
//从计算表中引用的
#region
string from = i.from.Trim();
if (!from.StartsWith("caldts."))
{
throw new Exception(string.Format("计算项\"{0}\"的from属性\"{1}\"必须以\"caldts.\"开头", i.name, i.from));
}
string[] arr = from.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
if (arr.Length < 3) throw new Exception(string.Format("计算项\"{0}\"的from属性\"{1}\"不符合规则,参照:\"caldts.JSYDYS.SJRQ\"", i.name, i.from));
caldt dt = this.caldts.FirstOrDefault<caldt>(j => j.name == arr[1]);
if (dt == null) throw new Exception(string.Format("计算项\"{0}\"的from属性\"{1}\"引用的计算表\"{2}\"未找到", i.name, i.from, arr[1]));
if (!dt.value.Columns.Contains(arr[2])) throw new Exception(string.Format("计算项\"{0}\"的from属性\"{1}\"引用的计算表\"{2}\"中未找到列\"{3}\"", i.name, i.from, arr[1], arr[2]));
DataRow[] rows = dt.value.Select();
if (!string.IsNullOrWhiteSpace(i.filter))
{
//根据filter筛选符合条件的行
rows = dt.value.Select(i.filter);
}
if (!string.IsNullOrWhiteSpace(i.fetch))
{
//根据fetch选取筛选后的行
if (!(i.fetch.Contains('[') &&
i.fetch.Contains(']') &&
i.fetch.Contains(':')))
{
throw new Exception(string.Format("计算项\"{0}\"的fetch属性\"{1}\"不符合规则,必须包含'[',']',':'三个字符,参照:\"[0:5]\",见python字符串截取语法", i.name, i.fetch));
}
List<DataRow> list = new List<DataRow>();
string[] fetcharr = i.fetch.Replace("[", "").Replace("]", "").Split(new char[] { ':' }, StringSplitOptions.None);
if (fetcharr.Length != 2)
{
throw new Exception(string.Format("计算项\"{0}\"的fetch属性\"{1}\"不符合规则,参照:\"[0:5]\",见python字符串截取语法", i.name, i.fetch));
}
if (string.IsNullOrWhiteSpace(fetcharr[0])) fetcharr[0] = "0";
if (string.IsNullOrWhiteSpace(fetcharr[1])) fetcharr[1] = rows.Length.ToString();
int start = int.Parse(fetcharr[0]);
int end = int.Parse(fetcharr[1]);
while (start < 0)
{
start += rows.Length;
}
while (end < 0)
{
end += rows.Length;
}
if (!(end < start || start > rows.Length - 1))
{
for (var k = start; k < rows.Length && k < end; k++)
{
list.Add(rows[k]);
}
}
rows = list.ToArray();
}
//根据聚合标志得到聚合后结果
if (string.IsNullOrWhiteSpace(i.aggregate))
{
i.aggregate = "str_join(,)";
}
if (i.aggregate.StartsWith("str_join"))
{
//字符串拼接
string joinstr = i.aggregate.Replace("str_join", "").Replace("(", "").Replace(")", "");
string _t = "";
for (var k = 0; k < rows.Length; k++)
{
string str = (rows[k][arr[2]] ?? "").ToString();
if (string.IsNullOrWhiteSpace(str)) continue;
if (k == 0)
{
_t += str;
}
else
{
_t += joinstr + str;
}
}
i.value = _t;
}
else if (i.aggregate == "sum")
{
//求和计算
double init = 0;
for (var k = 0; k < rows.Length; k++)
{
string str = (rows[k][arr[2]] ?? "").ToString();
double _t;
if (double.TryParse(str, out _t))
{
init += _t;
}
}
i.value = init.ToString();
}
else if (i.aggregate == "avg")
{
//不能转化为数字的不参与计算
double init = 0;
int len = 0;
for (var k = 0; k < rows.Length; k++)
{
string str = (rows[k][arr[2]] ?? "").ToString();
double _t;
if (double.TryParse(str, out _t))
{
len++;
init += _t;
}
}
i.value = (init / len).ToString();
}
else if (i.aggregate == "min")
{
//不能转化为数字的不参与计算
double init = 0;
for (var k = 0; k < rows.Length; k++)
{
string str = rows[k][arr[2]].ToString();
double _t;
if (double.TryParse(str, out _t))
{
init = init > _t ? _t : init;
}
}
i.value = init.ToString();
}
else if (i.aggregate == "max")
{
//不能转化为数字的不参与计算
double init = 0;
for (var k = 0; k < rows.Length; k++)
{
string str = rows[k][arr[2]].ToString();
double _t;
if (double.TryParse(str, out _t))
{
init = init > _t ? init : _t;
}
}
i.value = init.ToString();
}
else if (i.aggregate == "count")
{
//求数量
i.value = rows.Length.ToString();
}
#endregion
}
});
}
/// <summary>读取配置文件</summary>
/// <param name="confPath"></param>
private void ReadConf(string confPath)
{
string str = System.IO.File.ReadAllText(confPath, Encoding.UTF8);
XmlDocument doc = new XmlDocument();
doc.LoadXml(str);
XmlElement root = doc.DocumentElement;
if (root.Name != "WorkBook")
{
throw new Exception("配置文件的根节点必须是WorkBook");
}
if (string.IsNullOrEmpty(root.GetAttribute("version")))
{
throw new Exception("必须指定配置文件的版本");
}
XmlNodeList list = root.ChildNodes;
IEnumerable<XmlElement> li = list.OfType<XmlElement>();
li.ToList<XmlElement>().ForEach(i =>
{
int parameters_count = 0, idbs_count = 0, calitems_count = 0, caldts_count = 0, fastsheets_count = 0, sheets_count = 0;
if (i.Name == "parameters" && parameters_count == 0)
{
parameters_count++;
i.ChildNodes
.OfType<XmlElement>()
.Where<XmlElement>(ii => ii.Name == "parameter")
.ToList<XmlElement>()
.ForEach((iii) =>
{
this.parameters.Add(new parameter()
{
name = iii.GetAttribute("name"),
receive = iii.GetAttribute("receive"),
type = iii.GetAttribute("type")
});
});
}
if (i.Name == "idbs" && idbs_count == 0)
{
idbs_count++;
i.ChildNodes.OfType<XmlElement>()
.Where<XmlElement>(ii => ii.Name == "idb")
.ToList<XmlElement>()
.ForEach((iii) =>
{
idb idb = new idb();
idb.name = iii.GetAttribute("name");
idb.connstr_conf = iii.ChildNodes
.OfType<XmlElement>()
.FirstOrDefault<XmlElement>(iiii => iiii.Name == "connstr")
.GetAttribute("value");
idb.dbtype_conf = iii.ChildNodes
.OfType<XmlElement>()
.FirstOrDefault<XmlElement>(iiii => iiii.Name == "dbtype")
.GetAttribute("value");
this.idbs.Add(idb);
});
}
if (i.Name == "calitems" && calitems_count == 0)
{
calitems_count++;
i.ChildNodes.OfType<XmlElement>()
.Where<XmlElement>(ii => ii.Name == "calitem")
.ToList<XmlElement>()
.ForEach((iii) =>
{
calitem cal = new calitem();
this.calitems.Add(cal);
cal.name = iii.GetAttribute("name");
if (iii.HasAttribute("from"))
{
//该计算项是从计算表中引入的值
cal.from = iii.GetAttribute("from");
cal.fetch = iii.GetAttribute("fetch");
cal.filter = iii.GetAttribute("filter");
cal.aggregate = iii.GetAttribute("aggregate");
}
else
{
//该计算项是根据sql语句计算得来的
iii.ChildNodes.OfType<XmlElement>()
.ToList<XmlElement>()
.ForEach((iiii) =>
{
if (iiii.Name == "sqltmp" && string.IsNullOrEmpty(cal.sqltmp))
{
cal.sqltmp = iiii.InnerText.Trim(' ', '\t', '\r', '\n');
}
if (iiii.Name == "useidb" && string.IsNullOrEmpty(cal.useidb_conf))
{
cal.useidb_conf = iiii.GetAttribute("value");
}
if (iiii.Name == "usepara")
{
parameter p = this.parameters.FirstOrDefault<parameter>(para => para.name == iiii.GetAttribute("value").Replace("parameters.", ""));
if (p == null) throw new Exception("未找到参数:" + iiii.GetAttribute("value"));
cal.listpara.Add(p);
}
});
}
});
}
if (i.Name == "caldts" && caldts_count == 0)
{
caldts_count++;
i.ChildNodes.OfType<XmlElement>()
.Where<XmlElement>(ii => ii.Name == "caldt")
.ToList<XmlElement>()
.ForEach((iii) =>
{
caldt cal = new caldt();
this.caldts.Add(cal);
cal.name = iii.GetAttribute("name");
iii.ChildNodes.OfType<XmlElement>()
.ToList<XmlElement>()
.ForEach((iiii) =>
{
if (iiii.Name == "sqltmp" && string.IsNullOrEmpty(cal.sqltmp))
{
cal.sqltmp = iiii.InnerText.Trim(' ', '\t', '\r', '\n');
}
if (iiii.Name == "useidb" && string.IsNullOrEmpty(cal.useidb_conf))
{
cal.useidb_conf = iiii.GetAttribute("value");
}
if (iiii.Name == "usepara")
{
parameter p = this.parameters.FirstOrDefault<parameter>(para => para.name == iiii.GetAttribute("value").Replace("parameters.", ""));
if (p == null) throw new Exception("未找到参数:" + iiii.GetAttribute("value"));
cal.listpara.Add(p);
}
});
});
}
if (i.Name == "fastsheets" && fastsheets_count == 0)
{
fastsheets_count++;
this.fastsheets = i;
}
if (i.Name == "sheets" && sheets_count == 0)
{
sheets_count++;
this.sheets = i;
}
});
}
/// <summary>将结果写入excel文件</summary>
/// <param name="filePath"></param>
public void Write(string destfilepath)
{
if (sheets == null && fastsheets == null) throw new Exception("模板文件【" + templatePath.Substring(0, templatePath.LastIndexOf('.')) + ".xml" + "】缺少sheets节点或fastsheets节点");
if (fastsheets != null)
{
#region 优先解析fastsheets
string useds = fastsheets.GetAttribute("useds");
DataSet ds = null;
List<string> SheetHeaders = new List<string>();
List<string> combineColIndexs = new List<string>();
#region 首先装载dataset
if (!string.IsNullOrWhiteSpace(useds))
{
//如果fastsheets使用了useds属性,就是用useds属性装载dataset
useds = useds.Trim(' ');
if (!useds.StartsWith("parameters.")) throw new Exception("模板文件【" + templatePath.Substring(0, templatePath.LastIndexOf('.')) + ".xml" + "】fastsheets节点的useds属性应该以\"parameters.\"开头");
ds = this.parameters.FirstOrDefault<parameter>(i => i.name == useds.Replace("parameters.", "")).value as DataSet;
if (ds == null) throw new Exception("未找到参数" + useds);
}
else
{
//如果fastsheets没使用useds属性,就用fastsheet节点装载dataset
ds = new DataSet();
fastsheets.ChildNodes.OfType<XmlElement>()
.Where<XmlElement>(i => i.Name == "fastsheet")
.ToList<XmlElement>()
.ForEach(i =>
{
string usedt = i.GetAttribute("usedt");
usedt = (usedt ?? "").Trim(' ');
string name = i.GetAttribute("name");
name = (name ?? "").Trim(' ');
if (string.IsNullOrWhiteSpace(usedt)) throw new Exception("模板文件【" + templatePath.Substring(0, templatePath.LastIndexOf('.')) + ".xml" + "】fastsheets节点下的fastsheet节点缺少usedt属性");
DataTable dt = null;
if (usedt.StartsWith("parameters."))
{
usedt = usedt.Replace("parameters.", "");
dt = (this.parameters.FirstOrDefault<parameter>(ii => ii.name == usedt) ?? new parameter()).value as DataTable;
if (dt == null) throw new Exception("未找到表:parameters." + usedt);
}
else if (usedt.StartsWith("caldts."))
{
usedt = usedt.Replace("caldts.", "");
dt = (this.caldts.FirstOrDefault<caldt>(ii => ii.name == usedt) ?? new caldt()).value as DataTable;
if (dt == null) throw new Exception("未找到表:caldts." + usedt);
}
if (name != "")
{
dt.TableName = name;
}
ds.Tables.Add(dt);
});
}
#endregion
#region 装载SheetHeaders和combineColIndexs
fastsheets.ChildNodes.OfType<XmlElement>()
.Where<XmlElement>(i => i.Name == "fastsheet")
.ToList<XmlElement>()
.ForEach(i =>
{
string title = "";
string colindex = "";
i.ChildNodes.OfType<XmlElement>()
.ToList<XmlElement>()
.ForEach(ii =>
{
if (ii.Name == "title")
{
if (!string.IsNullOrWhiteSpace(ii.GetAttribute("value")))
{
title = ii.GetAttribute("value");
}
}
else if (ii.Name == "combineColIndexs")
{
if (!string.IsNullOrWhiteSpace(ii.GetAttribute("value")))
{
string[] arrtmp = ii.GetAttribute("value").Trim(' ').Split(',');
for (int j = 0; j < arrtmp.Length; j++)
{
colindex += GetColIndex(arrtmp[j]).ToString() + ",";
}
colindex = colindex.Trim(',');
}
}
});
SheetHeaders.Add(title);
combineColIndexs.Add(colindex);
});
#endregion
//输出
FileStream fs = new FileStream(destfilepath, FileMode.Create);
MemoryStream stream = ExcelHelper.ExportDS(ds, SheetHeaders, combineColIndexs);
byte[] bs = stream.ToArray();
fs.Write(bs, 0, bs.Length);
fs.Flush();
fs.Close();
#endregion
}
else if (sheets != null)
{
#region 不存在fastsheets节点的情况下解析sheets节点
FileStream file = new FileStream(this.templatePath, FileMode.Open, FileAccess.Read);
HSSFWorkbook book = new HSSFWorkbook(file);
//解析sheets
sheets.OfType<XmlElement>()
.Where<XmlElement>(i => i.Name == "sheet")
.ToList<XmlElement>()
.ForEach(sheet =>
{
ISheet isheet = book.GetSheet(sheet.GetAttribute("name"));
if (isheet == null) throw new Exception("模板excel【" + this.templatePath + "】中找不到sheet:" + sheet.GetAttribute("name"));
XmlElement rowmass = sheet.ChildNodes.OfType<XmlElement>()
.Where<XmlElement>(i => i.Name == "rowmass")
.FirstOrDefault<XmlElement>();
if (rowmass != null)
{
int currentrow = 0;
rowmass.ChildNodes.OfType<XmlElement>()
.Where<XmlElement>(i => i.Name == "row")
.ToList<XmlElement>()
.ForEach(row =>
{
//拿到row节点下的model、position、index属性
string model = row.GetAttribute("model");
model = (model ?? "").Trim(' ');
string position = row.GetAttribute("position");
position = (position ?? "").Trim(' ');
string index = row.GetAttribute("index");
index = (index ?? "").Trim(' ');
if (string.IsNullOrWhiteSpace(model)
|| string.IsNullOrWhiteSpace(position)
|| string.IsNullOrWhiteSpace(index)
)
{
throw new Exception("标签row的属性model,position,index都不能为空");
}
//根据定位属性计算出当前应该操作的行索引
if (position == "absolute")
{
currentrow = int.Parse(index) - 1;
}
else if (position == "relative")
{
currentrow += int.Parse(index);
}
if (model == "single")
{
#region 单行操作,不涉及到循环行
row.ChildNodes.OfType<XmlElement>()
.Where<XmlElement>(i => i.Name == "coltmp")
.ToList<XmlElement>()
.ForEach(col =>
{
string colindex = col.GetAttribute("index");
string colcelltype = col.GetAttribute("celltype");
colindex = (colindex ?? "").Trim(' ');
if (string.IsNullOrWhiteSpace(colindex)) throw new Exception("coltmp标签的index属性不能为空!");
string colval = col.GetAttribute("value") ?? "";
string celltype = col.GetAttribute("type") ?? "";
//colval = colval.Trim(' ');
if (colval == "")
{
colval = isheet.GetRow(currentrow).GetCell(GetColIndex(colindex), MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue ?? "";
}
//colval = colval.Trim(' ');
if (colval != "")
{
string res = ParseVal(colval);
if (celltype.ToLower() == "number")
{
double res_double;
if (double.TryParse(res, out res_double))
{
isheet.GetRow(currentrow).GetCell(GetColIndex(colindex), MissingCellPolicy.CREATE_NULL_AS_BLANK).SetCellValue(res_double);
}
}
else
{
ICell cell = isheet.GetRow(currentrow).GetCell(GetColIndex(colindex), MissingCellPolicy.CREATE_NULL_AS_BLANK);
ICellStyle cellStyle = cell.CellStyle;
if (cellStyle == null)
{
cellStyle = book.CreateCellStyle();
cell.CellStyle = cellStyle;
}
cellStyle.WrapText = true;
res = res.Replace("\\r", "\r")
.Replace("\\n", "\n")
.Replace("\\t", "\t");
cell.SetCellValue(new HSSFRichTextString(res));
}
}
});
#endregion
}
else if (model == "cycle")
{
#region 循环行操作
string binddt = row.GetAttribute("binddt");
binddt = (binddt ?? "").Trim(' ');
DataTable curdt = null;//存储当前行绑定到的DataTable
#region 首先从caldts和parameters中解析出指定的DataTable
if (binddt.StartsWith("caldts."))
{
string binddt_tmp = binddt.Replace("caldts.", "");
caldt ctmp = this.caldts.Where<caldt>(i => i.name == binddt_tmp).FirstOrDefault<caldt>();
if (ctmp == null) throw new Exception("循环行导出中未找到计算表项:" + binddt);
curdt = ctmp.value;
}
else if (binddt.StartsWith("parameters."))
{
string binddt_tmp = binddt.Replace("parameters.", "");
parameter para = this.parameters.Where<parameter>(i => i.name == binddt_tmp).FirstOrDefault<parameter>();
if (para == null) throw new Exception("循环行导出中未找到参数项:" + binddt);
if (para.value != null && para.value is DataTable)
{
curdt = para.value as DataTable;
}
else if (para.value is IList)
{
IList li = para.value as IList;
Type type = para.value.GetType();
Type[] tys = type.GetGenericArguments();
if (tys.Length == 0) throw new Exception("从集合构建表过程中找不到类型参数,请检查要输出的集合数据!");
Type inner = tys[0];
PropertyInfo[] props = inner.GetProperties();
DataTable dt = new DataTable();
for (int i = 0; i < props.Length; i++)
{
dt.Columns.Add(props[i].Name);
}
for (int j = 0; j < li.Count; j++)
{
DataRow row_tmp = dt.NewRow();
for (int jj = 0; jj < props.Length; jj++)
{
row_tmp[dt.Columns[jj].ColumnName] = (props[jj].GetValue(li[j], null) ?? "").ToString();
}
dt.Rows.Add(row_tmp);
}
curdt = dt;
}
else
{
throw new Exception("无法根据参数加载参数项:" + binddt);
}
}
#endregion
if (curdt.Rows.Count == 0)
{
//如果绑定的DataTable中的记录数为0那就删除模板行
if (isheet.LastRowNum >= currentrow + 1)
{
isheet.ShiftRows(currentrow + 1, isheet.LastRowNum, -1, true, false);
}
else
{
isheet.RemoveRow(isheet.GetRow(currentrow));
}
currentrow--;
}
else
{
List<string[]> coltmps = new List<string[]>();//存储模板列的配置参数,格式:0-索引,1-模板配置值,2-模板合并控制键
#region 首先装载模板列的配置参数
row.ChildNodes.OfType<XmlElement>()
.Where<XmlElement>(i => i.Name == "coltmp")
.ToList<XmlElement>()
.ForEach(coltmp =>
{
string coltmp_index = coltmp.GetAttribute("index") ?? "";
string coltmp_value = coltmp.GetAttribute("value") ?? "";
string coltmp_merge = coltmp.GetAttribute("mergekey") ?? "";
string coltmp_celltype = coltmp.GetAttribute("celltype") ?? "";
//模板列索引不能为空
if (coltmp_index == "")
{
throw new Exception("循环行的列模板coltmp标签的属性index不能为空.");
}
//模板列的引用,配置里找不到就去excel对应单元格中去找
if (coltmp_value == "")
{
coltmp_value = isheet.GetRow(currentrow).GetCell(GetColIndex(coltmp_index), MissingCellPolicy.CREATE_NULL_AS_BLANK).StringCellValue ?? "";
}
//模板引用不为空的话就添加存储
if (coltmp_value != "")
{
coltmps.Add(new string[] { coltmp_index, coltmp_value, coltmp_merge, coltmp_celltype });
}
});
#endregion
int cyclestartrow_index = currentrow;//存储循环行的起始行索引
//根据模板行和记录数插入缺少的行
ExcelHelper.InsertRow(isheet, currentrow + 1, curdt.Rows.Count - 1, currentrow);
for (int i = 0; i < curdt.Rows.Count; i++)
{
//解析当前行
coltmps.ForEach(arr =>
{
string[] res = ParseCycleVal(arr, curdt, i);
//输出列格式支持数字类型 2018-3-30
ICell cell = isheet.GetRow(currentrow).GetCell(GetColIndex(arr[0]), MissingCellPolicy.CREATE_NULL_AS_BLANK);
if (arr[3] == "number")
{
double d_t;
if (double.TryParse(res[0], out d_t))
{
cell.SetCellValue(d_t);
}
}
else
{
ICellStyle cellStyle = cell.CellStyle;
if (cellStyle == null)
{
cell.CellStyle = cellStyle;
cellStyle = book.CreateCellStyle();
}
cellStyle.WrapText = true;
res[0] = res[0].Replace("\\r", "\r")
.Replace("\\n", "\n")
.Replace("\\t", "\t");
cell.SetCellValue(new HSSFRichTextString(res[0]));
}
//如果存在控制合并键值,就进行预合并处理
if (arr[2] != "")
{
//将合并控制键对应的值填充进当前数据表中
if (!curdt.Columns.Contains(arr[2]))
{
curdt.Columns.Add(new DataColumn(arr[2]));
}
curdt.Rows[i][arr[2]] = res[1];
}
});
//当前行+1
currentrow++;
}
//回到循环行的最后一行
currentrow--;
#region 纵向合并单元格
coltmps.Where<string[]>(arr => arr[2] != "").ToList<string[]>()
.ForEach(arr =>
{
if (curdt.Rows.Count > 1)//数据记录数大于1时才进行合并
{
int curindex = cyclestartrow_index;//拿到循环行的起始行索引
//string val = (ExcelHelper.GetCellValue(isheet.GetRow(curindex).GetCell(GetColIndex(arr[0]))) ?? "").ToString();
string val = curdt.Rows[0][arr[2]].ToString();//拿到合并控制键对应的数据值
for (int i = 1; i < curdt.Rows.Count; i++)
{
//string realval = (ExcelHelper.GetCellValue(isheet.GetRow(cyclestartrow_index + i).GetCell(GetColIndex(arr[0]))) ?? "").ToString();
string realval = curdt.Rows[i][arr[2]].ToString();//拿到当前行合并控制键对应的数据值
if (realval == val)
{
//匹配成功
if (i == curdt.Rows.Count - 1)
{
//最后一行一定要参与合并
isheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(curindex, cyclestartrow_index + i, GetColIndex(arr[0]), GetColIndex(arr[0])));
}
}
else
{
//匹配未成功
//如果之前处在匹配成功的状态里,那么进行合并操作
isheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(curindex, cyclestartrow_index + i - 1, GetColIndex(arr[0]), GetColIndex(arr[0])));
val = realval;
curindex = cyclestartrow_index + i;
}
}
}
});
#endregion
}
#endregion
}
});
}
#region 解析图片
sheet.OfType<XmlElement>()
.Where<XmlElement>(i => i.Name == "pic")
.ToList<XmlElement>()
.ForEach(pic =>
{
XmlElement from = pic.ChildNodes.OfType<XmlElement>()
.Where<XmlElement>(i => i.Name == "from")
.FirstOrDefault<XmlElement>();
if (from == null) throw new Exception("pic节点下找不到from节点");
string frommodel = (from.GetAttribute("model") ?? "").ToString().Trim(' ');
if (frommodel == "") throw new Exception("pic节点下的from节点的model属性不能为空");
byte[] bytes;
string fromvalue = (from.GetAttribute("value") ?? "").ToString().Trim(' ');
string res = "";
int index_c = 0;
Regex reg = new Regex(@"#(parameters|calitems)\.([^#]+)#");
Match mat = reg.Match(fromvalue);
string type = "";
string ext = "";
if (mat.Success)
{
res += fromvalue.Substring(index_c, mat.Index - index_c);
index_c = mat.Index + mat.Length;
type = mat.Groups[1].Value;
ext = mat.Groups[2].Value;
if (type == "parameters")
{
parameter p = this.parameters.Where<parameter>(i => i.name == ext).FirstOrDefault<parameter>();
if (p == null) throw new Exception("找不到参数:" + mat.Groups[0].Value);
res += p.value.ToString();
}
else if (type == "calitems")
{
calitem cp = this.calitems.Where<calitem>(i => i.name == ext).FirstOrDefault<calitem>();
if (cp == null) throw new Exception("找不到计算项:" + mat.Groups[0].Value);
res += cp.value.ToString();
}
}
while ((mat = mat.NextMatch()).Success)
{
res += fromvalue.Substring(index_c, mat.Index - index_c);
index_c = mat.Index + mat.Length;
type = mat.Groups[1].Value;
ext = mat.Groups[2].Value;
if (type == "parameters")
{
parameter p = this.parameters.Where<parameter>(i => i.name == ext).FirstOrDefault<parameter>();
if (p == null) throw new Exception("找不到参数:" + mat.Groups[0].Value);
res += p.value.ToString();
}
else if (type == "calitems")
{
calitem cp = this.calitems.Where<calitem>(i => i.name == ext).FirstOrDefault<calitem>();
if (cp == null) throw new Exception("找不到计算项:" + mat.Groups[0].Value);
res += cp.value.ToString();
}
}
res += fromvalue.Substring(index_c, fromvalue.Length - index_c);
if (frommodel == "QRCode")
{
string qrsize = (from.GetAttribute("QRSize") ?? "").Trim(' ');
int size = 100;
if (qrsize != "") size = int.Parse(qrsize);
//解析二维码
string filepath = Guid.NewGuid().ToString().Replace("-", "") + ".png";
QRCodeOP.Encode(res, size, filepath, -1);
bytes = File.ReadAllBytes(filepath);
File.Delete(filepath);
XmlElement stretch = pic.ChildNodes.OfType<XmlElement>()
.Where<XmlElement>(i => i.Name == "stretch")
.FirstOrDefault<XmlElement>();
if (stretch == null) throw new Exception("pic节点下的必须存在stretch节点");
XmlElement start = stretch.ChildNodes.OfType<XmlElement>().Where<XmlElement>(i => i.Name == "start")
.FirstOrDefault<XmlElement>();
if (start == null) throw new Exception("stretch节点下必须存在start节点");
int col = GetColIndex(start.GetAttribute("col"));
int row = int.Parse(start.GetAttribute("row")) - 1;
int offx = int.Parse(start.GetAttribute("offx"));
int offy = int.Parse(start.GetAttribute("offy"));
//将图片数据装载到book中
int picindex = book.AddPicture(bytes, PictureType.PNG);
HSSFPatriarch patriarch = (HSSFPatriarch)isheet.CreateDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(offx, offy, 0, 0, col, row, col, row);
HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, picindex);
pict.Resize();//设置图片按照原来的大小计算
}
else
{
throw new Exception("仅支持二维码图片的插入,请将from节点的model配置项设置为QRCode");
}
});
#endregion
});
//注意下面保存的写法,很重要,被坑了。。。
FileStream sw = File.Create(destfilepath);
book.Write(sw);
sw.Close();
#endregion
}
}
/// <summary>解析值coltmp和pic\from的属性value的实际值
/// </summary>
/// <param name="colval">如:qwe#parameters.caseno#hjk</param>
/// <returns></returns>
private string ParseVal(string colval)
{
string res = "";
int index_c = 0;
Regex reg = new Regex(@"#(parameters|calitems)\.([^#]+)#");
Match mat = reg.Match(colval);
string type = "";
string ext = "";
if (mat.Success)
{
res += colval.Substring(index_c, mat.Index - index_c);
index_c = mat.Index + mat.Length;
type = mat.Groups[1].Value;
ext = mat.Groups[2].Value;
if (type == "parameters")
{
parameter p = this.parameters.Where<parameter>(i => i.name == ext).FirstOrDefault<parameter>();
if (p == null) throw new Exception("找不到参数:" + mat.Groups[0].Value);
res += p.value.ToString();
}
else if (type == "calitems")
{
calitem cp = this.calitems.Where<calitem>(i => i.name == ext).FirstOrDefault<calitem>();
if (cp == null) throw new Exception("找不到计算项:" + mat.Groups[0].Value);
res += cp.value.ToString();
}
}
while ((mat = mat.NextMatch()).Success)
{
res += colval.Substring(index_c, mat.Index - index_c);