How can I format a formulaic result in C# Excel Interop?

让人想犯罪 __ 提交于 2019-12-12 13:26:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!