这几天正在写一个小程序,但用到生成word表格和Excel表格。到网上查了一些资料,发现如果生成表格模板相对比较固定即可把其制作成模板,需要设置什么格式可以直接在模板中设置,而程序仅需替换相应的内容即可。
word中书签的使用也很方便,在模板中可以直接定位到书签上。如插入图片时即可利用书签进行插入。关键代码如下:
object bookmarkName = "zdMap"; wdDoc.Bookmarks.get_Item(ref bookmarkName).Select(); object linkToFile = false; object saveWithDocument = true; object Nothing = System.Reflection.Missing.Value; // wdApp.Selection.InlineShapes.AddPicture("c:\1.jpg", ref Nothing, ref Nothing, ref Nothing); if (StaticDJMessage.JpgNamePath != null && File.Exists(StaticDJMessage.DictoryDJ+"\"+StaticDJMessage.JpgName)) { Word.InlineShape shape = wdApp.Selection.InlineShapes.AddPicture(StaticDJMessage.JpgNamePath, ref linkToFile, ref saveWithDocument, ref Nothing); //shape.Width = 100f;//图片宽度 //shape.Height = 120f;//图片高度 // shape.ConvertToShape().WrapFormat.Type = Word.WdWrapType.wdWrapSquare;//四周环绕的方式 }
特别注意替换表格整个文档时,以第一行的文本格式为准。