有什么方法可以使用 NPOI 在我的 excel 文档中从十六进制值(如“#72fe9c”)向单元格添加颜色
Is there any way to add color to cell from hex value like '#72fe9c' in my excel doc using NPOI
因为我是 NPOI 的新手,所以我想为 excel sheet 中的单元格添加颜色。我有一个像“#ffeeff”这样的十六进制值,在 ICellStyle.FillForegroundColor
中只能分配短整型值。
System.OverflowException: Value was either too large or too small for an Int16.
我已经尝试过这样的代码并且它正在工作
style.FillForegroundColor = HSSFColor.Grey25Percent.Index;
但我只有十六进制值可以转换为整数,但它只支持短整型值。
//it is working
style.FillForegroundColor = HSSFColor.Grey25Percent.Index;
// not working for me as '#ffeeff' canot be converted to short, it can only be converted to int
style.FillForegroundColor = short.Parse(fontcolorCode.Substring(1), NumberStyles.HexNumber)
style.FillForegroundColor = short.Parse(fontcolorCode.Substring(1), NumberStyles.HexNumber)
它不应引发错误,并且在 excel sheet 中,必须对单元格应用相同的颜色 (fontcolorCode)
short
(Int16
) 对于这个值来说不够大。取 int
(Int32
) 代替:
string myHexColor = "#ffeeff";
int x = int.Parse(myHexColor.Substring(1), NumberStyles.HexNumber);
Console.WriteLine("Color is: " + x); // 16772863
Console.WriteLine("Max short: " + short.MaxValue); // 32767
Console.WriteLine("Max int: " + int.MaxValue); // 2147483647
您必须创建一个颜色对象:
string myHexColor = "#ffeeff";
byte r = Convert.ToByte(myHexColor.Substring(1, 2).ToUpper(), 16);
byte g = Convert.ToByte(myHexColor.Substring(3, 2), 16);
byte b = Convert.ToByte(myHexColor.Substring(5, 2), 16);
Console.WriteLine("{0} = {1}/{2}/{3}", myHexColor, r, g, b);
IWorkbook workbook = null;
NPOI.XSSF.UserModel.XSSFCellStyle style = (NPOI.XSSF.UserModel.XSSFCellStyle)workbook.CreateCellStyle();
// Here we create a color from RGB-values
IColor color = new NPOI.XSSF.UserModel.XSSFColor(new byte[] { r, g, b });
style.SetFillForegroundColor(color );
ICell cellToPaint = null; // Select your cell..
cellToPaint.CellStyle = style;
因为我是 NPOI 的新手,所以我想为 excel sheet 中的单元格添加颜色。我有一个像“#ffeeff”这样的十六进制值,在 ICellStyle.FillForegroundColor
中只能分配短整型值。
System.OverflowException: Value was either too large or too small for an Int16.
我已经尝试过这样的代码并且它正在工作
style.FillForegroundColor = HSSFColor.Grey25Percent.Index;
但我只有十六进制值可以转换为整数,但它只支持短整型值。
//it is working
style.FillForegroundColor = HSSFColor.Grey25Percent.Index;
// not working for me as '#ffeeff' canot be converted to short, it can only be converted to int
style.FillForegroundColor = short.Parse(fontcolorCode.Substring(1), NumberStyles.HexNumber)
style.FillForegroundColor = short.Parse(fontcolorCode.Substring(1), NumberStyles.HexNumber)
它不应引发错误,并且在 excel sheet 中,必须对单元格应用相同的颜色 (fontcolorCode)
short
(Int16
) 对于这个值来说不够大。取 int
(Int32
) 代替:
string myHexColor = "#ffeeff";
int x = int.Parse(myHexColor.Substring(1), NumberStyles.HexNumber);
Console.WriteLine("Color is: " + x); // 16772863
Console.WriteLine("Max short: " + short.MaxValue); // 32767
Console.WriteLine("Max int: " + int.MaxValue); // 2147483647
您必须创建一个颜色对象:
string myHexColor = "#ffeeff";
byte r = Convert.ToByte(myHexColor.Substring(1, 2).ToUpper(), 16);
byte g = Convert.ToByte(myHexColor.Substring(3, 2), 16);
byte b = Convert.ToByte(myHexColor.Substring(5, 2), 16);
Console.WriteLine("{0} = {1}/{2}/{3}", myHexColor, r, g, b);
IWorkbook workbook = null;
NPOI.XSSF.UserModel.XSSFCellStyle style = (NPOI.XSSF.UserModel.XSSFCellStyle)workbook.CreateCellStyle();
// Here we create a color from RGB-values
IColor color = new NPOI.XSSF.UserModel.XSSFColor(new byte[] { r, g, b });
style.SetFillForegroundColor(color );
ICell cellToPaint = null; // Select your cell..
cellToPaint.CellStyle = style;