C#生成Word文档
来源:互联网 发布:mac 预览 目录 编辑:程序博客网 时间:2024/04/27 23:44
I. 环境:
VS2005 SP1,office 2003
II. 设置:
首先在工程中添加Reference:Microsoft 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.ApplicationWordApp = new Word.ApplicationClass();
Word.Paragraph ParaHeader;
Word.DocumentWordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, refNothing, 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, refcount, 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, refNothing, ref Nothing);
各句含义可从其属性名称中得知
C. 文件保存:
WordDoc.SaveAs(refFileName, ref Nothing, refNothing, ref Nothing, refNothing, ref Nothing, refNothing, ref Nothing, refNothing, ref Nothing, refNothing, ref Nothing, refNothing, ref Nothing, refNothing, ref Nothing);
WordDoc.Close(ref Nothing, refNothing, ref Nothing);
WordApp.Quit(ref Nothing, refNothing, ref Nothing);
- C#生成Word文档
- C#生成Word文档
- C#生成Word文档
- c# 生成word文档
- C# 动态生成Word文档
- c#生成的word文档
- c#生成的word文档
- c#生成的word文档
- c# 生成word文档例子
- C#根据WORD模板生成WORD文档 C#网页打印
- C#用模版生成word文档
- 用C#生成word文档的方法
- C#编程实现动态生成Word文档
- C#编程实现动态生成Word文档
- C# 在Word文档中生成条形码
- C# 在Word文档中生成条形码
- C#编程实现动态生成word文档
- C#也能动态生成Word文档并填充数据
- 考过了网工,证件不知怎么发下了
- 【转帖】爆笑一则!
- word中多级项目符号的使用
- 必须掌握的八个【cmd 命令行】
- linux2.4 启动代码head.S分析
- C#生成Word文档
- phpclass实现动态属性
- 窗口内置的滚动条如何绘制的更好?
- 过去的,就过去了
- Tomcat连接池配置
- 你竭尽全力了吗?
- 豁出去了
- 售前工程师、售前经理必备知识和技巧
- VB鼠标绘图基础资料总结(转载)--------注: 可惜作者未写完