excel cell coloring
Asked Answered
P

7

11

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

Application excel = new Application();
Workbook wb = excel.Workbooks.Open(destPath);
 Worksheet ws = wb.Worksheets[1];
 ws.get_Range(ws.Cells[row, clmn]).Cells.Interior.Color = 36;

...to color cells, but this is not working. Can anyone help me out?

Promoter answered 5/5, 2011 at 11:40 Comment(1)
It is giving exception "Exception from HRESULT: 0x800A03EC"Promoter
P
15

Try something like that

ws.Cells[row, clmn].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
Pancake answered 5/5, 2011 at 11:43 Comment(3)
It is giving exception "Exception from HRESULT: 0x800A03EC"Promoter
How can I iterate through a column and check every row?Lineman
One thing that can cause the dreaded "Exception from HRESULT: 0x800A03EC" is if the sheet is protected. Check nWS.ProtectContents before you try to change anything on the sheet.Townsman
W
9

Cells[row, clmn] is a range so you don't need to call get_Range() and there is a enum that you can use for colors.

ws.Cells[row, clmn].Interior.Color = XlRgbColor.rgbBlack;
Westleigh answered 5/5, 2011 at 12:11 Comment(1)
.Interior isn't found`Lineman
B
7

If you want to set color by color index, you need to use this method:

    Cells[row, col].Interior.ColorIndex = 36;
Braunite answered 28/8, 2012 at 9:9 Comment(0)
S
3

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

Sorus answered 20/3, 2017 at 11:3 Comment(0)
D
1

Make sure you are using:

using Excel = Microsoft.Office.Interop.Excel;

If you have a variable for the range you want to change, then use:

chartRange = xlWorkSheet.get_Range("a5", "a8");    
chartRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);

If you want to just change the color of a specific cell, then use:

xlWorkSheet.Cells[row, col].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);

...where 'row' is the row number, and 'col' is the column number assigned to the given lettered columns (starting at 1).

Dig answered 19/9, 2017 at 14:37 Comment(0)
S
0
using System.Drawing; 
using Microsoft.Office.Interop.Excel;

and then follow with

ws.Cells[row, clmn].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)

Don't forget to add System.Drawing.dll assembly to your reference.

Swor answered 25/7 at 15:53 Comment(0)
T
-1

Exception from HRESULT: 0x800A03EC

Solution: Change the misValue to sheet1, sheet2 or sheet3.

xlWorkBook = xlApp.Workbooks.Add("sheet1"); 

This works for me. system.reflaction.missing.value what was that, it is not related to Excel.workbooks.add came from a Excel file default value. When you create a Excel file, the default worksheets are sheet1, sheet2 and sheet3.

Tamarra answered 2/7, 2017 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.