bill
bill

Reputation: 123

excel cell coloring

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.Cells[row, clmn].Interior.Color = XlRgbColor.rgbBlack;

to color cells..But this is not working.. It is giving an exception on the last line where i am coloring the cells

"Exception from HRESULT: 0x800A03EC"

I am unable to fix the exception Can anyone help me out..

Upvotes: 1

Views: 1382

Answers (3)

Michael
Michael

Reputation: 659

It might not work because the worksheet is protected.

You can check it the following way:

Application excel = new Application();
Workbook wb = excel.Workbooks.Open(destPath);
Worksheet ws = wb.Worksheets[1];

bool wasProtected = ws.ProtectContents;
if (wasProtected == true)
{
    // unprotect the worksheet
}
else
{
    ws.Cells[row, clmn].Interior.Color = XlRgbColor.rgbBlack;
}

...

if (wasProtected == true)
{
    // protect back the worksheet
}

Upvotes: 1

Tom Brothers
Tom Brothers

Reputation: 6007

I tested the code that you have on a new Excel file and it works fine. However, I was able to reproduce the error when testing with a row value of 0. So maybe the problem is with the row and clmn values. Otherwise it must be something specific to your Excel file... maybe a protected cell or something along those lines.

Upvotes: 0

Syjin
Syjin

Reputation: 2810

In my previous project I used the following snippet to Color Cells in Excel:

ws.Cells[row, clmn].Interior.Color = System.Drawing.ColorTranslator.ToWin32(Color.Black);

Upvotes: 0

Related Questions