How can fill I cells A1:A5 with a color using C#?

前端 未结 3 1998
醉酒成梦
醉酒成梦 2020-12-21 19:47

I have the following code:

Excel.Range chartRange;
chartRange = xlWorkSheet.get_Range(\"A3\", \"R3\");

I want to fill this range of cells w

3条回答
  •  失恋的感觉
    2020-12-21 20:10

    You are directly assigning the color to the worksheet variable which Excel interop cant understand.

    So you need to convert those color values to OLE values by using colorTranslator.ToOle method or use Excel way of coloring it. Provided both the ways.

    xlWorkSheet.get_Range(xlWorkSheet.Cells[2, 2], xlWorkSheet.Cells[2, 4]).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);
    

    else

    xlWorkSheet.get_Range(xlWorkSheet.Cells[2, 3], xlWorkSheet.Cells[2, 3]).Interior.Color = Excel.XlRgbColor.rgbRed;
    

    Here xlWorksheet is the object excel Worksheet object.

    get_Range takes 2 variable one start cell and other is end cell.

    so if you specify both the values same then only one cell is colored.

    xlWorkSheet.cells[row, column] is used to specify a cell.

    System.Drawing.ColorTranslator.ToOle(SystemDrawing.Color.Green) is used to define the color in OLE format.

    Excel.XlRgbColor.rgbRed is a excel way of coloring the cells This method gives access to large number of colors which can be found here list of colors

提交回复
热议问题