替换 Word 文档中的字符串 - 适用于 MainDocumentPart,不适用于 HeaderPart
我需要对Word文档中的字符串进行简单的搜索和替换。我认为这会很容易,但它不是(至少对我来说)
查看此代码(它需要一个流,打开文档的不同部分,搜索字符串,然后替换它)。
问题是仅保存 MainDocumentPart 和 FooterPart 内的内容。不保存 HeaderPart。奇怪...
public static void ProcessDocument(Dictionary<string, string> properties, Stream fs)
{
using (WordprocessingDocument doc = WordprocessingDocument.Open(fs, true))
{
string docText = null;
using (StreamReader sr = new StreamReader(doc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
docText = DoTheReplace(properties, docText);
using (StreamWriter sw = new StreamWriter(doc.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(docText);
}
foreach (FooterPart footer in doc.MainDocumentPart.FooterParts)
{
string footerText = null;
using (StreamReader sr = new StreamReader(footer.GetStream()))
{
footerText = sr.ReadToEnd();
}
footerText = DoTheReplace(properties, footerText);
using (StreamWriter sw = new StreamWriter(footer.GetStream(FileMode.Create)))
{
sw.Write(footerText);
}
}
foreach (HeaderPart header in doc.MainDocumentPart.HeaderParts)
{
string headerText = null;
using (StreamReader sr = new StreamReader(header.GetStream()))
{
headerText = sr.ReadToEnd();
}
headerText = DoTheReplace(properties, headerText);
using (StreamWriter sw = new StreamWriter(header.GetStream(FileMode.Create)))
{
sw.Write(headerText);
}
}
}
}
是的,如果有更简单的方法来替换 Word 文档中的字符串,请告诉我。
感
谢拉尔西的帮助
I need to do a simple search and replace of a string in a word document. I thought it would be pretty easy, but it's not (at least for me)
Check out this code (It takes a stream, opens the different part of the doc, searches for the string, and then it replaces it).
Problem is that only whats inside the MainDocumentPart and the FooterPart is saved. The HeaderPart is not saved. Strange...
public static void ProcessDocument(Dictionary<string, string> properties, Stream fs)
{
using (WordprocessingDocument doc = WordprocessingDocument.Open(fs, true))
{
string docText = null;
using (StreamReader sr = new StreamReader(doc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
docText = DoTheReplace(properties, docText);
using (StreamWriter sw = new StreamWriter(doc.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(docText);
}
foreach (FooterPart footer in doc.MainDocumentPart.FooterParts)
{
string footerText = null;
using (StreamReader sr = new StreamReader(footer.GetStream()))
{
footerText = sr.ReadToEnd();
}
footerText = DoTheReplace(properties, footerText);
using (StreamWriter sw = new StreamWriter(footer.GetStream(FileMode.Create)))
{
sw.Write(footerText);
}
}
foreach (HeaderPart header in doc.MainDocumentPart.HeaderParts)
{
string headerText = null;
using (StreamReader sr = new StreamReader(header.GetStream()))
{
headerText = sr.ReadToEnd();
}
headerText = DoTheReplace(properties, headerText);
using (StreamWriter sw = new StreamWriter(header.GetStream(FileMode.Create)))
{
sw.Write(headerText);
}
}
}
}
And yes if there are simpler ways of replacing a string in a word doc, please let me know.
Thanks for any help
Larsi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最终使用了 DocX。这是一个很棒的库,并且有一个简单的替换功能。
http://docx.codeplex.com/
I ended up using DocX. It's a great lib, and has a simple Replace function.
http://docx.codeplex.com/
当您在部件上调用 GetStream() 时,我相信它会返回部件的整个 XML 结构,而不仅仅是文本区域。 Microsoft Word 有时会在奇怪的地方分割单词,因此类似的字符串
可能看起来像
So 如果您尝试替换“Hello”,它可能无法使用简单的搜索和替换找到它。也许您的标题部分中的文本以这样奇怪的方式被分割。
When you call GetStream() on the part, I believe it returns the entire XML structure of the part, not just the text area. And Microsoft Word sometimes splits words in strange places, so a string like
might look like
So if you're trying to replace "Hello", it might not find it using a simple search and replace. Maybe the text in your header part is split up in a strange way like that.
就像“MainDocumentPart”有一个保存方法:
MainDocumentPart.Document.Save();
您还应该调用 Header 的 Save 方法:
header.Header.Save();
Like the "MainDocumentPart" has a method to save:
MainDocumentPart.Document.Save ();
You should also call the Save method for Header:
header.Header.Save ();