Gowtham Ramamoorthy
Gowtham Ramamoorthy

Reputation: 896

Isuse with Changing cell color in excel based on cell value

I tried changing color of a particular cell value based on its content and I have the below code where it seems to be fine without throwing any error.

Problem is when i rul this code the debugger stops at xlWorkBook.Close(true, misValue, misValue); and I'm not sure why the code stops here. Let me know if someone can help.

      foreach (DataTable table in ds.Tables)
        {

           for (int j = 1; j < table.Rows.Count; j++)
            {
                var cellvalue = (string)(excelWorkSheet.Cells[j, 5] as Excel.Range).Value;
                if (cellvalue == "Red")
                {
                    excelWorkSheet.Cells[5, j].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                }

                else if (cellvalue == "Blue")
                {
                    excelWorkSheet.Cells[5, j].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Blue);

                }

                else if (cellvalue == "Yellow")
                {
                    excelWorkSheet.Cells[5, j].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);

                }

                else if (cellvalue == "Orange")
                {
                    excelWorkSheet.Cells[5, j].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange);

                }

                else
                {
                    excelWorkSheet.Cells[5, j].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);
                }

            }


        }

        xlWorkBook.Sheets["Sheet1"].Delete();
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

Upvotes: 1

Views: 494

Answers (1)

user4039065
user4039065

Reputation:

The Workbook.Close has three parameters; all optional. If you do not specify the parameter of the option they are simply taken in order.

If you do not need to specify a parameter other than the default then ignore it.

'expression .Close(SaveChanges, Filename, RouteWorkbook)
'in your case
xlWorkBook.Close(true);

That should be enough to save the changes to your your workbook and close it under the same name.

Upvotes: 1

Related Questions