我有一个 C# 程序,它获取旧报告的文本文件并映射到 Excel 工作表。
但对于交易单元格,它输出为“数字存储为文本”,这不允许
任何格式。我们想要显示 1,000.00,但它仅显示为 1000。
有什么办法可以得到这种格式吗?这些列是余额和金额。
这里定义为:
ndcRange = currentSheet.Cells[transactionRowNo + pageOffset, 13];
ndcRange.NumberFormat = "@";
ndcRange.Value2 = tr.Amount;
ndcRange = currentSheet.Cells[transactionRowNo + pageOffset, 16];
ndcRange.NumberFormat = "@";
ndcRange.Value2 = tr.Balance;
这是一些完成一些格式化的代码:
string balance = transaction;
if (lastSpaceIndex != -1)
{
balance = transaction.Substring(lastSpaceIndex, (transaction.Length)lastSpaceIndex);
}
transac.Balance = balance;
transaction = transaction.Replace(balance, "");
transaction = transaction.Trim();
if (!string.IsNullOrWhiteSpace(transaction))
{
transac.Description = transaction;
}
patient.TransactionsList.Add(transac);
使用 Microsoft Excel 对象库中的 Range.NumberFormat。检查这个link http://support.microsoft.com/kb/302084.
示例代码:
oRng = oSheet.get_Range("C2", "C1000"); //change this range to suit your scenario
oRng.Formula = "<any formula you may be using already or skip it>";
oRng.NumberFormat = "#,##0.00";
参考:
如何从 Microsoft Visual C#.NET 自动化 Microsoft Excel http://support.microsoft.com/kb/302084
StackOverflow 示例 https://stackoverflow.com/questions/3828600/convert-excel-cell-value-from-text-to-number-using-c-sharp
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)