use*_*447 0 c# cell background-color
我正在使用Flexcel库,并希望更改表格中的单元格颜色(Excel文档).
我该怎么做?我找不到必要的API.我可以使用Flexcell吗?

我有同样的问题,我试图设置一个单元格的背景颜色.我从来没有设法直接设置它,但我设法做的是设置实际设置背景颜色的单元格的FillPattern.
在这里,我有一个私有方法,设置我计划使用的4种不同的背景颜色.请注意,添加样式时,可以直接将它们添加到Excel文件中.这就是为什么我将excelFile传递给此方法,然后将excelFile返回.您必须先将样式添加到excelFile,然后才能使用样式.
private XlsFile SetUpBackgroundColours(XlsFile excelFile)
{
var patternStyle = TFlxPatternStyle.Solid;
TFlxFormat format = excelFile.GetDefaultFormat;
format.FillPattern = new TFlxFillPattern { Pattern = patternStyle, FgColor = Color.Yellow }; //1
format.VAlignment = TVFlxAlignment.top;
format.HAlignment = THFlxAlignment.left;
excelFile.AddFormat(format);
format.FillPattern = new TFlxFillPattern { Pattern = patternStyle, FgColor = Color.LightBlue }; //2
format.VAlignment = TVFlxAlignment.top;
format.HAlignment = THFlxAlignment.left;
excelFile.AddFormat(format);
format.FillPattern = new TFlxFillPattern { Pattern = patternStyle, FgColor = Color.LightGray }; //3
format.VAlignment = TVFlxAlignment.top;
format.HAlignment = THFlxAlignment.left;
excelFile.AddFormat(format);
format.FillPattern = new TFlxFillPattern { Pattern = patternStyle, FgColor = Color.White }; //4
format.VAlignment = TVFlxAlignment.top;
format.HAlignment = THFlxAlignment.left;
excelFile.AddFormat(format);
return excelFile;
}
Run Code Online (Sandbox Code Playgroud)
这将设置4个新样式,然后我可以使用它们添加的顺序索引来引用.带有Flexcel的索引从1开始.
因此,当您使用SetCellValue方法在单元格中添加值时,可以提供已添加的样式的索引以更改背景颜色.以下显示了如何执行此操作的示例:
excelFile.SetCellValue(rowNumber, columnNumber, "Some text for the cell", 1)
Run Code Online (Sandbox Code Playgroud)
如果您将1视为方法的最后一个值,则此1指的是我使用上面的SetUpBackgroundColours()方法添加到Excel文件的第一个样式.
如果我希望我的细胞具有浅灰色,我会使用3而不是1,如下所示:
excelFile.SetCellValue(rowNumber, columnNumber, "Some text for the cell", 3)
Run Code Online (Sandbox Code Playgroud)
一些显示对SetUpBackgroundColours调用的其他代码:
var excelFile = new XlsFile(true);
excelFile.NewFile(someList.Count() + 1, TExcelFileFormat.v2007);
var sheetIndex = 0;
excelFile = SetUpBackgroundColours(excelFile);
excelFile.SetCellValue(1, 1, "Some text for the cell A1", 1) //cell colour will be yellow
excelFile.SetCellValue(2, 2, "Some text for the cell B1", 2) //cell colour will be lightblue
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3733 次 |
| 最近记录: |