123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- /* ==============================================================================
- * 功能描述:NPOI扩展
- * 创 建 者:Garrett
- * 创建日期:2018/11/5 9:55:33
- * ==============================================================================*/
- using NPOI.HSSF.Util;
- using NPOI.SS.UserModel;
- using NPOI.SS.Util;
- namespace Saga.PlugIn.ModelCheck
- {
- /// <summary>
- /// NPOIStyle
- /// </summary>
- public static class DCNPOIExtend
- {
- /// <summary>
- /// 创建一个指定格式的单元格,并向其添加内容
- /// </summary>
- /// <param name="row"></param>
- /// <param name="column"></param>
- /// <param name="str"></param>
- /// <param name="style"></param>
- public static void AddCell(this IRow row, int column, string str, DataCheckNPOIStyle style = DataCheckNPOIStyle.Content, bool hasStyle = false)
- {
- ICell cell00 = row.GetCell(column);
- if (cell00 == null)
- cell00 = row.CreateCell(column);
- if (hasStyle)
- cell00.CellStyle = DCNPOIExtend.GetCellStyle(row.Sheet.Workbook, style);
- cell00.SetCellType(NPOI.SS.UserModel.CellType.String);
- cell00.SetCellValue(str);
- }
- /// <summary>
- /// 合并单元格,并添加边框
- /// </summary>
- /// <param name="sheet"></param>
- /// <param name="row0"></param>
- /// <param name="row1"></param>
- /// <param name="col0"></param>
- /// <param name="col1"></param>
- public static void MarginCellAndBorder(this ISheet sheet, int row0, int row1, int col0, int col1)
- {
- var range = new CellRangeAddress(row0, row1, col0, col1);
- sheet.AddMergedRegion(range);
- //添加左对齐 剧中样式
- //合并后不加边框,取消样式
- //var style = DCNPOIExtend.GetCellStyle(sheet.Workbook);
- //for (int i = range.FirstRow; i <= range.LastRow; i++)
- //{
- // IRow row = CellUtil.GetRow(i, sheet);
- // for (int j = range.FirstColumn; j <= range.LastColumn; j++)
- // {
- // var cell = CellUtil.GetCell(row, j);
- // cell.CellStyle = style;
- // }
- //}
- }
- /// <summary>
- /// 获取单元格样式
- /// </summary>
- /// <param name="workbook"></param>
- /// <param name="style"></param>
- /// <returns></returns>
- private static ICellStyle GetCellStyle(this IWorkbook workbook, DataCheckNPOIStyle style = DataCheckNPOIStyle.Content)
- {
- ICellStyle cellstyle = null;
- switch (style)
- {
- case DataCheckNPOIStyle.Title:
- cellstyle = LeftCenterBlackYellowStyle(workbook);
- break;
- case DataCheckNPOIStyle.Error:
- cellstyle = LeftCenterRedStyle(workbook);
- break;
- case DataCheckNPOIStyle.HyperLink:
- cellstyle = HyperLinkStyle(workbook);
- break;
- default:
- cellstyle = LeftCenterBlackStyle(workbook);
- break;
- }
- return cellstyle;
- }
- /// <summary>
- /// 设置样式为超级链接
- /// </summary>
- /// <param name="workbook"></param>
- /// <returns></returns>
- private static ICellStyle HyperLinkStyle(IWorkbook workbook)
- {
- ICellStyle style = workbook.CreateCellStyle();
- //设置单元格的样式:水平对齐左侧垂直居中
- style.Alignment = HorizontalAlignment.Left;
- style.VerticalAlignment = VerticalAlignment.Center;
- //新建一个字体样式对象
- IFont font = workbook.CreateFont();
- font.Underline = FontUnderlineType.Single;
- //设置字体颜色
- font.Color = HSSFColor.Blue.Index;
- font.FontHeight = 12;
- //使用SetFont方法将字体样式添加到单元格样式中
- style.SetFont(font);
- //将新的样式赋给单元格
- return style;
- }
- /// <summary>
- /// 设置样式为水平左侧垂直居中,红色字体
- /// </summary>
- /// <param name="workbook"></param>
- /// <returns></returns>
- private static ICellStyle LeftCenterRedStyle(IWorkbook workbook)
- {
- ICellStyle style = workbook.CreateCellStyle();
- //设置单元格的样式:水平对齐左侧垂直居中
- style.Alignment = HorizontalAlignment.Left;
- style.VerticalAlignment = VerticalAlignment.Center;
- style.WrapText = true;
- style.BorderBottom = BorderStyle.Thin;
- style.BorderLeft = BorderStyle.Thin;
- style.BorderRight = BorderStyle.Thin;
- style.BorderTop = BorderStyle.Thin;
- //新建一个字体样式对象
- IFont font = workbook.CreateFont();
- //设置字体颜色
- font.Color = HSSFColor.Red.Index;
- font.FontHeight = 12;
- //使用SetFont方法将字体样式添加到单元格样式中
- style.SetFont(font);
- //将新的样式赋给单元格
- return style;
- }
- /// <summary>
- /// 设置样式为水平左侧垂直居中,黑色字体
- /// </summary>
- /// <param name="workbook"></param>
- /// <returns></returns>
- private static ICellStyle LeftCenterBlackStyle(IWorkbook workbook)
- {
- ICellStyle style = workbook.CreateCellStyle();
- //设置单元格的样式:水平对齐居中
- style.Alignment = HorizontalAlignment.Left;
- style.VerticalAlignment = VerticalAlignment.Center;
- style.WrapText = true;
- //style.BorderBottom = BorderStyle.Thin;
- //style.BorderLeft = BorderStyle.Thin;
- //style.BorderRight = BorderStyle.Thin;
- //style.BorderTop = BorderStyle.Thin;
- //新建一个字体样式对象
- //IFont font = workbook.CreateFont();
- ////设置字体颜色
- //font.Color = HSSFColor.Black.Index;
- //font.FontHeight = 12;
- ////使用SetFont方法将字体样式添加到单元格样式中
- //style.SetFont(font);
- //将新的样式赋给单元格
- return style;
- }
- /// <summary>
- /// 设置样式为水平左侧垂直居中,黑色字体,黄色背景
- /// </summary>
- /// <param name="workbook"></param>
- /// <returns></returns>
- private static ICellStyle LeftCenterBlackYellowStyle(IWorkbook workbook)
- {
- ICellStyle style = workbook.CreateCellStyle();
- //设置单元格的样式:水平对齐居中
- style.Alignment = HorizontalAlignment.Left;
- style.VerticalAlignment = VerticalAlignment.Center;
- style.WrapText = true;
- style.BorderBottom = BorderStyle.Thin;
- style.BorderLeft = BorderStyle.Thin;
- style.BorderRight = BorderStyle.Thin;
- style.BorderTop = BorderStyle.Thin;
- style.FillForegroundColor = HSSFColor.LightYellow.Index;
- style.FillPattern = FillPattern.SolidForeground;
- //新建一个字体样式对象
- //IFont font = workbook.CreateFont();
- ////设置字体颜色
- //font.Color = HSSFColor.Black.Index;
- //font.FontHeight = 12;
- ////使用SetFont方法将字体样式添加到单元格样式中
- //style.SetFont(font);
- //将新的样式赋给单元格
- return style;
- }
- /// <summary>
- /// 黑色边框样式
- /// </summary>
- /// <param name="workbook"></param>
- /// <returns></returns>
- private static ICellStyle ThinBorderStyle(this IWorkbook workbook)
- {
- ICellStyle style = workbook.CreateCellStyle();
- style.BorderBottom = BorderStyle.Thin;
- style.BorderLeft = BorderStyle.Thin;
- style.BorderRight = BorderStyle.Thin;
- style.BorderTop = BorderStyle.Thin;
- return style;
- }
- }
- public enum DataCheckNPOIStyle
- {
- Title,
- Content,
- Error,
- HyperLink,
- None
- }
- }
|