excel cell coloring

前端 未结 6 1795
刺人心
刺人心 2020-12-06 12:06

I am using c# to color particular cells of excel file. I am using:

Application excel = new Application();
Workbook wb = excel.Workbooks.Open(destPath);
 Work         


        
6条回答
  •  半阙折子戏
    2020-12-06 12:26

    You can color a cell or a entire column or entire row.

    The below code will help you out.

    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

    The below code is the way i defined the excel worksheet.

    Excel.Workbook xlWorkBook;
    Excel.Worksheet xlWorkSheet;
    Excel.Range xlwidthadjust; //used this to adjust width of columns
    object misValue = System.Reflection.Missing.Value;
    
    xlWorkBook = xlApp.Workbooks.Add(misValue);
    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    

    with this code i am sure that you wont get this exception Exception from HRESULT: 0x800A03EC

提交回复
热议问题