I need 13 cells containing three-character months followed by a space and then a two-character year, such as:
Apr 14
May 14
...
Jan 15
...
Apr 15
Getting the beginning (and ending) month and the ending year from the user, I wrote this function to store all the vals in a generic list:
private List<string> GetMonthAndTruncatedYears(string monthBeginAndEnd, string endYear)
{
const int MONTHS_TO_ADD = 13;
String endYear2Digit = (Convert.ToInt32(endYear)).ToString();
endYear2Digit = endYear2Digit.Substring(2);
String beginYear2Digit = (Convert.ToInt32(endYear) - 1).ToString();
beginYear2Digit = beginYear2Digit.Substring(2);
String yearToUse = beginYear2Digit;
List<String> listMonthsYears = new List<string>();
listMonthsYears.Add(String.Format("{0} {1}", monthBeginAndEnd, yearToUse));
int valsAdded = 1;
String curMonth = monthBeginAndEnd;
while (valsAdded < MONTHS_TO_ADD)
{
curMonth = GetMonthStr(curMonth, 1);
if (curMonth.Equals("Jan"))
{
yearToUse = endYear2Digit;
}
listMonthsYears.Add(String.Format("{0} {1}", curMonth, yearToUse));
valsAdded++;
}
return listMonthsYears;
}
private string GetMonthStr(string beginMonth, int offset)
{
int curIndex = UsageRptConstsAndUtils.months.IndexOf(beginMonth);
if (curIndex == 11)
{
curIndex = -1;
}
return UsageRptConstsAndUtils.months[curIndex + offset];
}
public static List<String> months = new List<String>
{
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
};
The values are stored as desired in the generic list.
I am adding the values to the cells as a string:
_xlSheet.Cells["7", "C"] = monthsTruncatedYears[0];
However, although I expect the items to display raw/verbatim, instead they display in the Excel cells as "14-Apr" etc. Why? Who told them to change format?
NOte: I am using Microsoft.Office.Interop.Excel, v2.0.50727
So how can I tell Excel to leave the formatting be?