duan
2024-08-21 22bd5bc1ce2b49284cc2f042c7f4f48619fcf85b
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DIXWeb.Entity.DIX;
using DIXWeb.Entity.GlobalM;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
 
namespace DIXWeb.Business.DIX
{
    public class ExcelHelper
    {
        private bool IsXlsx=false;
        public static void CreateExcel(List<ExcelEntity> list, string excelRealPath, string sheetName)
        {
            new ToExcelTemplate(excelRealPath, sheetName).ExportTemplate(list);
        }
 
 
        public static List<ExcelInspectObject> ReadExcel(string excelRealPath, string sheetName)
        {
            List<ExcelInspectObject> list = new List<ExcelInspectObject>();
            ExcelInspectObject record = null;
            NPOI.SS.UserModel.IWorkbook wk = null;
            string extension = Path.GetExtension(excelRealPath).ToLower();
            using (FileStream fs = File.OpenRead(excelRealPath))
            {
                if (".xls".Equals(extension))
                {
                    wk = new NPOI.HSSF.UserModel.HSSFWorkbook(fs);
                }
                else
                {
                    wk = new NPOI.XSSF.UserModel.XSSFWorkbook(fs);
                }
            }
            ISheet sheet = wk.GetSheetAt(0);
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i < sheet.LastRowNum + 1; i++)
            {
                record = new ExcelInspectObject();
                int index = 0;
                record.OrgId = sheet.GetRow(i).GetCell(index++).ToString();
                record.InspectObjectTypeId = sheet.GetRow(i).GetCell(index++).ToString();
                record.InspectObjectCode = sheet.GetRow(i).GetCell(index++).ToString();
                record.InspectObjectName = sheet.GetRow(i).GetCell(index++).ToString();
                record.InspectAreaId = sheet.GetRow(i).GetCell(index++).ToString();
                record.InspectLocation = sheet.GetRow(i).GetCell(index++).ToString();
                record.InspectTypeId = sheet.GetRow(i).GetCell(index++).ToString();
                record.InspectFrequencyId = sheet.GetRow(i).GetCell(index++).ToString();
                record.InspectObjectStatusId = sheet.GetRow(i).GetCell(index++).ToString();
                record.IsNeedInspect = sheet.GetRow(i).GetCell(index++).ToString();
                record.InspectStartDate = sheet.GetRow(i).GetCell(index++).DateCellValue.Date;
                record.AlarmDays = sheet.GetRow(i).GetCell(index++).ToString();
 
                list.Add(record);
            }
            return list;
        }
 
 
        public static void CreateExcel(string excelRealPath, string sheetName)
        {
            new ToExcelTemplate(excelRealPath, sheetName).ExportTemplate();
        }
 
 
        public void Create(string excelRealPath, string sheetName,bool IsXlsx)
        {
            this.IsXlsx = IsXlsx;
            HSSFWorkbook workbook = new HSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("Sheet1");
            SetDefaultStyle(workbook, sheet);
 
            ToExcelTemplate data = new ToExcelTemplate();
            string[] orgArr = data.GetOrg();
            string[] iotArr = data.GetInspectObjectType();
            string[] iaArr = data.GetInspectArea();
            string[] itArr = data.GetInspectType();
            string[] ifArr = data.GetInspectFrequency();
            string[] isArr = data.GetInspectStatu();
            string InspectObjectIsNeedStr = System.Configuration.ConfigurationManager.AppSettings["InspectObjectIsNeedStr"].ToString();
            string[] iNArr = InspectObjectIsNeedStr.Split(',');
            
            SetCellDropdownList(workbook, sheet, "Org", 1, 0, 0, orgArr,1);
            SetCellDropdownList(workbook, sheet, "InspectObjectType", 1, 1, 1, iotArr,2);
            SetCellDropdownList(workbook, sheet, "InspectArea", 1, 4, 4, iaArr,3);
            SetCellDropdownList(workbook, sheet, "InspectType", 1, 6, 6, itArr,4);
            SetCellDropdownList(workbook, sheet, "InspectFrequency", 1, 7, 7, ifArr,5);
            SetCellDropdownList(workbook, sheet, "InspectStatu", 1, 8, 8, isArr,6);
            SetCellDropdownList(workbook, sheet, "IsNeed", 1, 9, 9, iNArr,7);
 
            WriteToFile(excelRealPath, workbook);
        }
 
 
        public static void SetCellDropdownList(HSSFWorkbook workbook, ISheet sheet, string name,int firstRow, int firstcol, int lastcol, string[] vals, int sheetindex = 1)
        {
            //先创建一个Sheet专门用于存储下拉项的值       
            ISheet sheet2 = workbook.CreateSheet(name);
            //隐藏       
            workbook.SetSheetHidden(sheetindex, true);
            int index = 0;
            foreach (var item in vals)
            {
                sheet2.CreateRow(index).CreateCell(0).SetCellValue(item);
                index++;
            }
            //创建的下拉项的区域:       
            var rangeName = name + "Range";
            IName range = workbook.CreateName();
            range.RefersToFormula = name + "!$A$1:$A$" + index;
            range.NameName = rangeName;
            CellRangeAddressList regions = new CellRangeAddressList(firstRow, 65535, firstcol, lastcol);
            DVConstraint constraint = DVConstraint.CreateFormulaListConstraint(rangeName);
            HSSFDataValidation dataValidate = new HSSFDataValidation(regions, constraint);
            dataValidate.CreateErrorBox("输入不合法", "请输入或选择下拉列表中的值。");
            dataValidate.ShowPromptBox = true;
            sheet.AddValidationData(dataValidate);
        }
 
