用于使用c#的on范围的Excel NumberFormat

时间:2018-03-26 14:33:22

标签: c# excel interop excel-interop

我正在尝试使用此格式

格式化货币列范围
  

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;
    }
}

需要帮助我做错了我需要用它设置一些文化。

1 个答案:

答案 0 :(得分:1)

看起来NumberFormat属性使用$符号来表示单元格是货币格式 - 就我所见,它使用当前的区域设置货币符号。

可以在NumberFormatLocal属性中设置用于单元格/范围的实际货币符号。

设置两者都应将值显示为“$ 1,016.89”

 var format = "$ #,##0.00";
 oRange.NumberFormat = format;
 oRange.NumberFormatLocal = format;