/* ==============================================================================
* 功能描述:NPOI扩展
* 创 建 者:Garrett
* 创建日期:2018/11/5 9:55:33
* ==============================================================================*/
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
namespace Saga.PlugIn.ModelCheck
{
///
/// NPOIStyle
///
public static class DCNPOIExtend
{
///
/// 创建一个指定格式的单元格,并向其添加内容
///
///
///
///
///
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);
}
///
/// 合并单元格,并添加边框
///
///
///
///
///
///
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;
// }
//}
}
///
/// 获取单元格样式
///
///
///
///
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;
}
///
/// 设置样式为超级链接
///
///
///
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;
}
///
/// 设置样式为水平左侧垂直居中,红色字体
///
///
///
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;
}
///
/// 设置样式为水平左侧垂直居中,黑色字体
///
///
///
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;
}
///
/// 设置样式为水平左侧垂直居中,黑色字体,黄色背景
///
///
///
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;
}
///
/// 黑色边框样式
///
///
///
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
}
}