Gembox 查找和更新单元格

Gembox find and update cell

我正在尝试使用 gembox 更新 excel 文件。我希望 gembox 在 excel 文件中搜索某个字符串,并更新所有匹配项。但是方法FindText()只在returns第一次出现。

我尝试了以下方法,但它会导致无限循环,由于某种原因行和列不会更新到下一个搜索结果。

  private void RenameSubCategory(ExcelWorksheet sheet, string subCategoryOld, string subCategoryNew)
  {
     int row, column;
     while (sheet.Cells.FindText(subCategoryOld, false, false, out row, out column)) {
        sheet.Cells[row, column].Value = subCategoryNew;
     }
  }

您的 subCategoryNew 文本是否可能包含 subCategoryOld 文本?
换句话说,您是否可能有以下情况:

RenameSubCategory(sheet, "Sample", "Sample New");

这会导致无限循环,因为您总是会在设置为 "Sample New" 文本的单元格中找到 "Sample"
而是尝试使用 ReplaceText 方法之一,如下所示:

sheet.Cells.ReplaceText("Sample", "Sample New");

或尝试使用以下方法:

private static void RenameSubCategory(ExcelWorksheet sheet, string subCategoryOld, string subCategoryNew)
{
    foreach (ExcelRow row in sheet.Rows)
        foreach (ExcelCell cell in row.AllocatedCells)
            if (cell.ValueType == CellValueType.String &&
                cell.StringValue.Contains(subCategoryOld))
                cell.Value = subCategoryNew;
}

希望对您有所帮助。