0

Flexcel ライブラリを使用していて、テーブル (Excel ドキュメント) のセルの色を変更したい。

どうすればいいですか?必要な API が見つかりません。Flexcellでできますか?

ここに画像の説明を入力

4

3 に答える 3

5

セルの背景色を設定しようとしたときにも同じ問題が発生しました。直接設定することはできませんでしたが、セルの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;
    }

これにより、4つの新しいスタイルが設定され、それらが追加された順序のインデックスを使用して参照できます。Flexcelのインデックスは1から始まります。

したがって、SetCellValueメソッドを使用してセルに値を追加するときに、追加したスタイルのインデックスを指定して、背景色を変更できます。これを行う方法の例として、以下を示します。

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

1がメソッドの最後の値である場合、この1は、上記のSetUpBackgroundColours()メソッドを使用してExcelファイルに追加した最初のスタイルを指します。

セルの色をライトグレーにしたい場合は、次のように1ではなく3を使用します。

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

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
于 2013-03-14T10:38:46.273 に答える
2

私はその API を使用していませんが、方法が見つからない場合は、C# で次のように実行できます。

    //http://www.mvps.org/dmcritchie/excel/colors.htm
    private void setCellColor(Microsoft.Office.Interop.Excel.Range cell, int index)
    {
        cell.Interior.ColorIndex = index;
    }
于 2012-06-06T14:53:40.730 に答える
0

色の適用には TFlxFormat クラスを使用できます。このクラスを使用すると、任意のフォーマットを適用できます。

于 2012-08-27T03:54:05.447 に答える