C#으로 Excel 워크 시트에서 텍스트를 찾고 제거하십시오.

StackOverflow https://stackoverflow.com/questions/523690

  •  22-08-2019
  •  | 
  •  

문제

"Joe"와 같은 텍스트를 찾고 C#이있는 Excel 워크 시트의 위치에서 제거하고 싶습니까?

도움이 되었습니까?

해결책

Range.Replace 메소드를 사용하고 빈 문자열로 간단히 교체해야합니다.

static void ReplaceTextInExcelFile(string filename, string replace, string replacement)
{
    object m = Type.Missing;

    // open excel.
    Application app = new ApplicationClass();

    // open the workbook. 
    Workbook wb = app.Workbooks.Open(
        filename,
        m, false, m, m, m, m, m, m, m, m, m, m, m, m);

    // get the active worksheet. (Replace this if you need to.) 
    Worksheet ws = (Worksheet)wb.ActiveSheet;

    // get the used range. 
    Range r = (Range)ws.UsedRange;

    // call the replace method to replace instances. 
    bool success = (bool)r.Replace(
        replace,
        replacement,
        XlLookAt.xlWhole,
        XlSearchOrder.xlByRows,
        true, m, m, m);

    // save and close. 
    wb.Save();
    app.Quit();
    app = null;
}

다른 팁

이것은 효과가 있습니다 Excel 2016:

  • 검색 Microsoft.Office.Interop.Excel C : C : C# 프로젝트에 복사하여 참조로 포함하십시오.
  • 어셈블리 버전이 있어야합니다 15.0.0.0.

암호:

Excel.DisplayAlerts = false; // Prevent "Nothing found" dialogs.

var Excel = (Microsoft.Office.Interop.Excel.Application)ExcelDnaUtil.Application;

foreach(Worksheet sheet in Excel.Sheets)
{
    bool success = sheet.Rows.Replace(What: "ABC",Replacement: "DEF");
}

Excel.DisplayAlerts = true;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top