【1】XSSFCellStyle以及ICellStyle自定义颜色
NPOI.XSSF.UserModel.XSSFWorkbook xssfworkbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
方案1:>>XSSFCellStyle
XSSFCellStyle rowsStyleColor = (XSSFCellStyle)xssfworkbook.CreateCellStyle();
XSSFColor xssfColor = new XSSFColor();
//根据自己需要设置RGB
byte[] colorRgb = { (byte)252, (byte)139, (byte)139 };
xssfColor.SetRgb(colorRgb);
rowsStyleColor.FillForegroundColorColor = xssfColor;
rowsStyleColor.FillPattern = FillPattern.SolidForeground;
方案2:>>ICellStyle
ICellStyle rowsStyleColor = (XSSFCellStyle)xssfworkbook.CreateCellStyle();
rowsStyleColor.WrapText = true; 换行
//设置背景颜色...
rowsStyleColor.FillForegroundColor = 0;
rowsStyleColor.FillPattern = FillPattern.SolidForeground;
((XSSFColor)rowsStyleColor.FillForegroundColorColor).SetRgb(new byte[] { 252, 139, 139 });
【2】XSSFWorkbook设置背景色填充色的方法
方案一
//设置预定义填充颜色
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
方案二
//设置自定义填充颜色
style.setFillForegroundColor(new XSSFColor(new Color(255,255,255)));
【总结】
//加边框线居中 以及背景色 字体 显示数字格式
public ICellStyle BorderFormat_Center(XSSFWorkbook workBookName)
{
ICellStyle cellStyle = workBookName.CreateCellStyle();
cellStyle.Alignment = HorizontalAlignment.Center;居中
cellStyle.VerticalAlignment = VerticalAlignment.Center; 垂直居中
cellStyle.BorderBottom = BorderStyle.Thin;边框样式
cellStyle.BorderLeft = BorderStyle.Thin;
cellStyle.BorderRight = BorderStyle.Thin;
cellStyle.BorderTop = BorderStyle.Thin;
cellStyle.FillPattern = FillPattern.SolidForeground;
cellStyle.DataFormat = 176; 格式
cellStyle.FillForegroundColor = 0;
((XSSFColor)cellStyle.FillForegroundColorColor).SetRgb(new byte[] { 220, 230, 241 });
IFont f = workBookName.CreateFont();
f.Boldweight = (short)FontBoldWeight.Bold;
f.FontHeightInPoints = 11;
f.FontName = "宋体";
cellStyle.SetFont(f); 设置字体
return cellStyle;
}
// 合并单元格
public void Merged(int FirstRow, int LastRow, int FirstCol, int LastCol, ISheet Sheet, ICellStyle Style)
{
CellRangeAddress mCellRangeAddress = new CellRangeAddress(pFirstRow, pLastRow, pFirstCol, pLastCol);
pSheet.AddMergedRegion(mCellRangeAddress);
}
InsertRowAndMerged insertRowAndMerged = new InsertRowAndMerged();
//行起始数 行结束数 单元格起始数 单元格结束数 sheet页 单元格样式 insertRowAndMerged.MergedObject(0, 2, 2, 2, sheet01, CellStyle);
public class InsertRowAndMerged
{
//插入行
public void insertRow(XSSFWorkbook wb, ISheet sheet, int starRow, int rows)
{
/*
* ShiftRows(int startRow, int endRow, int n, bool copyRowHeight, bool resetOriginalRowHeight);
*
* startRow 开始行
* endRow 结束行
* n 移动行数
* copyRowHeight 复制的行是否高度在移
* resetOriginalRowHeight 是否设置为默认的原始行的高度
*
*/
sheet.ShiftRows(starRow + 1, sheet.LastRowNum, rows, true, true);
starRow = starRow - 1;
for (int i = 0; i < rows; i++)
{
IRow sourceRow = null;
IRow targetRow = null;
ICell sourceCell = null;
ICell targetCell = null;
short m;
starRow = starRow + 1;
sourceRow = sheet.GetRow(starRow);
//targetRow = sheet.CopyRow(0, 1);
targetRow = sheet.CreateRow(starRow + 1);
targetRow.HeightInPoints = sourceRow.HeightInPoints;
for (m = (short)sourceRow.FirstCellNum; m < sourceRow.LastCellNum; m++)
{
sourceCell = sourceRow.GetCell(m);
targetCell = targetRow.CreateCell(m);
//targetCell.Encoding = sourceCell.Encoding;
targetCell.CellStyle = sourceCell.CellStyle;
targetCell.SetCellType(sourceCell.CellType);
}
}
}
}