我想使用 C# 在 Excel 中查找和替换一组文本,而且我希望此替换仅发生在第一行中的文本。
我使用 Google 并找到了一些付费资源,如 Aspose API、Spire.Xls 等,但我正在寻找开源资源或任何其他有效的方法来实现这一点。请提出建议。
最佳答案
试试这个:
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;
}
关于c# - 使用 C# 在 Excel 中查找和替换文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40484841/