Change the background of Cells with C#

前端 未结 3 2138
走了就别回头了
走了就别回头了 2021-02-19 08:49

I\'m developing an program using C# to manipulate an Excel document, and I\'m using

    Microsoft.Office.Interop.Excel._Worksheet worksheet;

W

3条回答
  •  耶瑟儿~
    2021-02-19 09:10

    You can do it like this:

    private static readonly int DEL_PERF_FIRST_DATA_ROW = 10;
    private static readonly int SUN_ORDERS_COLUMN = 3;
    private static readonly int TUE_ORDERS_COLUMN = 5;
    private static readonly int THU_ORDERS_COLUMN = 7;
    private static readonly int SAT_ORDERS_COLUMN = 9;
    private static Color ALTERNATE_WEEKDAY_COLUMNS_COLOR = Color.LightGray;
    . . .
    int curRow = DEL_PERF_FIRST_DATA_ROW;
    

    (curRow gets incremented each time a row is written to the sheet.)

    // Pale Violetize (light gray, actually) Sun, Tues, Thurs, and Saturday columns
    var sundayColumnRange = _xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[DEL_PERF_FIRST_DATA_ROW, SUN_ORDERS_COLUMN],_xlSheetDelPerf.Cells[curRow - 1, SUN_ORDERS_COLUMN]];
    sundayColumnRange.Interior.Color = ALTERNATE_WEEKDAY_COLUMNS_COLOR;
    
    var tuesdayColumnRange = _xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[DEL_PERF_FIRST_DATA_ROW, TUE_ORDERS_COLUMN],_xlSheetDelPerf.Cells[curRow - 1, TUE_ORDERS_COLUMN]];
    tuesdayColumnRange.Interior.Color = ALTERNATE_WEEKDAY_COLUMNS_COLOR;
    
    var thursdayColumnRange = _xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[DEL_PERF_FIRST_DATA_ROW, THU_ORDERS_COLUMN],_xlSheetDelPerf.Cells[curRow - 1, THU_ORDERS_COLUMN]];
    thursdayColumnRange.Interior.Color = ALTERNATE_WEEKDAY_COLUMNS_COLOR;
    
    var saturdayColumnRange =_xlSheetDelPerf.Range[_xlSheetDelPerf.Cells[DEL_PERF_FIRST_DATA_ROW, SAT_ORDERS_COLUMN], _xlSheetDelPerf.Cells[curRow - 1, SAT_ORDERS_COLUMN]];
    saturdayColumnRange.Interior.Color = ALTERNATE_WEEKDAY_COLUMNS_COLOR;
    

提交回复
热议问题