使用C#在Excel中查找和replace文本

我想用C#在Excel中查找和replace一组文本,而且我希望这个replace只发生在第一行的文本中。

我曾经使用过Google,发现了Aspose API,Spire.Xls等一些付费资源,但是我正在寻找一种开源资源或者其他有效的方法来实现这一点。 请build议。

尝试这个:

Public 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; }