sou*_*gic 18 c# format excel currency
我开发了一个Excel加载项,因此您可以将MySQL数据库中的一些数字插入到特定单元格中.现在我尝试将这些单元格格式化为货币,我有两个问题.1.在格式化单元格上使用公式时,总和例如显示为:"353,2574€".如何以适当的方式显示它?有些单元格是空的,但也必须用货币格式化.当使用我用于总和公式的相同格式并输入内容时,只显示数字.没有"€",没有.那是什么?我指定了一个Excel.Range并使用它来格式化范围
sum.NumberFormat = "#.## €";
Run Code Online (Sandbox Code Playgroud)
但我也试过了
sum.NumberFormat = "0,00 €";
sum.NumberFormat = "#.##0,00 €";
Run Code Online (Sandbox Code Playgroud)
有人在想吗?
Job*_*mno 16
这个对我有用.我有excel测试应用程序,将货币格式化为2位小数,逗号为千位分隔符.下面是在Excel文件上写入数据的控制台应用程序.
确保您引用了Microsoft.Office.Interop.Excel dll
using System.Collections.Generic;
using Excel = Microsoft.Office.Interop.Excel;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var bankAccounts = new List<Account> {
new Account { ID = 345678, Balance = 541.27},
new Account {ID = 1230221,Balance = -1237.44},
new Account {ID = 346777,Balance = 3532574},
new Account {ID = 235788,Balance = 1500.033333}
};
DisplayInExcel(bankAccounts);
}
static void DisplayInExcel(IEnumerable<Account> accounts)
{
var excelApp = new Excel.Application { Visible = true };
excelApp.Workbooks.Add();
Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
workSheet.Cells[1, "A"] = "ID Number";
workSheet.Cells[1, "B"] = "Current Balance";
var row = 1;
foreach (var acct in accounts)
{
row++;
workSheet.Cells[row, "A"] = acct.ID;
workSheet.Cells[row, "B"] = acct.Balance;
}
workSheet.Range["B2", "B" + row].NumberFormat = "#,###.00 €";
workSheet.Columns[1].AutoFit();
workSheet.Columns[2].AutoFit();
}
}
public class Account
{
public int ID { get; set; }
public double Balance { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
输出
