0

目前,我正在使用 C# 和 vsto 开发一个 excel 插件。功能之一是将所有选定单元格的值四舍五入到最接近的美元。以下是我尝试实现的代码,它似乎可以完美地选择几个单元格。

However, when the number of cells being selected is large (eg. the entire col), it takes a huge of time to do the job. 想问问有没有什么办法可以提高代码的效率。

        Excel.Application ExApp = Globals.Helper.Application as Excel.Application;


        Excel.Worksheet ExWorksheet = ExApp.ActiveSheet as Excel.Worksheet;

        Excel.Range rng = (Excel.Range)ExApp.ActiveCell;

        Microsoft.Office.Interop.Excel.Range selectedRange =
            ExApp.ActiveWindow.RangeSelection;

        foreach (Excel.Range r in selectedRange.Cells)
        {
            if(r.Formula!=null)
            {
            string v = r.Formula.Replace("=", "");
            r.Formula = "=Round(" + v + ",0)";}
        }
4

0 回答 0