C# 使用NPOI出现超过最大字体数和单元格格式变成一样的解决
在使用NPOI写入Excel文件的时候出现“它已经超出最多允许的字体数”,查询资料发现是字体创建太多的原因,需要将常用字体创建好,传入CellStyle中。参考(http://www.cnblogs.com/sxdcgaq8080/p/7686895.html)
同时在修改的过程中,设置CellStyle出现了同一行设置不同的单元格格式,但是最后一整行的单元格样式都和最后一个设置的一样的问题。原因是获取cell.CellStyle直接进行设置,这时获取的全局默认的CellStyle也就影响到了其他单元格获取CellStyle进行设置,这就导致最后一个单元格设置的格式成为整行单元格的格式。参考(http://www.cnblogs.com/kingsleylam/p/5361365.html)
一下给我一个在我工程中为了解决这个问题实现的简单的类,用于在当前Workbook中获取设置好的字体和单元格样式:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using NPOI.SS.UserModel; 6 using NPOI.HSSF.UserModel; 7 8 namespace GenerateResult 9 { 10 class CellStyleHandle2 11 { 12 IWorkbook workBook ; 13 IFont defaultFont; 14 ICellStyle defaultCellStyle; 15 ICellStyle defaultCellStyleCenter; 16 ICellStyle defaultCellStyleRight; 17 18 IFont defaultFontColor;//蓝色 19 ICellStyle defaultCellStyleRightColor;//蓝色 20 21 public CellStyleHandle2(IWorkbook workBook) 22 { 23 this.workBook = workBook; 24 } 25 26 public IFont getDefaultFont() 27 { 28 if (null == defaultFont) 29 { 30 defaultFont = workBook.CreateFont();//内容输出用 31 //设置字体加粗样式 32 defaultFont.FontName = "宋体"; 33 defaultFont.IsBold = false; 34 defaultFont.FontHeightInPoints = 9; 35 defaultFont.Color = 0; 36 } 37 return defaultFont; 38 } 39 40 public ICellStyle getDefaultCellStyle() 41 { 42 if (null == defaultCellStyle) 43 { 44 defaultCellStyle = workBook.CreateCellStyle(); 45 defaultCellStyle.Alignment = HorizontalAlignment.Left; 46 defaultCellStyle.VerticalAlignment = VerticalAlignment.Center; 47 defaultCellStyle.WrapText = true; 48 49 ////使用SetFont方法将字体样式添加到单元格样式中 50 defaultCellStyle.SetFont(getDefaultFont()); 51 } 52 return defaultCellStyle; 53 } 54 55 public ICellStyle getDefaultCellStyleCenter() 56 { 57 if (null == defaultCellStyleCenter) 58 { 59 defaultCellStyleCenter = workBook.CreateCellStyle(); 60 defaultCellStyleCenter.Alignment = HorizontalAlignment.Center; 61 defaultCellStyleCenter.VerticalAlignment = VerticalAlignment.Center; 62 defaultCellStyleCenter.WrapText = true; 63 64 ////使用SetFont方法将字体样式添加到单元格样式中 65 defaultCellStyleCenter.SetFont(getDefaultFont()); 66 } 67 return defaultCellStyleCenter; 68 } 69 70 public ICellStyle getDefaultCellStyleRight() 71 { 72 if (null == defaultCellStyleRight) 73 { 74 defaultCellStyleRight = workBook.CreateCellStyle(); 75 defaultCellStyleRight.Alignment = HorizontalAlignment.Right; 76 defaultCellStyleRight.VerticalAlignment = VerticalAlignment.Center; 77 defaultCellStyleRight.WrapText = true; 78 79 ////使用SetFont方法将字体样式添加到单元格样式中 80 defaultCellStyleRight.SetFont(getDefaultFont()); 81 } 82 return defaultCellStyleRight; 83 } 84 85 public IFont getDefaultFontColor() 86 { 87 if (null == defaultFontColor) 88 { 89 defaultFontColor = workBook.CreateFont();//内容输出用 90 //设置字体加粗样式 91 defaultFontColor.FontName = "宋体"; 92 defaultFontColor.IsBold = false; 93 defaultFontColor.FontHeightInPoints = 9; 94 defaultFontColor.Color = 12; 95 } 96 return defaultFontColor; 97 } 98 99 public ICellStyle getDefaultCellStyleRightColor() 100 { 101 if (null == defaultCellStyleRightColor) 102 { 103 defaultCellStyleRightColor = workBook.CreateCellStyle(); 104 defaultCellStyleRightColor.Alignment = HorizontalAlignment.Right; 105 defaultCellStyleRightColor.VerticalAlignment = VerticalAlignment.Center; 106 defaultCellStyleRightColor.WrapText = true; 107 108 ////使用SetFont方法将字体样式添加到单元格样式中 109 defaultCellStyleRightColor.SetFont(getDefaultFontColor()); 110 } 111 return defaultCellStyleRightColor; 112 } 113 } 114 }
只针对当前Workbook有效。
转载于:https://www.cnblogs.com/fengyifeng/p/8697125.html
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
