如何在C#for Excel中使用函数替换为函数公式

如何在C#for Excel中使用函数替换为函数公式,c#,excel,excel-formula,C#,Excel,Excel Formula,我可以找到解决这个问题的办法 我有一个C#中的项目,可以更改excel文件中的一些单元格 因此,我尝试这样做: range = sheet.UsedRange; // leer las celdas int rows = range.Rows.Count; int cols = range.Columns.Count; Excel.Range startCell = sheet.Cells[1, 1]; Excel.Range endCell = sheet.Cells[rows, cols]

我可以找到解决这个问题的办法

我有一个C#中的项目,可以更改excel文件中的一些单元格

因此,我尝试这样做:

range = sheet.UsedRange;
// leer las celdas
int rows = range.Rows.Count;
int cols = range.Columns.Count;

Excel.Range startCell = sheet.Cells[1, 1];
Excel.Range endCell = sheet.Cells[rows, cols];
sheet.Range[startCell, endCell].Formula.Replace(@"C:\", @"C:\Gestion\");
但是,我不能使用最后一行中的函数公式

此行错误:
sheet.Range[startCell,endCell].Formula.Replace(@“C:\”,@“C:\Gestion\”)

右边是
sheet.Range[startCell,endCell].Replace(@“C:\”,@“C:\gesting\”)


但是现在,我有另一个问题。程序不会更改所有单元格。当函数找到此字符串@“C:\”时,仅更改一个单元格。

我认为您必须将公式重新分配到以下范围:

sheet.Range[startCell, endCell].Formula = sheet.Range[startCell, endCell].Formula.Replace(@"C:\", @"C:\Gestion\");