DCNPOIExtend.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* ==============================================================================
  2. * 功能描述:NPOI扩展
  3. * 创 建 者:Garrett
  4. * 创建日期:2018/11/5 9:55:33
  5. * ==============================================================================*/
  6. using NPOI.HSSF.Util;
  7. using NPOI.SS.UserModel;
  8. using NPOI.SS.Util;
  9. namespace Saga.PlugIn.ModelCheck
  10. {
  11. /// <summary>
  12. /// NPOIStyle
  13. /// </summary>
  14. public static class DCNPOIExtend
  15. {
  16. /// <summary>
  17. /// 创建一个指定格式的单元格,并向其添加内容
  18. /// </summary>
  19. /// <param name="row"></param>
  20. /// <param name="column"></param>
  21. /// <param name="str"></param>
  22. /// <param name="style"></param>
  23. public static void AddCell(this IRow row, int column, string str, DataCheckNPOIStyle style = DataCheckNPOIStyle.Content, bool hasStyle = false)
  24. {
  25. ICell cell00 = row.GetCell(column);
  26. if (cell00 == null)
  27. cell00 = row.CreateCell(column);
  28. if (hasStyle)
  29. cell00.CellStyle = DCNPOIExtend.GetCellStyle(row.Sheet.Workbook, style);
  30. cell00.SetCellType(NPOI.SS.UserModel.CellType.String);
  31. cell00.SetCellValue(str);
  32. }
  33. /// <summary>
  34. /// 合并单元格,并添加边框
  35. /// </summary>
  36. /// <param name="sheet"></param>
  37. /// <param name="row0"></param>
  38. /// <param name="row1"></param>
  39. /// <param name="col0"></param>
  40. /// <param name="col1"></param>
  41. public static void MarginCellAndBorder(this ISheet sheet, int row0, int row1, int col0, int col1)
  42. {
  43. var range = new CellRangeAddress(row0, row1, col0, col1);
  44. sheet.AddMergedRegion(range);
  45. //添加左对齐 剧中样式
  46. //合并后不加边框,取消样式
  47. //var style = DCNPOIExtend.GetCellStyle(sheet.Workbook);
  48. //for (int i = range.FirstRow; i <= range.LastRow; i++)
  49. //{
  50. // IRow row = CellUtil.GetRow(i, sheet);
  51. // for (int j = range.FirstColumn; j <= range.LastColumn; j++)
  52. // {
  53. // var cell = CellUtil.GetCell(row, j);
  54. // cell.CellStyle = style;
  55. // }
  56. //}
  57. }
  58. /// <summary>
  59. /// 获取单元格样式
  60. /// </summary>
  61. /// <param name="workbook"></param>
  62. /// <param name="style"></param>
  63. /// <returns></returns>
  64. private static ICellStyle GetCellStyle(this IWorkbook workbook, DataCheckNPOIStyle style = DataCheckNPOIStyle.Content)
  65. {
  66. ICellStyle cellstyle = null;
  67. switch (style)
  68. {
  69. case DataCheckNPOIStyle.Title:
  70. cellstyle = LeftCenterBlackYellowStyle(workbook);
  71. break;
  72. case DataCheckNPOIStyle.Error:
  73. cellstyle = LeftCenterRedStyle(workbook);
  74. break;
  75. case DataCheckNPOIStyle.HyperLink:
  76. cellstyle = HyperLinkStyle(workbook);
  77. break;
  78. default:
  79. cellstyle = LeftCenterBlackStyle(workbook);
  80. break;
  81. }
  82. return cellstyle;
  83. }
  84. /// <summary>
  85. /// 设置样式为超级链接
  86. /// </summary>
  87. /// <param name="workbook"></param>
  88. /// <returns></returns>
  89. private static ICellStyle HyperLinkStyle(IWorkbook workbook)
  90. {
  91. ICellStyle style = workbook.CreateCellStyle();
  92. //设置单元格的样式:水平对齐左侧垂直居中
  93. style.Alignment = HorizontalAlignment.Left;
  94. style.VerticalAlignment = VerticalAlignment.Center;
  95. //新建一个字体样式对象
  96. IFont font = workbook.CreateFont();
  97. font.Underline = FontUnderlineType.Single;
  98. //设置字体颜色
  99. font.Color = HSSFColor.Blue.Index;
  100. font.FontHeight = 12;
  101. //使用SetFont方法将字体样式添加到单元格样式中
  102. style.SetFont(font);
  103. //将新的样式赋给单元格
  104. return style;
  105. }
  106. /// <summary>
  107. /// 设置样式为水平左侧垂直居中,红色字体
  108. /// </summary>
  109. /// <param name="workbook"></param>
  110. /// <returns></returns>
  111. private static ICellStyle LeftCenterRedStyle(IWorkbook workbook)
  112. {
  113. ICellStyle style = workbook.CreateCellStyle();
  114. //设置单元格的样式:水平对齐左侧垂直居中
  115. style.Alignment = HorizontalAlignment.Left;
  116. style.VerticalAlignment = VerticalAlignment.Center;
  117. style.WrapText = true;
  118. style.BorderBottom = BorderStyle.Thin;
  119. style.BorderLeft = BorderStyle.Thin;
  120. style.BorderRight = BorderStyle.Thin;
  121. style.BorderTop = BorderStyle.Thin;
  122. //新建一个字体样式对象
  123. IFont font = workbook.CreateFont();
  124. //设置字体颜色
  125. font.Color = HSSFColor.Red.Index;
  126. font.FontHeight = 12;
  127. //使用SetFont方法将字体样式添加到单元格样式中
  128. style.SetFont(font);
  129. //将新的样式赋给单元格
  130. return style;
  131. }
  132. /// <summary>
  133. /// 设置样式为水平左侧垂直居中,黑色字体
  134. /// </summary>
  135. /// <param name="workbook"></param>
  136. /// <returns></returns>
  137. private static ICellStyle LeftCenterBlackStyle(IWorkbook workbook)
  138. {
  139. ICellStyle style = workbook.CreateCellStyle();
  140. //设置单元格的样式:水平对齐居中
  141. style.Alignment = HorizontalAlignment.Left;
  142. style.VerticalAlignment = VerticalAlignment.Center;
  143. style.WrapText = true;
  144. //style.BorderBottom = BorderStyle.Thin;
  145. //style.BorderLeft = BorderStyle.Thin;
  146. //style.BorderRight = BorderStyle.Thin;
  147. //style.BorderTop = BorderStyle.Thin;
  148. //新建一个字体样式对象
  149. //IFont font = workbook.CreateFont();
  150. ////设置字体颜色
  151. //font.Color = HSSFColor.Black.Index;
  152. //font.FontHeight = 12;
  153. ////使用SetFont方法将字体样式添加到单元格样式中
  154. //style.SetFont(font);
  155. //将新的样式赋给单元格
  156. return style;
  157. }
  158. /// <summary>
  159. /// 设置样式为水平左侧垂直居中,黑色字体,黄色背景
  160. /// </summary>
  161. /// <param name="workbook"></param>
  162. /// <returns></returns>
  163. private static ICellStyle LeftCenterBlackYellowStyle(IWorkbook workbook)
  164. {
  165. ICellStyle style = workbook.CreateCellStyle();
  166. //设置单元格的样式:水平对齐居中
  167. style.Alignment = HorizontalAlignment.Left;
  168. style.VerticalAlignment = VerticalAlignment.Center;
  169. style.WrapText = true;
  170. style.BorderBottom = BorderStyle.Thin;
  171. style.BorderLeft = BorderStyle.Thin;
  172. style.BorderRight = BorderStyle.Thin;
  173. style.BorderTop = BorderStyle.Thin;
  174. style.FillForegroundColor = HSSFColor.LightYellow.Index;
  175. style.FillPattern = FillPattern.SolidForeground;
  176. //新建一个字体样式对象
  177. //IFont font = workbook.CreateFont();
  178. ////设置字体颜色
  179. //font.Color = HSSFColor.Black.Index;
  180. //font.FontHeight = 12;
  181. ////使用SetFont方法将字体样式添加到单元格样式中
  182. //style.SetFont(font);
  183. //将新的样式赋给单元格
  184. return style;
  185. }
  186. /// <summary>
  187. /// 黑色边框样式
  188. /// </summary>
  189. /// <param name="workbook"></param>
  190. /// <returns></returns>
  191. private static ICellStyle ThinBorderStyle(this IWorkbook workbook)
  192. {
  193. ICellStyle style = workbook.CreateCellStyle();
  194. style.BorderBottom = BorderStyle.Thin;
  195. style.BorderLeft = BorderStyle.Thin;
  196. style.BorderRight = BorderStyle.Thin;
  197. style.BorderTop = BorderStyle.Thin;
  198. return style;
  199. }
  200. }
  201. public enum DataCheckNPOIStyle
  202. {
  203. Title,
  204. Content,
  205. Error,
  206. HyperLink,
  207. None
  208. }
  209. }