c# - .Net 中的 Excel 样式条件数字格式

标签 c# .net excel number-formatting

我需要使用 Excel 条件格式字符串来格式化 .Net 应用程序中的数字。对于那些不熟悉它们的人,Excel 格式字符串如下所示:

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

...应该解释为“对大于 2 的数字使用第一种格式,对小于 -2 的数字使用第二种格式,对其他所有内容使用第三种格式”。

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

最佳答案

您必须自己实现格式化,但您可以使用接口(interface) IFormatProvider 连接到 .NET 现有的格式化框架。和 ICustomFormatter .

这是一个关于如何做到这一点的例子。创建了一个 ConditionalFormatter 类,它由多个 ConditionalFormat 对象组成。 ConditionalFormat 类有一个 Predicate 和一个 FormatConditionalFormatter 将按顺序搜索所有 ConditionalFormat 对象,找到第一个 Predicate 为 true 的对象,并使用关联的 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);

关于c# - .Net 中的 Excel 样式条件数字格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5620746/

相关文章:

excel - 如何将列从 MS Excel 添加到 MS Project - VBA

c# - .ToString() 和 + ""有什么区别

c# - 将并行 for 循环作为低优先级 CPU 操作执行的最简单方法是什么?

.net - NHibernate调用PostgresSQL的SELECT DISTINCT ON()

c# - 从 ConditionalWeakTable<T> 获取事件项列表

c# - Excel 数据读取器问题、列名称和工作表选择

c# - 元素 'entityFramework' 具有无效的子元素 Entity Framework

c# - 发布网站获取空文件夹

c# - 如何绑定(bind)到 Silverlight 中的页面属性?

vba - 如何使 For-Each 循环向后运行