C#生成Word文档

  rel="File-List" href="file:///C:%5CDOCUME%7E1%5CLancer%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">

I.      环境:

VS2005 SP1office 2003

II. 设置:

首先在工程中添加ReferenceMicrosoft Word 11.0 Object Library。同时在cs文件开头using word

III.          代码:

A.    初始:

1.       设定文档名称

Object FileName = 示例;

2.       创建两个object

Object Nothing = System.Reflection.Missing.Value;

Object EndOfDoc = "//endofdoc";

3.       创建word文档

Word.Application WordApp = new Word.ApplicationClass();

              Word.Paragraph ParaHeader;

Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

B.    写入内容:

1.       添加一段文字:

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

              ParaHeader.Range.Text = "一段文字";

              ParaHeader.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;

              ParaHeader.Range.Font.Bold = 3;

              ParaHeader.Range.Font.Size = 26;

              ParaHeader.Range.Font.Name = "宋体";

ParaHeader.Range.InsertParagraphAfter();

 

各句含义可从其属性名称中得知。

2.       换行:

object count = 35;

              object WdLine = Word.WdUnits.wdLine;

              WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);

              WordApp.Selection.TypeParagraph();

WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;

 

各句含义可从其属性名称中得知。

3.       添加图片:

String PicPathWeek = System.Windows.Forms.Application.StartupPath + "//Week.gif";

WordApp.Selection.InlineShapes.AddPicture(PicPathWeek, ref Nothing, ref Nothing, ref Nothing);

 

各句含义可从其属性名称中得知

C.    文件保存:

WordDoc.SaveAs(ref FileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);

         WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);

          WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值