c# word操作

1.在文章开头添加图片:

object missing = System.Reflection.Missing.Value;

            object unit;

            unit = Word.WdUnits.wdStory;

            WordApp.Selection.HomeKey(ref unit, ref missing); //定位到文章开头

            string FileName = "D:/mark.jpg"; // 图片所在路径

            object LinkToFile = false;

            object SaveWithDocument = true;

            object Anchor = WordDoc.Application.Selection.Range;

            object left = 0, top = 0, width = 100, height = 100;

            object Anchor2 = WordApp.Selection.Range;

            WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref  LinkToFile, ref  SaveWithDocument, ref  Anchor2);

            WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 90f; // 图片宽度

            WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 90f; // 图片高度

            WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;

            //将图片设置为四周环绕型

            Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();

            s.WrapFormat.Type = Word.WdWrapType.wdWrapTopBottom;

注意这段代码最好写在最后,因为写在开始的话在之后再添加内容很可能就会把图片给覆盖掉。

 

2.添加段落并设置文字属性:

Word.Paragraph para;

            para = WordDoc.Content.Paragraphs.Add(ref Nothing);

            para.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;

            para.Range.Font.Name = "楷体_GB2312";

            para.Range.Text = "这是生成的word文档";

            para.Range.Font.Bold = 1;

            para.Range.Font.Size = 23;

            para.Range.InsertParagraphAfter();

 

3.在此段落后面添加分页符:

//插入分页符

object pBreak = (int)WdBreakType.wdSectionBreakNextPage;

para.Range.InsertBreak(ref pBreak);

 

还有几篇很好的讲生成word的牛人的随笔:

c# 生成 word_大杂烩

http://hi.baidu.com/metaza/blog/item/b277ccc4183b73ab8226ac8b.html

.netword的操作

http://www.cnblogs.com/niuyy/archive/2008/06/25/1229755.html

[分享]一段导出到word模版的代码

http://www.cnblogs.com/goody9807/archive/2005/08/25/222526.html

再谈word2003编程

http://www.cnblogs.com/Andmm/archive/2008/06/18/1224422.html

最近一直在做C#操作office方面的工作!总结一下!Word()

http://www.cnblogs.com/wngwz/archive/2004/08/19/34678.html

C#也能动态生成Word文档并填充数据

http://www.cnblogs.com/qyfan82/archive/2007/09/14/893293.html

 

posted @ 2008-09-03 18:02  达达7  阅读(4364)  评论(1编辑  收藏  举报