C# Flexcel. Change the colour of background cell. Excell

笑着哭i 提交于 2019-12-25 05:15:08

问题


I am using Flexcel library and want to change the colour of the cell in the table (Excel document).

How can I do it? I can't found necessary API. Can I do it with Flexcell?


回答1:


I haven't used that API, but if you can't find a way, this is how you can do it with C#:

    //http://www.mvps.org/dmcritchie/excel/colors.htm
    private void setCellColor(Microsoft.Office.Interop.Excel.Range cell, int index)
    {
        cell.Interior.ColorIndex = index;
    }



回答2:


I had this same issue where I was trying to set the background colour of a cell. I never managed to set it directly but what I did manage to do was set the FillPattern of the cell which in effect set the background colour.

Here I have a private method that sets up the 4 different background colours I plan on using. Note that when you add in styles you add them to the Excel file directly. This is why I am passing in the excelFile to this method and then getting the excelFile back. You have to add the styles to the excelFile before you can use the styles.

        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;
    }

This will set up 4 new styles that I can then reference using the index of the order they were added in. The index with Flexcel starts with 1.

So then when you are adding values in your cells using the SetCellValue method, you can provide the index of the style you have added to change the background color. The following shows as example of how to do this:

excelFile.SetCellValue(rowNumber, columnNumber, "Some text for the cell", 1)

Where you see the 1 as the last value for the method, this 1 refers to the first style that I added to the Excel file using the SetUpBackgroundColours() method above.

If I wanted my cell to have a lighgray colour I would use 3 instead of 1 as follows:

excelFile.SetCellValue(rowNumber, columnNumber, "Some text for the cell", 3)

Some additional code that shows the call to 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



回答3:


You can use TFlxFormat class for applying color. Using this class You can apply any format you want.



来源:https://stackoverflow.com/questions/10916067/c-sharp-flexcel-change-the-colour-of-background-cell-excell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!