C#中对word文档操作,替换指定标签文字,插入指定标签的图片。
初始化word文档,获取操作对象。
添加引用
using OpWord = Microsoft.Office.Interop.Word;
public static Tuple<OpWord.Application, OpWord.Document> GetDocument(string file)
{
var app = new OpWord.Application();
app.Visible = false;
var document = app.Documents.Open(
file, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
var result = new Tuple<OpWord.Application, OpWord.Document>(app, document);
return result;
}
将文档中确定的标签字符串strTag,替换成新的文字strNew
public static void ReplaceText(Tuple<OpWord.Application, OpWord.Document> doc, string strTag, string strNew)
{
if (string.IsNullOrEmpty(strNew))
return;
object nullobj = Type.Missing, missing = Type.Missing;
object objReplace = OpWord.WdReplace.wdReplaceAll;
doc.Item1.Selection.Find.ClearFormatting();
doc.Item1.Selection.Find.Replacement.ClearFormatting();
foreach (Microsoft.Office.Interop.Word.Section section in doc.Item2.Sections)
{
Microsoft.Office.Interop.Word.Range fphFooterRange = section.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;
fphFooterRange.Find.Text = strTag;
fphFooterRange.Find.Replacement.Text = strNew;
fphFooterRange.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref objReplace, ref missing, ref missing, ref missing, ref missing);
Microsoft.Office.Interop.Word.Range fpHeaderRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;
fpHeaderRange.Find.Text = strTag;
fpHeaderRange.Find.Replacement.Text = strNew;
fpHeaderRange.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref objReplace, ref missing, ref missing, ref missing, ref missing);
//Get the header range and add the header details.
Microsoft.Office.Interop.Word.Range headerRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
Microsoft.Office.Interop.Word.Range footerRange = section.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerRange.Find.Text = strTag;
footerRange.Find.Replacement.Text = strNew;
footerRange.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref objReplace, ref missing, ref missing, ref missing, ref missing);
headerRange.Find.Text = strTag;
headerRange.Find.Replacement.Text = strNew;
headerRange.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref objReplace, ref missing, ref missing, ref missing, ref missing);
}
if (strNew.Length <= 255)
{
doc.Item1.Selection.Find.Text = strTag;
doc.Item1.Selection.Find.Replacement.Text = strNew;
doc.Item1.Selection.Find.Execute(ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref objReplace, ref nullobj,
ref nullobj, ref nullobj, ref nullobj);
}
else
{
int hasSubLength = 0;
int itemLength = 255 - strTag.Length;
while (true)
{
string itemStr = "";
int subLength = 0;
if (strNew.Length - hasSubLength <= 255) // 剩余的内容不超过255,直接替换
{
doc.Item1.Selection.Find.ClearFormatting();
doc.Item1.Selection.Find.Replacement.ClearFormatting();
doc.Item1.Selection.Find.Text = strTag;
doc.Item1.Selection.Find.Replacement.Text = strNew.Substring(hasSubLength, strNew.Length - hasSubLength);
doc.Item1.Selection.Find.Execute(ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref objReplace, ref nullobj,
ref nullobj, ref nullobj, ref nullobj);
break; // 结束循环
}
// 由于Word中换行为“^p”两个字符不能分割
// 如果分割位置将换行符分开了,则本次少替换一个字符
if (strNew.Substring(hasSubLength, itemLength).EndsWith("^") &&
strNew.Substring(hasSubLength, itemLength + 1).EndsWith("p"))
{
subLength = itemLength - 1;
}
else
{
subLength = itemLength;
}
itemStr = strNew.Substring(hasSubLength, subLength) + strTag;
doc.Item1.Selection.Find.ClearFormatting();
doc.Item1.Selection.Find.Replacement.ClearFormatting();
doc.Item1.Selection.Find.Text = strTag;
doc.Item1.Selection.Find.Replacement.Text = itemStr;
doc.Item1.Selection.Find.Execute(ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref objReplace, ref nullobj,
ref nullobj, ref nullobj, ref nullobj);
hasSubLength += subLength;
}
}
// doc.Item2.Save();
// ReleaseAll(doc);
}
将word文档中图片标签strTag,替换成图片 image,可以设置图片的宽和高
注意使用了,剪切板方法进行copy操作
public static void ReplaceImage(Tuple<OpWord.Application, OpWord.Document> doc, string strTag, Image image, int width, int height)
{
object nullobj = Type.Missing;
object objReplace = OpWord.WdReplace.wdReplaceAll;
OpWord.Selection selection = doc.Item1.Selection;
object unite = OpWord.WdUnits.wdStory;
selection.Find.Text = strTag;
var isSelect= selection.Find.Execute();
if (!isSelect)
{
return;
}
var range = selection.Range;
System.Windows.Forms.Clipboard.SetDataObject(image);
range.Paste();
var inlineShape = range.InlineShapes[1];
//设置图片大小
inlineShape.Width = width;
inlineShape.Height = height;
selection.Find.Text = strTag;
selection.Find.Replacement.Text = "";
selection.Find.Execute(ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref objReplace, ref nullobj,
ref nullobj, ref nullobj, ref nullobj);
// doc.Item2.Save();
System.Windows.Forms.Clipboard.Clear();
}
操作完成调用对象保存和释放方法
//保存方法直接调用第一个方法生成的对象Item2的系统方法SaveAs2
word.Item2.SaveAs2(fn);
public static void ReleaseAll(Tuple<OpWord.Application, OpWord.Document> ad)
{
ad.Item2.Close(false, Missing.Value, Missing.Value);
NAR(ad.Item2);
ad.Item1.Quit();
NAR(ad.Item1);
System.GC.Collect();
}
完整的应用如下
var word = WordUtil.GetDocument(templatePath);
WordUtil.ReplaceText(word, tag, newstr);
WordUtil.ReplaceImage(word, tag, image, 600, 300);
word.Item2.SaveAs2(fn);
WordUtil.ReleaseAll(word);