I used this code to populate a cell in a spreadsheet using EPPlus:
using (var footerMonth1Cell = prodUsageWorksheet.Cells[columnFooterRow, MONTH1_COL]) { footerMonth1Cell.Value = monthsTruncatedYears[0]; footerMonth1Cell.Style.Font.Size = DATA_FONT_SIZE; footerMonth1Cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right; }
It worked fine, I thought, but the user complained that it produces “green triangles” in the cells. "monthsTruncatedYears" is a general list of strings.
So, I added this helper function:
public static void SetValueIntOrStr(this ExcelRangeBase range, object value) { string strVal = value.ToString(); if (!String.IsNullOrEmpty(strVal)) { double dVal; int iVal; if (double.TryParse(strVal, out dVal)) range.Value = dVal; else if (Int32.TryParse(strVal, out iVal)) range.Value = iVal; else range.Value = strVal; } else range.Value = null; }
... which I got from here and then tried to reorganize the source code to call such a helper function:
using (var footerMonth1Cell = prodUsageWorksheet.Cells[columnFooterRow, MONTH1_COL]) { footerMonth1Cell.Value = SetValueIntOrStr(footerMonth1Cell, monthsTruncatedYears[0]); footerMonth1Cell.Style.Font.Size = DATA_FONT_SIZE; footerMonth1Cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right; }
However, it will not compile, telling me: "It is not possible to implicitly convert the type" void "to" object ""
Since the second arg is passed to SetValueIntOrStr (), namely "value", has an object of type, I assume this is a problem. So why does the compiler seem to consider monthsTruncatedYears [0] to be invalid? In legacy code, I assigned it as the value for the cell, and at that time it was not empty, so ...?!?
I tried making the second arg argument like this:
footerMonth1Cell.Value = ReportRunnerConstsAndUtils.SetValueIntOrStr(footerMonth1Cell, object(monthsTruncatedYears[0]));
... but it will also not be calculated using the "Invalid term expression" object "
... and this similar attempt:
footerMonth1Cell.Value = ReportRunnerConstsAndUtils.SetValueDoubleOrIntOrStr(footerMonth1Cell, monthsTruncatedYears[0] as object);
... elicits, "It is not possible to implicitly convert the type 'void' to 'object'"
Note that the helper method is actually incorrectly specified in the borrowed code; instead of "SetValueIntOrStr" it should be "SetValueDoubleOrIntOrStr"