1
duan
2024-08-21 f71a02229c1ba00fbecaead19256593ffb052753
提交 | 用户 | age
90c6eb 1 using System;
J 2 using System.Collections.Generic;
c82ec1 3 using System.IO;
90c6eb 4 using System.Linq;
J 5 using System.Text;
6 using System.Threading.Tasks;
3ddf58 7 using DIXWeb.Entity.DIX;
c82ec1 8 using DIXWeb.Entity.GlobalM;
1bf8ef 9 using NPOI.HSSF.UserModel;
90c6eb 10 using NPOI.SS.UserModel;
1bf8ef 11 using NPOI.SS.Util;
90c6eb 12
3ddf58 13 namespace DIXWeb.Business.DIX
90c6eb 14 {
J 15     public class ExcelHelper
16     {
1bf8ef 17         private bool IsXlsx=false;
A 18         public static void CreateExcel(List<ExcelEntity> list, string excelRealPath, string sheetName)
19         {
90c6eb 20             new ToExcelTemplate(excelRealPath, sheetName).ExportTemplate(list);
J 21         }
c82ec1 22
A 23
29abd3 24         public static List<ExcelInspectObject> ReadExcel(string excelRealPath, string sheetName)
c82ec1 25         {
29abd3 26             List<ExcelInspectObject> list = new List<ExcelInspectObject>();
A 27             ExcelInspectObject record = null;
c82ec1 28             NPOI.SS.UserModel.IWorkbook wk = null;
A 29             string extension = Path.GetExtension(excelRealPath).ToLower();
30             using (FileStream fs = File.OpenRead(excelRealPath))
31             {
32                 if (".xls".Equals(extension))
33                 {
34                     wk = new NPOI.HSSF.UserModel.HSSFWorkbook(fs);
35                 }
36                 else
37                 {
38                     wk = new NPOI.XSSF.UserModel.XSSFWorkbook(fs);
39                 }
40             }
41             ISheet sheet = wk.GetSheetAt(0);
42             StringBuilder sb = new StringBuilder();
43             for (int i = 1; i < sheet.LastRowNum + 1; i++)
44             {
29abd3 45                 record = new ExcelInspectObject();
c82ec1 46                 int index = 0;
29abd3 47                 record.OrgId = sheet.GetRow(i).GetCell(index++).ToString();
A 48                 record.InspectObjectTypeId = sheet.GetRow(i).GetCell(index++).ToString();
c82ec1 49                 record.InspectObjectCode = sheet.GetRow(i).GetCell(index++).ToString();
A 50                 record.InspectObjectName = sheet.GetRow(i).GetCell(index++).ToString();
29abd3 51                 record.InspectAreaId = sheet.GetRow(i).GetCell(index++).ToString();
c82ec1 52                 record.InspectLocation = sheet.GetRow(i).GetCell(index++).ToString();
29abd3 53                 record.InspectTypeId = sheet.GetRow(i).GetCell(index++).ToString();
A 54                 record.InspectFrequencyId = sheet.GetRow(i).GetCell(index++).ToString();
55                 record.InspectObjectStatusId = sheet.GetRow(i).GetCell(index++).ToString();
c82ec1 56                 record.IsNeedInspect = sheet.GetRow(i).GetCell(index++).ToString();
73d1b1 57                 record.InspectStartDate = sheet.GetRow(i).GetCell(index++).DateCellValue.Date;
b6fc6e 58                 record.AlarmDays = sheet.GetRow(i).GetCell(index++).ToString();
c82ec1 59
A 60                 list.Add(record);
61             }
62             return list;
63         }
6ac346 64
A 65
66         public static void CreateExcel(string excelRealPath, string sheetName)
67         {
68             new ToExcelTemplate(excelRealPath, sheetName).ExportTemplate();
69         }
70
71
1bf8ef 72         public void Create(string excelRealPath, string sheetName,bool IsXlsx)
A 73         {
74             this.IsXlsx = IsXlsx;
75             HSSFWorkbook workbook = new HSSFWorkbook();
76             ISheet sheet = workbook.CreateSheet("Sheet1");
77             SetDefaultStyle(workbook, sheet);
78
79             ToExcelTemplate data = new ToExcelTemplate();
80             string[] orgArr = data.GetOrg();
81             string[] iotArr = data.GetInspectObjectType();
82             string[] iaArr = data.GetInspectArea();
83             string[] itArr = data.GetInspectType();
84             string[] ifArr = data.GetInspectFrequency();
85             string[] isArr = data.GetInspectStatu();
86             string InspectObjectIsNeedStr = System.Configuration.ConfigurationManager.AppSettings["InspectObjectIsNeedStr"].ToString();
87             string[] iNArr = InspectObjectIsNeedStr.Split(',');
88             
89             SetCellDropdownList(workbook, sheet, "Org", 1, 0, 0, orgArr,1);
90             SetCellDropdownList(workbook, sheet, "InspectObjectType", 1, 1, 1, iotArr,2);
91             SetCellDropdownList(workbook, sheet, "InspectArea", 1, 4, 4, iaArr,3);
92             SetCellDropdownList(workbook, sheet, "InspectType", 1, 6, 6, itArr,4);
93             SetCellDropdownList(workbook, sheet, "InspectFrequency", 1, 7, 7, ifArr,5);
94             SetCellDropdownList(workbook, sheet, "InspectStatu", 1, 8, 8, isArr,6);
95             SetCellDropdownList(workbook, sheet, "IsNeed", 1, 9, 9, iNArr,7);
96
97             WriteToFile(excelRealPath, workbook);
98         }
99
100
101         public static void SetCellDropdownList(HSSFWorkbook workbook, ISheet sheet, string name,int firstRow, int firstcol, int lastcol, string[] vals, int sheetindex = 1)
102         {
103             //先创建一个Sheet专门用于存储下拉项的值       
104             ISheet sheet2 = workbook.CreateSheet(name);
105             //隐藏       
106             workbook.SetSheetHidden(sheetindex, true);
107             int index = 0;
108             foreach (var item in vals)
109             {
110                 sheet2.CreateRow(index).CreateCell(0).SetCellValue(item);
111                 index++;
112             }
113             //创建的下拉项的区域:       
114             var rangeName = name + "Range";
115             IName range = workbook.CreateName();
116             range.RefersToFormula = name + "!$A$1:$A$" + index;
117             range.NameName = rangeName;
118             CellRangeAddressList regions = new CellRangeAddressList(firstRow, 65535, firstcol, lastcol);
119             DVConstraint constraint = DVConstraint.CreateFormulaListConstraint(rangeName);
120             HSSFDataValidation dataValidate = new HSSFDataValidation(regions, constraint);
121             dataValidate.CreateErrorBox("输入不合法", "请输入或选择下拉列表中的值。");
122             dataValidate.ShowPromptBox = true;
123             sheet.AddValidationData(dataValidate);
124         }
125
126         private string WriteToFile(string fileName, HSSFWorkbook workbook)
127         {
128             string result = string.Empty;
129             try
130             {
131                 using (System.IO.FileStream file = new System.IO.FileStream(fileName, System.IO.FileMode.Create))
132                 {
133                     workbook.Write(file);
134                 }
135                 result = "SUCCESS,";
136             }
137             catch (Exception ex)
138             {
139                 result = "ERROR," + ex.Message;
140             }
141             return result;
142         }
143         private void SetDefaultStyle(IWorkbook workbook, ISheet sheet)
144         {
145             Dictionary<String, ICellStyle> styles = createCellStyles(workbook);
146             if (!IsXlsx)
147                 sheet.TabColorIndex = NPOI.HSSF.Util.HSSFColor.CornflowerBlue.Index;
148             NPOI.SS.UserModel.IPrintSetup printSetup = sheet.PrintSetup;
149             sheet.DisplayGridlines = false;
150             SetDefaultRowHeight(sheet, 28.0F);
151
152             sheet.FitToPage = (true);
153             sheet.HorizontallyCenter = (true);  //设置Sheet缩放
154             sheet.SetZoom(82, 100);              // 100 percent magnification
155             SetColumnWidth(sheet, 00, 25.00);
156             SetColumnWidth(sheet, 01, 25.00);
157             SetColumnWidth(sheet, 02, 25.00);
158             SetColumnWidth(sheet, 03, 25.00);
159             SetColumnWidth(sheet, 04, 25.00);
160             SetColumnWidth(sheet, 05, 25.00);
161             SetColumnWidth(sheet, 06, 25.00);
162             SetColumnWidth(sheet, 07, 25.00);
163             SetColumnWidth(sheet, 08, 25.00);
164             SetColumnWidth(sheet, 09, 25.00);
165             SetColumnWidth(sheet, 10, 25.00);
166
167             sheet.SetColumnHidden(24, true);
168
169             IRow curRow = sheet.CreateRow(0);
170             ICell curCel = null;
171             SetRowHeight(curRow, 33);//第一行行高
172             //第一行 列头
173             curCel = curRow.CreateCell(0);
174             SetColumnWidth(sheet, 00, 15.00);
175             curCel.SetCellValue("组织ID");
176             curCel.CellStyle = styles["Bold_13_Black_Font"];
177
178             curCel = curRow.CreateCell(1);
179             SetColumnWidth(sheet, 01, 25.00);
180             curCel.SetCellValue("设备类型ID");
181             curCel.CellStyle = styles["Bold_13_Black_Font"];
182
183             curCel = curRow.CreateCell(2);
184             SetColumnWidth(sheet, 02, 15.00);
185             curCel.SetCellValue("设备编号");
186             curCel.CellStyle = styles["Bold_13_Black_Font"];
187
188             curCel = curRow.CreateCell(3);
189             SetColumnWidth(sheet, 03, 15.00);
190             curCel.SetCellValue("设备名称");
191             curCel.CellStyle = styles["Bold_13_Black_Font"];
192
193             curCel = curRow.CreateCell(4);
194             SetColumnWidth(sheet, 04, 25.00);
195             curCel.SetCellValue("设备区域ID");
196             curCel.CellStyle = styles["Bold_13_Black_Font"];
197
198             curCel = curRow.CreateCell(5);
199             SetColumnWidth(sheet, 05, 15.00);
200             curCel.SetCellValue("设备位置");
201             curCel.CellStyle = styles["Bold_13_Black_Font"];
202
203             curCel = curRow.CreateCell(6);
204             SetColumnWidth(sheet, 06, 25.00);
205             curCel.SetCellValue("点检周期ID");
206             curCel.CellStyle = styles["Bold_13_Black_Font"];
207
208             curCel = curRow.CreateCell(7);
209             SetColumnWidth(sheet, 07, 20.00);
210             curCel.SetCellValue("点检频率ID");
211             curCel.CellStyle = styles["Bold_13_Black_Font"];
212
213             curCel = curRow.CreateCell(8);
214             SetColumnWidth(sheet, 08, 20.00);
215             curCel.SetCellValue("设备状态ID");
216             curCel.CellStyle = styles["Bold_13_Black_Font"];
217
218             curCel = curRow.CreateCell(9);
219             SetColumnWidth(sheet, 09, 30.00);
220             curCel.SetCellValue("是否需要点检");
221             curCel.CellStyle = styles["Bold_13_Black_Font"];
222
223             curCel = curRow.CreateCell(10);
224             SetColumnWidth(sheet, 10, 25.00);
225             curCel.SetCellValue("导入日期");
226             curCel.CellStyle = styles["Bold_13_Black_Font"];
227
228             curCel = curRow.CreateCell(11);
229             SetColumnWidth(sheet, 11, 25.00);
230             curCel.SetCellValue("有效期(年)");
231             curCel.CellStyle = styles["Bold_13_Black_Font"];
232
233
234         }
235         protected void SetDefaultRowHeight(ISheet sheet, float rowHeight)
236         {
237             sheet.DefaultRowHeight = Convert.ToInt16(rowHeight * 20);
238         }
239         protected void SetColumnWidth(ISheet sheet, int columnIndex, double width)
240         {
241             sheet.SetColumnWidth(columnIndex, (int)((width + 0.72) * 256));
242         }
243         protected void SetRowHeight(IRow targetRow, float rowHeight)
244         {
245             targetRow.Height = Convert.ToInt16(rowHeight * 20);
246         }
247         protected void SetFontHeight(IFont targetFont, double fontHeight)
248         {
249             if (IsXlsx == true)
250             {
251                 targetFont.FontHeightInPoints = Convert.ToInt16(fontHeight);
252             }
253             else
254             {
255                 targetFont.FontHeight = fontHeight * 20;
256             }
257         }
258
259         protected Dictionary<string, ICellStyle> createCellStyles(IWorkbook wb)
260         {
261             //return base.createCellStyles(wb);
262             Dictionary<string, ICellStyle> styles = new Dictionary<string, ICellStyle>();
263             IDataFormat df = wb.CreateDataFormat();
264             NPOI.SS.UserModel.IFont MsFont_9_Black_Font = wb.CreateFont();
265             MsFont_9_Black_Font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Normal;
266             SetFontHeight(MsFont_9_Black_Font, 9);
267             MsFont_9_Black_Font.FontName = "微软雅黑";
268
269             NPOI.SS.UserModel.IFont MsFont_9_Blue_Font = wb.CreateFont();
270             MsFont_9_Blue_Font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Normal;
271             MsFont_9_Blue_Font.Color = NPOI.HSSF.Util.HSSFColor.Blue.Index;
272             SetFontHeight(MsFont_9_Blue_Font, 9);
273             MsFont_9_Blue_Font.FontName = "微软雅黑";
274
275             NPOI.SS.UserModel.IFont MsFont_9_Red_Font = wb.CreateFont();
276             MsFont_9_Red_Font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Normal;
277             MsFont_9_Red_Font.Color = NPOI.HSSF.Util.HSSFColor.Red.Index;
278             SetFontHeight(MsFont_9_Red_Font, 9);
279             MsFont_9_Red_Font.FontName = "微软雅黑";
280
281
282             NPOI.SS.UserModel.ICellStyle cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
283             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
284             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
285             cellStyle.SetFont(MsFont_9_Black_Font);
286             cellStyle.WrapText = (true);
287             cellStyle.IsLocked = false;
288             styles.Add("微黑_9_V中H中_换行_全框细", cellStyle);
289
290             cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
291             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right;
292             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
293             cellStyle.SetFont(MsFont_9_Black_Font);
294             cellStyle.WrapText = (true);
295             cellStyle.IsLocked = false;
296             styles.Add("微黑_9_V中H右_换行_全框细", cellStyle);
297
298             cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
299             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
300             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
301             cellStyle.SetFont(MsFont_9_Blue_Font);
302             cellStyle.WrapText = (true);
303             cellStyle.IsLocked = false;
304             styles.Add("微黑_9_V中H左_换行_全框细_蓝字", cellStyle);
305
306
307             cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
308             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
309             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
310             cellStyle.SetFont(MsFont_9_Black_Font);
311             cellStyle.WrapText = (true);
312             cellStyle.IsLocked = true;  //上锁
313             styles.Add("微黑_9_V中H中_换行_全框细_上锁", cellStyle);
314
315             cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
316             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
317             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
318             cellStyle.SetFont(MsFont_9_Red_Font);
319             cellStyle.WrapText = (true);
320             cellStyle.IsLocked = false;
321             styles.Add("微黑_红9_V中H中_换行_全框细", cellStyle);
322
323             cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
324             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
325             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
326             cellStyle.SetFont(MsFont_9_Black_Font);
327             cellStyle.WrapText = (true);
328             cellStyle.IsLocked = false;
329             cellStyle.FillPattern = FillPattern.SolidForeground;
330             cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.PaleBlue.Index;
331             styles.Add("微黑_9_V中H中_换行_全框细_蓝背景", cellStyle);
332
333             cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
334             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
335             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
336             cellStyle.SetFont(MsFont_9_Black_Font);
337             cellStyle.WrapText = (true);
338             cellStyle.IsLocked = true;
339             cellStyle.FillPattern = FillPattern.SolidForeground;
340             cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.PaleBlue.Index;
341             styles.Add("微黑_9_V中H中_换行_全框细_蓝背景_上锁", cellStyle);
342
343             cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
344             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
345             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
346             cellStyle.SetFont(MsFont_9_Blue_Font);
347             cellStyle.WrapText = (true);
348             cellStyle.IsLocked = true;
349             cellStyle.FillPattern = FillPattern.SolidForeground;
350             cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.PaleBlue.Index;
351             styles.Add("微黑_9蓝_V中H中_换行_全框细_蓝背景_上锁", cellStyle);
352
353             cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
354             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right;
355             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
356             cellStyle.SetFont(MsFont_9_Black_Font);
357             cellStyle.WrapText = (true);
358             cellStyle.IsLocked = false;
359             cellStyle.FillPattern = FillPattern.SolidForeground;
360             cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.BrightGreen.Index;
361             styles.Add("微黑_9_V中H右_换行_全框细_绿背景", cellStyle);
362
363             cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
364             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
365             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
366             cellStyle.SetFont(MsFont_9_Black_Font);
367             cellStyle.WrapText = (true);
368             cellStyle.IsLocked = false;
369             cellStyle.FillPattern = FillPattern.SolidForeground;
370             cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LemonChiffon.Index;
371             styles.Add("微黑_9_V中H中_换行_全框细_柠檬黄背景", cellStyle);
372
373             cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
374             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right;
375             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
376             cellStyle.SetFont(MsFont_9_Black_Font);
377             cellStyle.WrapText = (true);
378             cellStyle.IsLocked = false;
379             cellStyle.FillPattern = FillPattern.SolidForeground;
380             cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LemonChiffon.Index;
381             styles.Add("微黑_9_V中H右_换行_全框细_柠檬黄背景", cellStyle);
382
383             cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
384             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
385             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
386             cellStyle.SetFont(MsFont_9_Black_Font);
387             cellStyle.WrapText = (true);
388             cellStyle.IsLocked = true;
389             cellStyle.FillPattern = FillPattern.SolidForeground;
390             cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LemonChiffon.Index;
391             styles.Add("微黑_9_V中H中_换行_全框细_柠檬黄背景_上锁", cellStyle);
392
393             cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
394             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
395             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
396             cellStyle.SetFont(MsFont_9_Black_Font);
397             cellStyle.WrapText = (false);
398             cellStyle.IsLocked = false;
399             cellStyle.FillPattern = FillPattern.SolidForeground;
400             cellStyle.DataFormat = (df.GetFormat("m-d h:mm"));
401             cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LemonChiffon.Index;
402             styles.Add("微黑_9_V中H中_不换行_全框细_柠檬黄背景_日期", cellStyle);
403
404             cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
405             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
406             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
407             cellStyle.SetFont(MsFont_9_Black_Font);
408             cellStyle.WrapText = (true);
409             cellStyle.IsLocked = false;
410             cellStyle.FillPattern = FillPattern.SolidForeground;
411             cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightTurquoise.Index;
412             styles.Add("微黑_9_V中H中_换行_全框细_淡蓝背景", cellStyle);
413
414
415             cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
416             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
417             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
418             cellStyle.SetFont(MsFont_9_Black_Font);
419             cellStyle.WrapText = (false);
420             cellStyle.IsLocked = false;
421             cellStyle.FillPattern = FillPattern.SolidForeground;
422             cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightTurquoise.Index;
423             cellStyle.DataFormat = (df.GetFormat("m-d h:mm"));
424             styles.Add("微黑_9_V中H中_不换行_全框细_淡蓝背景_日期", cellStyle);
425
426
427             NPOI.SS.UserModel.IFont Bold_13_Black_Font = wb.CreateFont();
428             Bold_13_Black_Font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
429             Bold_13_Black_Font.FontHeight = 13 * 20;      //字体高度,XLSX下有字体大小问题
430             //Bold_15_Black_Font.FontHeightInPoints = 10; //字体高度,XLS下有大小问题
431             Bold_13_Black_Font.FontName = "黑体";
432
433             NPOI.SS.UserModel.IFont Bold_15_Black_Font = wb.CreateFont();
434             Bold_15_Black_Font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
435             Bold_15_Black_Font.FontHeight = 15 * 20;      //字体高度,XLSX下有字体大小问题
436             //Bold_15_Black_Font.FontHeightInPoints = 10; //字体高度,XLS下有大小问题
437             Bold_15_Black_Font.FontName = "黑体";
438
439             NPOI.SS.UserModel.IFont MsFont_12_Black_Font = wb.CreateFont();
440             MsFont_12_Black_Font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Normal;
441             SetFontHeight(MsFont_12_Black_Font, 12);
442             MsFont_12_Black_Font.FontName = "微软雅黑";
443
444             cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
445             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
446             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
447             cellStyle.SetFont(Bold_13_Black_Font);
448             //cellStyle.WrapText = (false);//换行
449             styles.Add("Bold_13_Black_Font", cellStyle);
450
451             cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
452             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
453             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
454             cellStyle.SetFont(Bold_15_Black_Font);
455             //cellStyle.WrapText = (false);
456             styles.Add("Bold_15_Black_Font", cellStyle);
457
458
459             cellStyle = CreateBorderedStyle(wb, BorderStyle.Thin);
460             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
461             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
462             cellStyle.SetFont(MsFont_12_Black_Font);
463             //cellStyle.WrapText = (true);//换行
464             cellStyle.IsLocked = true;
465             styles.Add("微黑_12_V中H中_全框细_上锁", cellStyle);
466
467             return styles;
468         }
469
470         protected NPOI.SS.UserModel.ICellStyle CreateBorderedStyle(NPOI.SS.UserModel.IWorkbook wb,
471             NPOI.SS.UserModel.BorderStyle defaltBorderStyle = NPOI.SS.UserModel.BorderStyle.Thin)
472         {
473             NPOI.SS.UserModel.ICellStyle style = wb.CreateCellStyle();
474             style.BorderRight = defaltBorderStyle;
475             style.BorderBottom = defaltBorderStyle;
476             style.BorderLeft = defaltBorderStyle;
477             style.BorderTop = defaltBorderStyle;
478             style.RightBorderColor = (NPOI.SS.UserModel.IndexedColors.Black.Index);
479             style.BottomBorderColor = (NPOI.SS.UserModel.IndexedColors.Black.Index);
480             style.LeftBorderColor = (NPOI.SS.UserModel.IndexedColors.Black.Index);
481             style.TopBorderColor = (NPOI.SS.UserModel.IndexedColors.Black.Index);
482             return style;
483         }
90c6eb 484     }
J 485 }
1bf8ef 486