样式

parent 5ac214da
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Text;
namespace Performance.Services
{
public enum StyleType
{
//列头固定,
列头,
系数,
默认,
}
public enum CellFormat
{
数字,
数字2,
百分比,
百分比2,
时间,
默认,
}
public class CellStyle
{
public static ICellStyle CreateCellStyle(IWorkbook wb, StyleType type = StyleType.默认, CellFormat format = CellFormat.默认)
{
ICellStyle cellStyle = wb.CreateCellStyle();
//字体
IFont font = wb.CreateFont();
font.FontHeightInPoints = 11;
font.FontName = "微软雅黑";
font.Color = HSSFColor.Black.Index;
//边框
cellStyle.BorderBottom = BorderStyle.Thin;
cellStyle.BorderLeft = BorderStyle.Thin;
cellStyle.BorderRight = BorderStyle.Thin;
cellStyle.BorderTop = BorderStyle.Thin;
//边框颜色
cellStyle.BottomBorderColor = HSSFColor.Black.Index;
cellStyle.TopBorderColor = HSSFColor.Black.Index;
cellStyle.LeftBorderColor = HSSFColor.Black.Index;
cellStyle.RightBorderColor = HSSFColor.Black.Index;
cellStyle.SetFont(font);
switch (type)
{
case StyleType.列头:
cellStyle.FillForegroundColor = HSSFColor.Orange.Index;
break;
case StyleType.系数:
cellStyle.FillForegroundColor = HSSFColor.Green.Index;
break;
case StyleType.默认:
cellStyle.SetFont(font);
break;
}
IDataFormat datastyle = wb.CreateDataFormat();
switch (format)
{
case CellFormat.时间:
cellStyle.DataFormat = datastyle.GetFormat("yyyy/mm/dd");
break;
case CellFormat.数字:
cellStyle.DataFormat = datastyle.GetFormat("0");
break;
case CellFormat.数字2:
cellStyle.DataFormat = datastyle.GetFormat("0.00");
break;
case CellFormat.百分比:
cellStyle.DataFormat = datastyle.GetFormat("0%");
break;
case CellFormat.百分比2:
cellStyle.DataFormat = datastyle.GetFormat("0.00%");
break;
case CellFormat.默认:
cellStyle.SetFont(font);
break;
}
return cellStyle;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment