问题
I've got this code that sums the values in the given range (Column O Row 10 to Column O the last signficant row):
Note: That's the letter "O" not the number "Zero".
var totalTotalPackageCountCell =
(Range)_xlSheetDelPerf.Cells[curDelPerfRow + 2, TOTAL_PACKAGE_COUNT_COLUMN];
totalTotalPackageCountCell.Formula = string.Format("=SUM(O10:O{0})", curDelPerfRow);
It works pretty dandy except for one thing: the value displays without commas, such as "20192" (I want it to be "20,192").
I tried this:
totalTotalPackageCountCell.Formula =
string.Format("=SUM(O10:O{0})", curDelPerfRow)
.ToString("N0", CultureInfo.CurrentCulture);
...as .ToString("N0", CultureInfo.CurrentCulture)
is how I successfully add commas to other values.
In this (formula value) case, though, it doesn't even compile - I get:
No overload for method 'ToString' takes 2 arguments
How can I have my formula and format it, too?
回答1:
You could try this one. Try to specify the NumberFormat of the cell, before assigning a value.
totalTotalPackageCountCell.NumberFormat = "#,##0";
totalTotalPackageCountCell.Formula = string.Format("=SUM(O10:O{0})", curDelPerfRow);
来源:https://stackoverflow.com/questions/34032569/how-can-i-format-a-formulaic-result-in-c-sharp-excel-interop