Generate a flat list of all excel cell formulas

前端 未结 5 1100
离开以前
离开以前 2021-01-06 14:10

I have a massive program written with VBA and cell formulas. I am tasked to reverse engineer it into C# winforms. I figured for a start, I need to see all the cell formulas

5条回答
  •  情书的邮戳
    2021-01-06 14:48

    Here is some code that I used to get a list of cells on a worksheet with formulas in them. It seems pretty fast.

    try
    {
        Excel.Worksheet excelWorksheet = workbook.ActiveSheet as Excel.Worksheet;
        Excel.Range formulaCell = excelWorksheet.Cells.SpecialCells(
            Excel.XlCellType.xlCellTypeFormulas, Type.Missing);
    
        Excel.Range cell;
        foreach (var fc in formulaCell)
        {
            cell = fc as Excel.Range;
            string s1 = cell.Formula as string;
            int c = cell.Column;
            int r = cell.Row;
    
            // Gives formula text and location of formula.
        }
    }
    catch (Exception)
    {
        ; // Throws an exception if there are no results.
          // Probably should ignore that exception only
    }
    

提交回复
热议问题