【c#搬砖记】用Docx导出word格式的docx文件

2018-05-11 14:31:25 浏览数 (1)

DocX开源网址:http://docx.codeplex.com/

1、引入DocX.dll 调用ReplaceText()方法替换模板中的字符。只支持docx格式的word文档

代码语言:javascript复制
using (DocX docx = DocX.Load(fileDemo))
            {
                docx.ReplaceText("@某某某", tester.name);
                docx.ReplaceText("@110101198101010001", tester.IdCode);

                docx.SaveAs(fileSave);

            }

2、插入图片形状。

引入 Aspose.Words 组件

代码语言:javascript复制
            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);

0 人点赞