        private string WriteToFile(string fileName, HSSFWorkbook workbook)
        {
            string result = string.Empty;
            try
            {
                using (System.IO.FileStream file = new System.IO.FileStream(fileName, System.IO.FileMode.Create))
                {
                    workbook.Write(file);
                }
                result = "SUCCESS,";
            }
            catch (Exception ex)
            {
                result = "ERROR," + ex.Message;
            }
            return result;
        }
        private void SetDefaultStyle(IWorkbook workbook, ISheet sheet)
        {
            Dictionary<String, ICellStyle> styles = createCellStyles(workbook);
            if (!IsXlsx)
                sheet.TabColorIndex = NPOI.HSSF.Util.HSSFColor.CornflowerBlue.Index;
            NPOI.SS.UserModel.IPrintSetup printSetup = sheet.PrintSetup;
            sheet.DisplayGridlines = false;
            SetDefaultRowHeight(sheet, 28.0F);
 
            sheet.FitToPage = (true);
            sheet.HorizontallyCenter = (true);  //设置Sheet缩放
            sheet.SetZoom(82, 100);              // 100 percent magnification
            SetColumnWidth(sheet, 00, 25.00);
            SetColumnWidth(sheet, 01, 25.00);
            SetColumnWidth(sheet, 02, 25.00);
            SetColumnWidth(sheet, 03, 25.00);
            SetColumnWidth(sheet, 04, 25.00);
            SetColumnWidth(sheet, 05, 25.00);
            SetColumnWidth(sheet, 06, 25.00);
            SetColumnWidth(sheet, 07, 25.00);
            SetColumnWidth(sheet, 08, 25.00);
            SetColumnWidth(sheet, 09, 25.00);
            SetColumnWidth(sheet, 10, 25.00);
 
            sheet.SetColumnHidden(24, true);
 
            IRow curRow = sheet.CreateRow(0);
            ICell curCel = null;
            SetRowHeight(curRow, 33);//第一行行高
            //第一行 列头
            curCel = curRow.CreateCell(0);
            SetColumnWidth(sheet, 00, 15.00);
            curCel.SetCellValue("组织ID");
            curCel.CellStyle = styles["Bold_13_Black_Font"];
 
            curCel = curRow.CreateCell(1);
            SetColumnWidth(sheet, 01, 25.00);
            curCel.SetCellValue("设备类型ID");
            curCel.CellStyle = styles["Bold_13_Black_Font"];
 
            curCel = curRow.CreateCell(2);
            SetColumnWidth(sheet, 02, 15.00);
            curCel.SetCellValue("设备编号");
            curCel.CellStyle = styles["Bold_13_Black_Font"];
 
            curCel = curRow.CreateCell(3);
            SetColumnWidth(sheet, 03, 15.00);
            curCel.SetCellValue("设备名称");
            curCel.CellStyle = styles["Bold_13_Black_Font"];
 
            curCel = curRow.CreateCell(4);
            SetColumnWidth(sheet, 04, 25.00);
            curCel.SetCellValue("设备区域ID");
            curCel.CellStyle = styles["Bold_13_Black_Font"];
 
            curCel = curRow.CreateCell(5);
            SetColumnWidth(sheet, 05, 15.00);
            curCel.SetCellValue("设备位置");
            curCel.CellStyle = styles["Bold_13_Black_Font"];
 
            curCel = curRow.CreateCell(6);
            SetColumnWidth(sheet, 06, 25.00);
            curCel.SetCellValue("点检周期ID");
            curCel.CellStyle = styles["Bold_13_Black_Font"];
 
            curCel = curRow.CreateCell(7);
            SetColumnWidth(sheet, 07, 20.00);
            curCel.SetCellValue("点检频率ID");
            curCel.CellStyle = styles["Bold_13_Black_Font"];
 
            curCel = curRow.CreateCell(8);
            SetColumnWidth(sheet, 08, 20.00);
            curCel.SetCellValue("设备状态ID");
            curCel.CellStyle = styles["Bold_13_Black_Font"];
 
            curCel = curRow.CreateCell(9);
            SetColumnWidth(sheet, 09, 30.00);
            curCel.SetCellValue("是否需要点检");
            curCel.CellStyle = styles["Bold_13_Black_Font"];
 
            curCel = curRow.CreateCell(10);
            SetColumnWidth(sheet, 10, 25.00);
            curCel.SetCellValue("导入日期");
            curCel.CellStyle = styles["Bold_13_Black_Font"];
 
            curCel = curRow.CreateCell(11);
            SetColumnWidth(sheet, 11, 25.00);
            curCel.SetCellValue("有效期(年)");
            curCel.CellStyle = styles["Bold_13_Black_Font"];
 
 
        }
        protected void SetDefaultRowHeight(ISheet sheet, float rowHeight)
        {
            sheet.DefaultRowHeight = Convert.ToInt16(rowHeight * 20);
        }
        protected void SetColumnWidth(ISheet sheet, int columnIndex, double width)
        {
            sheet.SetColumnWidth(columnIndex, (int)((width + 0.72) * 256));
        }
        protected void SetRowHeight(IRow targetRow, float rowHeight)
        {
            targetRow.Height = Convert.ToInt16(rowHeight * 20);
        }
        protected void SetFontHeight(IFont targetFont, double fontHeight)
        {
            if (IsXlsx == true)
            {
                targetFont.FontHeightInPoints = Convert.ToInt16(fontHeight);
            }
            else
            {
                targetFont.FontHeight = fontHeight * 20;
            }
        }
 
