. net中Excel样式的条件数字格式

本文关键字:条件 数字 格式 样式 Excel net | 更新日期: 2023-09-27 17:49:45

我需要在。net应用程序中使用Excel条件格式字符串来格式化数字。对于那些不熟悉它们的人来说,Excel格式字符串看起来像这样:

[>=2]#,##0.0;[<=-2]-#,##0.0;#,##0.00

…这应该被解释为"对大于2的数字使用第一种格式,对小于-2的数字使用第二种格式,对其他所有的数字使用第三种格式"。

在我继续构建自己的自定义格式字符串解析器之前,有没有人知道在。net中是否有类似的东西我可以使用?我知道格式字符串有;分隔符,但据我所知,它似乎不能考虑"是负/正/零"以外的条件。

. net中Excel样式的条件数字格式

你必须自己实现格式化,但是你可以通过使用接口IFormatProviderICustomFormatter来连接到。net现有的格式化框架。

下面是如何做到这一点的一个例子。创建一个由几个ConditionalFormat对象组成的ConditionalFormatter类。ConditionalFormat类有PredicateFormatConditionalFormatter将按顺序搜索所有ConditionalFormat对象,找到Predicate为真的第一个对象,并使用相关的Format。格式化程序使用字母'Z'作为格式字符串。

class ConditionalFormat<T> where T : IFormattable {
  public Func<T, Boolean> Predicate { get; set; }
  public String Format { get; set; }
  public static readonly Func<T, Boolean> Tautology = _ => true;
}
class ConditionalFormatter<T> : Collection<ConditionalFormat<T>>, IFormatProvider, ICustomFormatter
  where T : IFormattable {
  public const String FormatString = "Z";
  readonly CultureInfo cultureInfo;
  public ConditionalFormatter(IEnumerable<ConditionalFormat<T>> conditionalFormats)
    : this(conditionalFormats, null) { }
  public ConditionalFormatter(IEnumerable<ConditionalFormat<T>> conditionalFormats, CultureInfo cultureInfo)
    : base(conditionalFormats.ToList()) {
    this.cultureInfo = cultureInfo;
  }
  public Object GetFormat(Type formatType) {
    return formatType == typeof(ICustomFormatter) ? this : null;
  }
  public String Format(String format, Object arg, IFormatProvider formatProvider) {
    if (arg.GetType() != typeof(T))
      return HandleOtherFormats(format, arg);
    var formatUpperCase = format.ToUpperInvariant();
    if (formatUpperCase != FormatString)
      return HandleOtherFormats(format, arg);
    var value = (T) arg;
    foreach (var conditionalFormat in this)
      if (conditionalFormat.Predicate(value))
        return ((IFormattable) value).ToString(conditionalFormat.Format, cultureInfo);
    throw new InvalidOperationException(String.Format("No format matching value {0}.", value));
  }
  String HandleOtherFormats(String format, Object arg) {
    var formattable = arg as IFormattable;
    if (formattable != null)
      return formattable.ToString(format, this.cultureInfo);
    else if (arg != null)
      return arg.ToString();
    else
      return String.Empty;
  }
}

类是泛型的,您必须创建一个与您想要格式化的类型匹配的实例。下面是一个使用Double的例子:

var conditionalFormatter = new ConditionalFormatter<Double>(
  new[] {
    new ConditionalFormat<Double> {
      Predicate = d => -2 < d && d < 2,
      Format = "#,##0.00"
    },
    new ConditionalFormat<Double> {
      Predicate = ConditionalFormat<Double>.Tautology,
      Format = "#,##0.0"
    },
  }
);
var value = 1234.5678;
var formattedValue = String.Format(conditionalFormatter, "Value is {0:Z}", value);

另一个有趣的解决方案:有一个名为SmartFormat的库,它包含一个条件格式化器。

示例的语法为:

var output = Smart.Format("{0:>=2?{0:#,##0.0}|<=-2?{0:-#,##0.0}|{0:#,##0.00}}", value);

语法解释请参见ConditionalFormatter。

然而,如果你想要完全支持上面提到的Excel语法,你可以下载SmartFormat源代码并修改ConditionalFormatter以支持你的语法。它使用Regex来解析每个(condition),因此很容易修改。

你需要写if/else if语句。没有直接的方法

string numberToFormat = //state your number here
string FormattedString = null;
if(numberToFormat >2)
{
    //Format 1
    //e.g. FormattedString = String.Format("{0:0.00}", numberToFormat);
}
else if(numberToFormat < -2)
{
     //Format 2
}
else 
{
     // Format Default     
}

这是最短的路。当然你也可以得到你自己的条件格式化器