如何使用ExcelLibrary更改Excel文件中的单元格样式? [英] How can I change cell style in an Excel file with ExcelLibrary?
问题描述
我想设置一个单元格背景和字体颜色,但我不知道该怎么做。
我尝试访问一个单元格样式,但是我没有找到它。
有任何想法吗?
我已经看过这个图书馆,发现以下内容(警告 - 这是坏消息!):
-
没有发布版本的 ExcelLibrary 允许访问单元格颜色。
-
在未发布的源代码中,有一个
BackColor
属性新的CellStyle
类,但是没有表示前景颜色的属性。 -
BackColor
属性在保存工作簿时不会保留。它仅用于在加载工作簿时设置单元格的背景颜色。
如果使用颜色然后使用 NPOI (根据@jamietre的建议)的要求。然后,您可以设置这样的前景和背景颜色:
HSSFCellStyle style1 = hssfworkbook.CreateCellStyle();
//单元格背景
style1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.BLUE.index;
style1.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;
//字体颜色
HSSFFont font1 = hssfworkbook.CreateFont();
font1.Color = NPOI.HSSF.Util.HSSFColor.YELLOW.index;
style1.SetFont(font1);
cell.CellStyle = style1;
Can anybody help me with ExcelLibrary? I'd like to set a cell background and font color, but I don't know how can I do it. I try to get access to a cell style, but I didn't found it.
Anybody have any ideas?
I've looked into this library for you and found the following (warning - it's bad news!):
There is no released version of ExcelLibrary that allows access to cell colours.
In the unreleased source code there is a
BackColor
property in the newCellStyle
class, however there is no property to represent foreground colour.The
BackColor
property is not persisted when the workbook is saved. It is only used to set the background colour of a cell when the workbook is loaded.
If use of colours is a requirement then use NPOI (as recommended by @jamietre). You can then set foreground and background colours like this:
HSSFCellStyle style1 = hssfworkbook.CreateCellStyle();
// cell background
style1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.BLUE.index;
style1.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;
// font color
HSSFFont font1 = hssfworkbook.CreateFont();
font1.Color = NPOI.HSSF.Util.HSSFColor.YELLOW.index;
style1.SetFont(font1);
cell.CellStyle = style1;
这篇关于如何使用ExcelLibrary更改Excel文件中的单元格样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!