【问题标题】:is there other faster way of doing this还有其他更快的方法吗
【发布时间】:2010-11-03 18:05:08
【问题描述】:

下面的代码执行以下操作:它需要一个范围,然后在一个范围内找到不同的值, 并将它们存储在 d_distinct 数组中,也为每个不同的值创建不同的颜色,然后使用 Excel.FormatCondition 为范围着色...(我当前的范围是 A1:HM232)

for (int t = 0; t < d_distinct.Length; t++ )
{                        
    Excel.FormatCondition cond =
        (Excel.FormatCondition)range.FormatConditions.Add(
        Excel.XlFormatConditionType.xlCellValue,
        Excel.XlFormatConditionOperator.xlEqual, 
        "="+d_distinct[t],
        mis, mis, mis, mis, mis);
    cond.Interior.PatternColorIndex = 
        Excel.Constants.xlAutomatic;
    cond.Interior.TintAndShade = 0;
    cond.Interior.Color = ColorTranslator.ToWin32(c[t]);
    cond.StopIfTrue = false;                        
}

但这工作太慢了...用户将不得不坐下来等待大约一分钟...我是这样做的,否则如果我用一行代码简单地这样做(颜色惊人快)

range.FormatConditions.AddColorScale(3);

我将无法请求单元格的颜色...(我可以在一个范围内有十多个不同的值)

你能帮我让我的第一种方法更快地工作吗?提前致谢!

【问题讨论】:

    标签: c# excel excel-2007 vba


    【解决方案1】:

    尝试在代码运行时关闭屏幕更新,然后再将其重新打开。在 VBA 中,这将是:

    Application.ScreenUpdating = False
    // do stuff
    Application.ScreenUpdating = True
    

    由于您没有使用 VBA,请尝试http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._application.screenupdating(office.11).aspx 以获得指导

    【讨论】:

      【解决方案2】:

      您是否尝试过使用 OpenXML SDK 来处理 Office 2007?我已经将它用于单词,发现它比使用 vba 或 COM 快得多。试试version 2.01.0。我认为 2.0 仍然是 CTP。不过,在我的书中比 vba 更直观。否则,屏幕更新技巧可能是最简单的方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-22
        • 1970-01-01
        相关资源
        最近更新 更多