        protected Dictionary<string, ICellStyle> createCellStyles(IWorkbook wb)
        {
            //return base.createCellStyles(wb);
            Dictionary<string, ICellStyle> styles = new Dictionary<string, ICellStyle>();
            IDataFormat df = wb.CreateDataFormat();
            NPOI.SS.UserModel.IFont MsFont_9_Black_Font = wb.CreateFont();
            MsFont_9_Black_Font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Normal;
            SetFontHeight(MsFont_9_Black_Font, 9);
            MsFont_9_Black_Font.FontName = "微软雅黑";
 
            NPOI.SS.UserModel.IFont MsFont_9_Blue_Font = wb.CreateFont();
            MsFont_9_Blue_Font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Normal;
            MsFont_9_Blue_Font.Color = NPOI.HSSF.Util.HSSFColor.Blue.Index;
            SetFontHeight(MsFont_9_Blue_Font, 9);
            MsFont_9_Blue_Font.FontName = "微软雅黑";
 
            NPOI.SS.UserModel.IFont MsFont_9_Red_Font = wb.CreateFont();
            MsFont_9_Red_Font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Normal;
            MsFont_9_Red_Font.Color = NPOI.HSSF.Util.HSSFColor.Red.Index;
            SetFontHeight(MsFont_9_Red_Font, 9);
            MsFont_9_Red_Font.FontName = "微软雅黑";
 
 
            NPOI.SS.UserModel.ICellStyle cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            cellStyle.SetFont(MsFont_9_Black_Font);
            cellStyle.WrapText = (true);
            cellStyle.IsLocked = false;
            styles.Add("微黑_9_V中H中_换行_全框细", cellStyle);
 
            cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right;
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            cellStyle.SetFont(MsFont_9_Black_Font);
            cellStyle.WrapText = (true);
            cellStyle.IsLocked = false;
            styles.Add("微黑_9_V中H右_换行_全框细", cellStyle);
 
            cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            cellStyle.SetFont(MsFont_9_Blue_Font);
            cellStyle.WrapText = (true);
            cellStyle.IsLocked = false;
            styles.Add("微黑_9_V中H左_换行_全框细_蓝字", cellStyle);
 
 
            cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            cellStyle.SetFont(MsFont_9_Black_Font);
            cellStyle.WrapText = (true);
            cellStyle.IsLocked = true;  //上锁
            styles.Add("微黑_9_V中H中_换行_全框细_上锁", cellStyle);
 
            cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            cellStyle.SetFont(MsFont_9_Red_Font);
            cellStyle.WrapText = (true);
            cellStyle.IsLocked = false;
            styles.Add("微黑_红9_V中H中_换行_全框细", cellStyle);
 
            cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            cellStyle.SetFont(MsFont_9_Black_Font);
            cellStyle.WrapText = (true);
            cellStyle.IsLocked = false;
            cellStyle.FillPattern = FillPattern.SolidForeground;
            cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.PaleBlue.Index;
            styles.Add("微黑_9_V中H中_换行_全框细_蓝背景", cellStyle);
 
            cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            cellStyle.SetFont(MsFont_9_Black_Font);
            cellStyle.WrapText = (true);
            cellStyle.IsLocked = true;
            cellStyle.FillPattern = FillPattern.SolidForeground;
            cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.PaleBlue.Index;
            styles.Add("微黑_9_V中H中_换行_全框细_蓝背景_上锁", cellStyle);
 
            cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            cellStyle.SetFont(MsFont_9_Blue_Font);
            cellStyle.WrapText = (true);
            cellStyle.IsLocked = true;
            cellStyle.FillPattern = FillPattern.SolidForeground;
            cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.PaleBlue.Index;
            styles.Add("微黑_9蓝_V中H中_换行_全框细_蓝背景_上锁", cellStyle);
 
            cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right;
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            cellStyle.SetFont(MsFont_9_Black_Font);
            cellStyle.WrapText = (true);
            cellStyle.IsLocked = false;
            cellStyle.FillPattern = FillPattern.SolidForeground;
            cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.BrightGreen.Index;
            styles.Add("微黑_9_V中H右_换行_全框细_绿背景", cellStyle);
 
            cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            cellStyle.SetFont(MsFont_9_Black_Font);
            cellStyle.WrapText = (true);
            cellStyle.IsLocked = false;
            cellStyle.FillPattern = FillPattern.SolidForeground;
            cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LemonChiffon.Index;
            styles.Add("微黑_9_V中H中_换行_全框细_柠檬黄背景", cellStyle);
 
            cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right;
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            cellStyle.SetFont(MsFont_9_Black_Font);
            cellStyle.WrapText = (true);
            cellStyle.IsLocked = false;
            cellStyle.FillPattern = FillPattern.SolidForeground;
            cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LemonChiffon.Index;
            styles.Add("微黑_9_V中H右_换行_全框细_柠檬黄背景", cellStyle);
 
            cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            cellStyle.SetFont(MsFont_9_Black_Font);
            cellStyle.WrapText = (true);
            cellStyle.IsLocked = true;
            cellStyle.FillPattern = FillPattern.SolidForeground;
            cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LemonChiffon.Index;
            styles.Add("微黑_9_V中H中_换行_全框细_柠檬黄背景_上锁", cellStyle);
 
            cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            cellStyle.SetFont(MsFont_9_Black_Font);
            cellStyle.WrapText = (false);
            cellStyle.IsLocked = false;
            cellStyle.FillPattern = FillPattern.SolidForeground;
            cellStyle.DataFormat = (df.GetFormat("m-d h:mm"));
            cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LemonChiffon.Index;
            styles.Add("微黑_9_V中H中_不换行_全框细_柠檬黄背景_日期", cellStyle);
 
            cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            cellStyle.SetFont(MsFont_9_Black_Font);
            cellStyle.WrapText = (true);
            cellStyle.IsLocked = false;
            cellStyle.FillPattern = FillPattern.SolidForeground;
            cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightTurquoise.Index;
            styles.Add("微黑_9_V中H中_换行_全框细_淡蓝背景", cellStyle);
 
 
            cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            cellStyle.SetFont(MsFont_9_Black_Font);
            cellStyle.WrapText = (false);
            cellStyle.IsLocked = false;
            cellStyle.FillPattern = FillPattern.SolidForeground;
            cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightTurquoise.Index;
            cellStyle.DataFormat = (df.GetFormat("m-d h:mm"));
            styles.Add("微黑_9_V中H中_不换行_全框细_淡蓝背景_日期", cellStyle);
 
 
            NPOI.SS.UserModel.IFont Bold_13_Black_Font = wb.CreateFont();
            Bold_13_Black_Font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
            Bold_13_Black_Font.FontHeight = 13 * 20;      //字体高度,XLSX下有字体大小问题
            //Bold_15_Black_Font.FontHeightInPoints = 10; //字体高度,XLS下有大小问题
            Bold_13_Black_Font.FontName = "黑体";
 
            NPOI.SS.UserModel.IFont Bold_15_Black_Font = wb.CreateFont();
            Bold_15_Black_Font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
            Bold_15_Black_Font.FontHeight = 15 * 20;      //字体高度,XLSX下有字体大小问题
            //Bold_15_Black_Font.FontHeightInPoints = 10; //字体高度,XLS下有大小问题
            Bold_15_Black_Font.FontName = "黑体";
 
            NPOI.SS.UserModel.IFont MsFont_12_Black_Font = wb.CreateFont();
            MsFont_12_Black_Font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Normal;
            SetFontHeight(MsFont_12_Black_Font, 12);
            MsFont_12_Black_Font.FontName = "微软雅黑";
 
            cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            cellStyle.SetFont(Bold_13_Black_Font);
            //cellStyle.WrapText = (false);//换行
            styles.Add("Bold_13_Black_Font", cellStyle);
 
            cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            cellStyle.SetFont(Bold_15_Black_Font);
            //cellStyle.WrapText = (false);
            styles.Add("Bold_15_Black_Font", cellStyle);
 
 
            cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            cellStyle.SetFont(MsFont_12_Black_Font);
            //cellStyle.WrapText = (true);//换行
            cellStyle.IsLocked = true;
            styles.Add("微黑_12_V中H中_全框细_上锁", cellStyle);
 
            return styles;
        }
 
        protected NPOI.SS.UserModel.ICellStyle CreateBorderedStyle(NPOI.SS.UserModel.IWorkbook wb,
            NPOI.SS.UserModel.BorderStyle defaltBorderStyle = NPOI.SS.UserModel.BorderStyle.Thin)
        {
            NPOI.SS.UserModel.ICellStyle style = wb.CreateCellStyle();
            style.BorderRight = defaltBorderStyle;
            style.BorderBottom = defaltBorderStyle;
            style.BorderLeft = defaltBorderStyle;
            style.BorderTop = defaltBorderStyle;
            style.RightBorderColor = (NPOI.SS.UserModel.IndexedColors.Black.Index);
            style.BottomBorderColor = (NPOI.SS.UserModel.IndexedColors.Black.Index);
            style.LeftBorderColor = (NPOI.SS.UserModel.IndexedColors.Black.Index);
            style.TopBorderColor = (NPOI.SS.UserModel.IndexedColors.Black.Index);
            return style;
        }
    }
}