我想使用C#在Excel中查找并替换一组文本,而且我希望此替换只发生在第一行的文本中。
我使用Google并找到了一些付费资源,如Aspose API,Spire.Xls等,但我正在寻找开源资源或任何其他有效的方法来实现这一目标。请建议。
答案 0 :(得分:1)
试试这个:
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;
}