我想在每个工作表的excel文件中找到特定的单词,并试图用新单词替换它。当我尝试运行下面的程序时,它给了我错误。搜索并替换Excel文档中的文本不起作用?
预期类,委托,枚举,接口或结构类型或 命名空间名称“DocumentFormat”无法找到(使用指令或程序集引用是否缺少 ?)错误1个标识符 预期
using System.IO;
using System.Text.RegularExpressions;
using DocumentFormat.OpenXml.Packaging;
using (WordprocessingDocument wordDoc =
WordprocessingDocument.Open(document, true))
{
using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
SearchAndReplace(@"C:\Users\Public\Documents\MyPkg8.docx");
public static void SearchAndReplace(string document)
{
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
{
string docText = null;
using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
Regex regexText = new Regex("Hello world!");
docText = regexText.Replace(docText, "Hi Everyone!");
using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(docText);
}
}
}
}
取出声明“使用DocumentFormat.OpenXml.Packaging;” – user469104
我刚刚注意到你的代码与word文档一起工作,你问的是excel。你在寻找这个词文档代码的优秀版本吗? – robaudas