DocX开源网址:http://docx.codeplex.com/
1、引入DocX.dll 调用ReplaceText()方法替换模板中的字符。只支持docx格式的word文档
using (DocX docx = DocX.Load(fileDemo)) { docx.ReplaceText("@某某某", tester.name); docx.ReplaceText("@110101198101010001", tester.IdCode); docx.SaveAs(fileSave); }
2、插入图片形状。
Document doc = new Document(fileSave); DocumentBuilder builder = new DocumentBuilder(doc); Shape shape = new Shape(doc, ShapeType.Rectangle); //shape.ImageData.SetImage(fileImage); shape.Width = width; shape.Height = height; shape.FillColor = Color.Red; //shape.HorizontalAlignment = HorizontalAlignment.Left; //靠右对齐 builder.MoveToBookmark(bookmark); builder.InsertNode(shape); //Bookmark myname = doc.Range.Bookmarks["myname"]; //myname.Text = ""; //doc.Range.Bookmarks["myname"].Remove(); doc.Save(fileSave, SaveFormat.Docx);
时间: 2024-11-08 22:28:21