Excel 范围内的 NumberFormat 无法使用 c#
Excel NumberFormat for on range not working using c#
我正在尝试使用此格式格式化货币列范围
NumberFormat = "$ #,##0.00";
每次我得到这个结果
£ 1,016.89
这是设置列的完整函数
public void SetColumns(List<ReportColumn> columns)
{
try
{
int row = 9;
int index = 10;
int count = Report.Data.Count - 1;
for (int i = 0; i < columns.Count; i++)
{
switch (columns[i].Type.ToUpperInvariant())
{
case ColumnSettingType.Number:
{
oRange = (Excel.Range)oSheet.Range[oSheet.Cells[index, i + 1], oSheet.Cells[index + count, i + 1]];
oRange.NumberFormat = "#,###,###0";
oRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
break;
}
case ColumnSettingType.Currency:
{
oRange = (Excel.Range)oSheet.Range[oSheet.Cells[index, i + 1], oSheet.Cells[index + count, i + 1]];
var format = "$ #,##0.00";
oRange.NumberFormat = format;
oRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
break;
}
case ColumnSettingType.Percentage:
{
oRange = (Excel.Range)oSheet.Range[oSheet.Cells[index, i + 1], oSheet.Cells[index + count, i + 1]];
oRange.NumberFormat = "0.00%";
oRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
break;
}
case ColumnSettingType.Date:
{
oRange = (Excel.Range)oSheet.Range[oSheet.Cells[index, i + 1], oSheet.Cells[index + count, i + 1]];
oRange.NumberFormat = "MM/dd/yyyy";
oRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
break;
}
}
oSheet.Cells[row, i + 1] = columns[i].ColumnName;
}
}
catch (Exception ex)
{
throw ex;
}
}
我做错了什么需要帮助,也需要用它来建立一些文化。
NumberFormat 属性 似乎使用 $ 符号来指示单元格采用货币格式 - 据我所知,它使用当前区域设置的货币符号。
用于 cell/range 的实际货币符号可以在 NumberFormatLocal 属性 中设置。
同时设置两者应将值显示为“$ 1,016.89”
var format = "$ #,##0.00";
oRange.NumberFormat = format;
oRange.NumberFormatLocal = format;
我正在尝试使用此格式格式化货币列范围
NumberFormat = "$ #,##0.00";
每次我得到这个结果
£ 1,016.89
这是设置列的完整函数
public void SetColumns(List<ReportColumn> columns)
{
try
{
int row = 9;
int index = 10;
int count = Report.Data.Count - 1;
for (int i = 0; i < columns.Count; i++)
{
switch (columns[i].Type.ToUpperInvariant())
{
case ColumnSettingType.Number:
{
oRange = (Excel.Range)oSheet.Range[oSheet.Cells[index, i + 1], oSheet.Cells[index + count, i + 1]];
oRange.NumberFormat = "#,###,###0";
oRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
break;
}
case ColumnSettingType.Currency:
{
oRange = (Excel.Range)oSheet.Range[oSheet.Cells[index, i + 1], oSheet.Cells[index + count, i + 1]];
var format = "$ #,##0.00";
oRange.NumberFormat = format;
oRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
break;
}
case ColumnSettingType.Percentage:
{
oRange = (Excel.Range)oSheet.Range[oSheet.Cells[index, i + 1], oSheet.Cells[index + count, i + 1]];
oRange.NumberFormat = "0.00%";
oRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
break;
}
case ColumnSettingType.Date:
{
oRange = (Excel.Range)oSheet.Range[oSheet.Cells[index, i + 1], oSheet.Cells[index + count, i + 1]];
oRange.NumberFormat = "MM/dd/yyyy";
oRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
break;
}
}
oSheet.Cells[row, i + 1] = columns[i].ColumnName;
}
}
catch (Exception ex)
{
throw ex;
}
}
我做错了什么需要帮助,也需要用它来建立一些文化。
NumberFormat 属性 似乎使用 $ 符号来指示单元格采用货币格式 - 据我所知,它使用当前区域设置的货币符号。
用于 cell/range 的实际货币符号可以在 NumberFormatLocal 属性 中设置。
同时设置两者应将值显示为“$ 1,016.89”
var format = "$ #,##0.00";
oRange.NumberFormat = format;
oRange.NumberFormatLocal = format;