问题
I have two scenario, I'm developing with two pc, one with Visual Studio 2010, Office 2010, PIA 2010 and another PC with Visual Studio 2010, Office 2007, PIA 2007. The same application have different behaviour, the first one works fine, the second one doesn't work.
I need to set a conditional formatting in a spreadsheet but with office 2007 it doesn't work because the formula in Excel is copy as string and not as formula. My code is the following for both office version:
Excel.Range cellToSet = worksheet.get_Range(startCell, endCell);
Excel.FormatConditions fcs = cellToSet.FormatConditions;
Excel.FormatCondition fc = (Excel.FormatCondition)fcs.Add(Excel.XlFormatConditionType.xlExpression, Type.Missing, "H4>=M4");
string str = fc.Formula1;
Excel.Interior interior = fc.Interior;
interior.Color = ColorTranslator.ToOle(backgroundColor);
The variable str is used to check the formula content after the Add calling, str returns the following: "=\"H4>=M4\"" and the worksheet doesn't work properly. Opening the excel file, under the menu Conditional Formatting / Manager Rules / Show Formatting rules I see: Formula: ="H4>=M4" instead of Formula: =H4>=M4 (without the characters ")
In the PC with office 2010 the str variable returns "=H4>=M4" (without the characters ") and the generated file works fine.
Why in office 2007 this code doesn't work?
回答1:
Try to used absolute Cell references as described in this article. So instead of:
"H4>=M4" try "$H$4>=$M$4$".
来源:https://stackoverflow.com/questions/22083487/excel-interop-conditional-formatting-with-formula