开发者

excel cell coloring

I am using c# to开发者_JAVA技巧 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..


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
}


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);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