版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、C#生成word文档之绝对最全最近两天研究了一下如何使用VS2008(C#语言)输出Word文档。以下是几点总结:1、非常简单。2、开发及运行环境要求。操作系统为:WindowsXP(安装.netframework2.0)/Vista/Win7;在操作系统必须安装Word2003完全安装版。这里必须要强调是Word2003完全安装版,因为软件开发及运行都需要一个com组件:Microsoftword11.0ObjectLibrary。如果不是Word2003完全安装版,可以下载这个com组件,并手动的安装这个com组件。下载地址为:3、C#工程设置。这里的工程设置,就是添加com组件。步骤为:
2、在工程资源管理器中"添加引用"->"com"选项卡->在下拉列表中选Microsoftword11.0ObjectLibrary。ok了,看上去,跟添加一般的dll一样,但实际上vs2008在这个过程中完成一系列复杂的关于.net调用com组件的操作,不过,幸好我们不用管这个。4、接下就是写代码了。在这里,使用Word的com对像,跟使用一般的非com对像一样,非常流畅,好像根本就不管它什么com不com的。为了使代码比较简洁,可以在源代码文件顶添加这样的一条语句:usingWord=Microsoft.Office.Interop.Word
3、;5、最好是对word对像模型有一定的了解,这样在写代码的时候就不会那么“迷茫”了。wore对像模型中有几个比较重要的对像,它们是Application、Document、Selection、RangeBookmark,以及其它的一些对像,如:Paragraph、Section、Table等级。刚开始学的时候,感觉Selection、Range、Bookmark这几个对像有点迷惑人,Selection可能好理解,就是表示当前的选择区域,如果没有选择就表示光标所在位置。Range和Bookmark,其实在很多地方很像,不过也有一些区别,在这里就不多说了,google一下"word.Ra
4、nge"就行了。6、在写代码的过程中,经常会想要实现的一些操作,但是由于对word对像不熟悉而不知怎么用代码实现。比如设置页眉、添加页码什么的,如果在Word程序里手动的操作当然很简单,但是要用代码来实现,对初学者来说就可能不那么容易了。遇到这种情况,一般有两种方法可以选择:一种是"百度/google法",别一种,也是我所推荐的一种就是,利用Word的“录制宏”功能把想要实现的操作录成宏之后,再看宏里的代码,宏里的代码其实几乎就是你想要的代码了(只不过语法有一点不一样而已)。7、以下给出一个示例,这个示例里面包括了一些常用的图、文、表、公式的编辑与排版以及页面设置
5、、页眉、页码的操作,里面都有注释,写得很清楚。usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingMicrosoft.Office.Interop;usingWord=Microsoft.Office.Interop.Word;usingSystem.Runtime.InteropServices;namespaceWindowsFormsApplication1publicpartial
6、classForm1:System.Windows.Forms.FormDllImport("shell32.dll")publicstaticexternintShellExecute(IntPtrhwnd,StringlpszOp,StringlpszFile,StringlpszParams,StringlpszDir,intFsShowCmd);publicForm1()InitializeComponent();privatevoidbutton1_Click(objectsender,EventArgse)/新建文档/Word.Applicationnewapp
7、=newWord.Application();/用这句也能初始化Word.Applicationnewapp=newWord.ApplicationClass();Word.Documentnewdoc;objectnothing=System.Reflection.Missing.Value;/用于作为函数的默认参数newdoc=newapp.Documents.Add(refnothing,refnothing,refnothing,refnothing);/生成一个word文档newapp.Visible=true;/是否显示word程序界面/页面设置/newdoc.PageSetup.
8、Orientation=Word.WdOrientation.wdOrientLandscape;/newdoc.PageSetup.PageWidth=newapp.CentimetersToPoints(21.0f);/newdoc.PageSetup.PageHeight=newapp.CentimetersToPoints(29.7f);newdoc.PageSetup.PaperSize=Word.WdPaperSize.wdPaperA4;newdoc.PageSetup.Orientation=Word.WdOrientation.wdOrientPortrait;newdoc.
9、PageSetup.TopMargin=57.0f;newdoc.PageSetup.BottomMargin=57.0f;newdoc.PageSetup.LeftMargin=57.0f;newdoc.PageSetup.RightMargin=57.0f;newdoc.PageSetup.HeaderDistance=30.0f;/页眉位置/设置页眉newapp.ActiveWindow.View.Type=Word.WdViewType.wdOutlineView;/视图样式。newapp.ActiveWindow.View.SeekView=Word.WdSeekView.wdSee
10、kPrimaryHeader;/进入页眉设置,其中页眉边距在页面设置中已完成newapp.Selection.ParagraphFormat.AlignmentWord.WdParagraphAlignment.wdAlignParagraphRight;/插入页眉图片stringheaderfile="d:header.jpg"this.outpicture(headerfile,Properties.Resources.header);Word.InlineShapeshape1=newapp.ActiveWindow.ActivePane.Selection.Inli
11、neShapes.AddPicture(headerfile,refnothing,refnothing,refnothing);shape1.Height=30;shape1.Width=80;newapp.ActiveWindow.ActivePane.Selection.InsertAfter("中建东北院");/去掉页眉的那条横线newapp.ActiveWindow.ActivePane.Selection.ParagraphFormat.BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderBotto
12、m.LineStyle=Word.WdLineStyle.wdLineStyleNone;newapp.ActiveWindow.ActivePane.Selection.BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderBottom.Visible=false;newapp.ActiveWindow.ActivePane.View.SeekView=Word.WdSeekView.wdSeekMainDocument;/退出页眉设置/添加页码Word.PageNumberspns=newapp.Selection.Sectio
13、ns1.HeadersMicrosoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages.PageNumbers;pns.NumberStyle=Word.WdPageNumberStyle.wdPageNumberStyleNumberInDash;pns.HeadingLevelForChapter=0;pns.IncludeChapterNumber=false;pns.ChapterPageSeparator=Word.WdSeparatorType.wdSeparatorHyphen;pns.Restar
14、tNumberingAtSection=false;pns.StartingNumber=0;objectpagenmbetal=Word.WdPageNumberAlignment.wdAlignPageNumberCenter;objectfirst=true;newapp.Selection.Sections1.FootersMicrosoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages.PageNumbers.Add(refpagenmbetal,reffirst);/文字设置(Selection表示当
15、前选择集,如果当前没有选择对像,则指对光标所在处进行设置)newapp.Selection.Font.Size=14;newapp.Selection.Font.Bold=0;newapp.Selection.Font.Color=Word.WdColor.wdColorBlack;newapp.Selection.Font.Name="宋体"/段落设置newapp.Selection.ParagraphFormat.LineSpacingRuleWord.WdLineSpacing.wdLineSpaceExactly;newapp.Selection.Paragraph
16、Format.LineSpacing=20;newapp.Selection.ParagraphFormat.AlignmentWord.WdParagraphAlignment.wdAlignParagraphLeft;newapp.Selection.ParagraphFormat.FirstLineIndent=30;newdoc.Content.InsertAfter(WindowsFormsApplication1.Properties.Resources.PreViewWords);/插入公式objectoEndOfDoc="endofdoc"Word.Rang
17、erang1=newdoc.Bookmarks.get_Item(refoEndOfDoc).Range;objectfieldType=Word.WdFieldType.wdFieldEmpty;i(a,b,Exdx)"objectpresrveFormatting=false;rang1.Text=formula.ToString();rang1.Font.Size=14;rang1.Font.Bold=0;rang1.Font.Subscript=0;rang1.Font.Color=Word.WdColor.wdColorBlue;rang1.Font.Name="
18、宋体"rang1.ParagraphFormat.LineSpacingRuleWord.WdLineSpacing.wdLineSpaceSingle;rang1.ParagraphFormat.AlignmentWord.WdParagraphAlignment.wdAlignParagraphRight;newdoc.Fields.Add(rang1,reffieldType,refformula,refpresrveFormatting);将文档的前三个字替换成"asdfasdf",并将其颜色设为蓝色objectstart=0;objectend=3;Wo
19、rd.Rangerang2=newdoc.Range(refstart,refend);rang2.Font.Color=Word.WdColor.wdColorBlue;rang2.Text="as签"将文档开头的"as"替换成"袁波”rang1.Start=0;rang1.End=2;rang1.Text="这是一个"rang1.InsertAfter("书");/rang1.Select();objectcodirection=Word.WdCollapseDirection.wdCollapseS
20、tart;rang1.Collapse(refcodirection);/将rang1的起点和终点都定于起点或终点/对前三个字符进行加粗newdoc.Range(refstart,refend).Bold=1;objectrang=rang2;newdoc.Bookmarks.Add("yb",refrang);objectunite=Word.WdUnits.wdStory;newapp.Selection.EndKey(refunite,refnothing);/将光标移至文末newapp.Selection.Font.Size=10;newapp.Selection.
21、TypeText("(式1)n");/插入图片newapp.Selection.EndKey(refunite,refnothing);/将光标移至文末/newapp.Selection.HomeKey(refunite,refnothing);/将光标移至文开头newapp.Selection.ParagraphFormat.LineSpacingRuleWord.WdLineSpacing.wdLineSpaceSingle;newapp.Selection.ParagraphFormat.AlignmentWord.WdParagraphAlignment.wdAli
22、gnParagraphCenter;objectLinkToFile=false;objectSaveWithDocument=true;objectAnchor=newapp.Selection.Range;stringpicname="d:kk.jpg"this.outpicture(picname,Properties.Resources.IMG_2169);newdoc.InlineShapes.AddPicture(picname,refLinkToFile,refSaveWithDocument,refAnchor);newdoc.InlineShapes1.H
23、eight=200;newdoc.InlineShapes1.Width=200;newdoc.Content.InsertAfter("n");newapp.Selection.EndKey(refunite,refnothing);/将光标移至文末newapp.Selection.ParagraphFormat.AlignmentWord.WdParagraphAlignment.wdAlignParagraphCenter;newapp.Selection.Font.Size=10;newapp.Selection.TypeText("图1袁冶n"
24、);newapp.Selection.ParagraphFormat.AlignmentWord.WdParagraphAlignment.wdAlignParagraphRight;newdoc.Content.InsertAfter("n");newdoc.Content.InsertAfter("n");/用这种方式也可以插入公式,并且这种方法更简单newapp.Selection.Font.Size=14;newapp.Selection.InsertFormula(refformula,refnothing);newapp.Selection.
25、Font.Size=10;newapp.Selection.TypeText("(式2)n");newapp.Selection.ParagraphFormat.AlignmentWord.WdParagraphAlignment.wdAlignParagraphCenter;newapp.Selection.TypeText("表1电子产品n");/插入表格Word.Tabletable1=newdoc.Tables.Add(newapp.Selection.Range,4,3,refnothing,refnothing);newdoc.Tables1
26、.Cell(1,1).Range.Text="产品n项目"newdoc.Tables1.Cell(1,2).Range.Text="电脑"newdoc.Tables1.Cell(1,3).Range.Text="手机"newdoc.Tables1.Cell(2,1).Range.Text="重量(kg)"newdoc.Tables1.Cell(3,1).Range.Text="价格(元)"newdoc.Tables1.Cell(4,1).Range.Text="共同信息"ne
27、wdoc.Tables1.Cell(4,2).Range.Text="信息A"newdoc.Tables1.Cell(4,3).Range.Text="信息B"table1.Select();table1.Rows.Alignment=Word.WdRowAlignment.wdAlignRowCenter;/整个表格居中newapp.Selection.Cells.VerticalAlignmentWord.WdCellVerticalAlignment.wdCellAlignVerticalCenter;newapp.Selection.Paragr
28、aphFormat.AlignmentWord.WdParagraphAlignment.wdAlignParagraphCenter;newapp.Selection.Cells.HeightRuleWord.WdRowHeightRule.wdRowHeightExactly;newapp.Selection.Cells.Height=40;table1.Rows2.Height=20;table1.Rows3.Height=20;table1.Rows4.Height=20;table1.Range.ParagraphFormat.AlignmentWord.WdParagraphAli
29、gnment.wdAlignParagraphCenter;newapp.Selection.Cells.Width=150;table1.Columns1.Width=75;table1.Cell(1,1).Range.ParagraphFormat.AlignmentWord.WdParagraphAlignment.wdAlignParagraphRight;table1.Cell(1,1).Range.Paragraphs2.Format.AlignmentWord.WdParagraphAlignment.wdAlignParagraphLeft;/表头斜线table1.Cell(1
30、,1).BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown.Visible=true;table1.Cell(1,1).BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown.Color=Word.WdColor.wdColorGreen;table1.Cell(1,1).BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown.Line
31、Width=Word.WdLineWidth.wdLineWidth050pt;/表格边框table1.BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal.Visible=true;table1.BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal.Color=Word.WdColor.wdColorGreen;table1.BordersMicrosoft.Office.Interop.Word.WdBorderType.w
32、dBorderHorizontal.LineWidth=Word.WdLineWidth.wdLineWidth050pt;ertical.Visible=true;ertical.Colorertical.LineWidthtable1.BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderVtable1.BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderVWord.WdColor.wdColorGreen;table1.BordersMicrosoft.Offic
33、e.Interop.Word.WdBorderType.wdBorderVWord.WdLineWidth.wdLineWidth050pt;table1.BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderLeft.Visible=true;table1.BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderLeft.ColorWord.WdColor.wdColorGreen;table1.BordersMicrosoft.Office.Interop.Word.W
34、dBorderType.wdBorderLeft.LineWidthWord.WdLineWidth.wdLineWidth050pt;table1.BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderLeft.LineStyleWord.WdLineStyle.wdLineStyleDoubleWavy;table1.BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderRight.Visible=true;table1.BordersMicrosoft.Office
35、.Interop.Word.WdBorderType.wdBorderRight.ColorWord.WdColor.wdColorGreen;table1.BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderRight.LineWidthWord.WdLineWidth.wdLineWidth050pt;table1.BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderRight.LineStyleWord.WdLineStyle.wdLineStyleDouble
36、Wavy;table1.BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderBottom.Visible=true;table1.BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderBottom.ColorWord.WdColor.wdColorGreen;table1.BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderBottom.LineWidthWord.WdLineWidth.wdLineWidth050pt;table1.BordersMicrosoft.Office.Interop.Word.WdBorderType.wdBorderBottom.LineStyleWord.WdLi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年健康产业研究报告:市场需求与创新发展
- 2026年智能制造行业解决方案报告及未来五年市场前景预测
- 2026年健身行业市场前景报告:聚焦瑜伽、普拉提、球类、游泳培训发展动态
- 2026年健身行业私教个性化服务市场报告
- 2026年人工智能在制造行业的智能化改造报告
- 2026年服装产业链分析报告:原材料供应与成本控制
- 2026年新型铝基轴瓦材料行业商业计划书
- 2026年环保材料行业生物降解塑料市场分析报告
- 2026年输油泵热力机械行业发展趋势报告
- 2026年铌产品创新应用案例研究报告
- 2025年企业主要负责人安全培训考试题及完整答案【各地模拟题】
- 重庆谈判历史学习课件
- YMO数学竞赛2024秋季四年级复赛真题试卷含答案解析可打印
- 贵州省2024年12月普通高中学业水平合格性考试数学试卷(含答案)
- 生猪屠宰兽医卫生检验人员理论考试题及答案
- 生产部人员岗位技能矩阵图
- DL∕T 1878-2018 燃煤电厂储煤场盘点导则
- HG∕T 4104-2019 水性氟树脂涂料
- 机械加工财务管理制度
- 第10章-干法刻蚀课件
- LY/T 2906-2017美国白蛾核型多角体病毒杀虫剂
评论
0/150
提交评论