Users are uploading Excel files to my site. They are dollar amounts, and I'd like to store them as decimals. Further, I'd like users to be able to use at least the Currency, Accounting, or Number formats in Excel (or anything else that C# can convert to a decimal).
What I've tried:
var table = (from a in excelFile.WorksheetRangeNoHeader("B2", "B32", "Upload Template") select a[0]).ToList();
BenefitsUploadViewModel model = new BenefitsUploadViewModel() { ErrorList = new List<string>() };
Benefits b = new Benefits();
b.ResponseId = ResponseId;
bool success = Decimal.TryParse(table[0], out decimal MedicalTotal);
if (success)
{
b.MedicalTotal = MedicalTotal;
}
else
{
model.ErrorList.Add("Medical total cell should be formatted as Currency, Accounting, or Number.");
}
bool success1 = Decimal.TryParse(table[1].Value.ToString(), out decimal StdSicknessAccident);
if (success1)
{
b.StdSicknessAccident = StdSicknessAccident;
}
else
{
model.ErrorList.Add("STD, Sickness, and Accident Insurance cell should be formatted as Currency, Accounting, or Number.");
}
bool success2 = Decimal.TryParse(table[2].Value, out decimal LtdWage);
if (success2)
{
b.LtdWage = LtdWage;
}
else
{
model.ErrorList.Add("LTD & Wage Insurance cell should be formatted as Currency, Accounting, or Number.");
}
All 3 of these are being added to the error list despite being formatted in Excel as number, currency, or accounting.
Here are some useful watches:
What other method can I use to convert them to decimals suitable for storing in my database?
Thanks!