- 9
- 0
- 约1.04万字
- 约 9页
- 2016-09-21 发布于重庆
- 举报
C编程实现动态生成word文档
如何用C#编程实现动态生成Word文档并填充数据的效果呢?要使用C#操作word,首先要添加引用:
??? 1、添加引用-COM-Microsoft Word 11.0 Object Library
??? 2、在.cs文件中添加
??? using? Word;
??? 下面的例子中包括C#对Word文档的创建、插入表格、设置样式等操作:
??? (例子中代码有些涉及数据信息部分被省略,重要是介绍一些C#操作word文档的方法)
??? public?? string? CreateWordFile( string? CheckedInfo)
??? ... {
??? string? message? =?? ;
??? try
??? ... {
??? Object Nothing? =? System.Reflection.Missing.Value;
??? Directory.CreateDirectory( C:/CNSI );?? // 创建文件所在目录
??? string? name? =?? CNSI_ ?? +? DateTime.Now.ToShortString() + .doc ;
??? object? filename? =?? C://CNSI// ?? +? name;?? // 文件保存路径
??? // 创建Word文档
??? Word.Application WordApp? =?? new? Word.ApplicationClass();
??? Word.Document WordDoc? =? WordApp.Documents.Add( ref? Nothing,? ref? Nothing,? ref? Nothing,? ref? Nothing);
??? // 添加页眉
??? WordApp.ActiveWindow.View.Type? =? WdViewType.wdOutlineView;
??? WordApp.ActiveWindow.View.SeekView? =? WdSeekView.wdSeekPrimaryHeader;
??? WordApp.ActiveWindow.ActivePane.Selection.InsertAfter( [页眉内容] );
??? WordApp.Selection.ParagraphFormat.Alignment? =? Word.WdParagraphAlignment.wdAlignParagraphRight; // 设置右对齐
??? WordApp.ActiveWindow.View.SeekView? =? WdSeekView.wdSeekMainDocument; // 跳出页眉设置
??? WordApp.Selection.ParagraphFormat.LineSpacing? =? 15f; // 设置文档的行间距
??? // 移动焦点并换行
??? object? count? =?? 14 ;
??? object? WdLine? =? Word.WdUnits.wdLine; // 换一行;
??? WordApp.Selection.MoveDown( ref? WdLine,? ref? count,? ref? Nothing); // 移动焦点
??? WordApp.Selection.TypeParagraph(); // 插入段落
??? // 文档中创建表格
??? Word.Table newTable? =? WordDoc.Tables.Add(WordApp.Selection.Range,? 12 ,? 3 ,? ref? Nothing,? ref? Nothing);
??? // 设置表格样式
??? newTable.Borders.OutsideLineStyle? =? Word.WdLineStyle.wdLineStyleThickThinLargeGap;
??? newTable.Borders.InsideLineStyle? =? Word.WdLineStyle.wdLineStyleSingle;
??? newTable.Columns[ 1 ].Width? =? 100f;
??? newTable.Columns[ 2 ].Width? =? 220f;
??? newTable.Columns[ 3 ].Width? =? 105f;
??? // 填充表格内容
??? newTable.Cell( 1 ,? 1 ).Range.Text? =?? 产品详细信息表 ;
??? new
您可能关注的文档
- 高中生物必修1复习要点.doc
- 高中生物必修二第1章第2节课时活页训练.doc
- 高中级专业技术人员职称评审赋分标准.doc
- 高中英语作文写作模式经典句子总结.doc
- 高中英语作文提分攻略英语作文逻辑连贯的必杀技过渡词.doc
- 高中英语作文的万能开头和关联词.doc
- 高中英语定语从句及名词性从句混合练习题.doc
- 高中英语必修5(北师大版)Unit13People知识点总结.docx
- 高中英语必考固定用法50则.doc
- 高中英语构词法精讲.doc
- 克林贝格锥齿轮技术BC12 B27 CS200 说明书用户手册.pdf
- Fengfei广告系统说明书用户手册.pdf
- Megawin兆威8051-Based MCU品类信息型号信息用户手册.pdf
- Microchip Technology Company串行四I O(SQI)闪存SST26VF016 SST26VF032数据手册.pdf
- 英克司兰中文名称品类信息型号信息说明书用户手册.pdf
- Easecurities易盛证券金融 证券5.3用户手册.pdf
- SLAMTEC用户手册S1用户手册.pdf
- Wanda Information Co., Ltd. 用户手册 说明书 三次修订稿 说明书用户手册.pdf
- Wanda Info. 说明书四次修订稿用户手册.pdf
- BOSCH CAN FD协议模块 CAN FD 说明书用户手册.pdf
最近下载
- 乐购Hymall连锁超市-收银员排班工具使用作业规范.pdf VIP
- 黑龙江省教育学会示范性高中专业委员会2025届高三下学期第一次模拟考试 语文 含答案.docx VIP
- ISPE第五部分试车与确认课件.pptx VIP
- 【2026年春新教材】部编版小学二年级下册道德与法治全册教案(教学设计).docx
- 数学北师大版八年级下册多边形的内角和教学设计.docx VIP
- 守正创新担使命,凝心聚力启新程——校长在2026年春季学期全体教师开学大会上的讲话.docx
- 《体育课程与教学论》全套PPT课件.pptx VIP
- 第一章税收初阶资料.ppt VIP
- 数据接口安全管理制度.docx VIP
- 道路车辆 电气及电子设备的环境条件和试验 第3部分:机械负荷.docx VIP
原创力文档

文档评论(0)