您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 广告经营 > 在Delphi中很精确地控制生成的WORD文档的格式
在Delphi中很精确地控制生成的WORD文档的格式varWordApplication1:TWordApplication;通过以下的方式就可以在DELPHI中很精确地控制生成的WORD文档的格式。//-----------------打开WORD-------------------------------Wordapplication1.visible:=true;//显示WRODWorddocument1.activate;//-----------------设置字体--------------------------------wordapplication1.Selection.Font.Size:=14;//字号wordapplication1.Selection.Font.Name:='宋体';//字体wordapplication1.Selection.Font.Bold:=wdToggle;//加粗//-------------将图形文件插入文档当前位置--------------------Wordapplication1.Selection.InlineShapes.AddPicture('pict.jpg',emptyparam,emptyparam,emptyparam);//---------------换行---------------------wordapplication1.Selection.TypeParagraph;//------------写入文字---------------wordapplication1.Selection.TypeText('这是用TypeText方法向文档写入的文字');//------------写带下划线的文字----------------wordapplication1.Selection.Font.UnderlineColor:=wdcolorautomatic;//下划线颜色wordapplication1.Selection.Font.Underline:=wdUnderlineSingle;//下划线线型wordapplication1.Selection.TypeText('这是下划线文字');wordapplication1.Selection.Font.Underline:=wdUnderlineNone;//无下划线//---------------文字居中----------------wordapplication1.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;//居中对齐wordapplication1.Selection.TypeText('水平对齐文字');wordapplication1.Selection.ParagraphFormat.Alignment:=wdAlignParagraphLeft;//靠左对齐//--------------插入分节符,其中t:olevariant;---------------------t:=wdPageBreak;wordapplication1.Selection.InsertBreak(t);//-----------------插入分页符,其中t:olevariant;----------------------------t:=wdSectionBreakNextPage;wordapplication1.Selection.InsertBreak(t);//-------------------生成页眉页脚并写入数据----------worddocument1.ActiveWindow.ActivePane.View.SeekView:=wdseekcurrentpageheader;//选页眉对象wordapplication1.Selection.TypeText('这是页眉文字);wordapplication1.Selection.ParagraphFormat.Alignment:=wdAlignParagraphLeft;//对齐方式wordapplication1.Selection.PageSetup.DifferentFirstPageHeaderFooter:=-1;//首页不同worddocument1.ActiveWindow.ActivePane.View.SeekView:=wdseekcurrentpageFooter;//选页脚对象//页脚上面横线wordapplication1.Selection.WholeStory;wordapplication1.Selection.ParagraphFormat.Borders.Item(wdBorderLeft).LineStyle:=wdLineStyleNone;wordapplication1.Selection.ParagraphFormat.Borders.Item(wdBorderLeft).LineStyle:=wdLineStyleNone;wordapplication1.Selection.ParagraphFormat.Borders.Item(wdBorderRight).LineStyle:=wdLineStyleNone;wordapplication1.Selection.ParagraphFormat.Borders.Item(wdBorderTop).LineStyle:=wdLineStyleSingle;wordapplication1.Selection.ParagraphFormat.Borders.Item(wdBorderTop).LineWidth:=wdLineWidth050pt;wordapplication1.Selection.ParagraphFormat.Borders.Item(wdBorderTop).Color:=wdColorAutomatic;wordapplication1.Selection.ParagraphFormat.Borders.Item(wdBorderBottom).LineStyle:=wdLineStyleNone;wordapplication1.Selection.ParagraphFormat.Borders.DistanceFromTop:=1;wordapplication1.Selection.ParagraphFormat.Borders.DistanceFromLeft:=4;wordapplication1.Selection.ParagraphFormat.Borders.DistanceFromBottom:=1;wordapplication1.Selection.ParagraphFormat.Borders.DistanceFromRight:=4;wordapplication1.Selection.ParagraphFormat.Borders.Shadow:=False;//页脚文字wordapplication1.Selection.TypeText('这是页脚文字');wordapplication1.Selection.ParagraphFormat.Alignment:=wdAlignParagraphRight;//对齐方式wordapplication1.Selection.PageSetup.DifferentFirstPageHeaderFooter:=-1;//切换到文档区域wordapplication1.ActiveWindow.ActivePane.View.SeekView:=wdseekmaindocument;//------------------写带上、下标的文字-------------------------Wordapplication1.Selection.TypeText('正常文字1');Wordapplication1.Selection.Font.Superscript:=-1;//设置为上标Wordapplication1.Selection.TypeText('上标文字');Wordapplication1.Selection.Font.Superscript:=0;//恢复正常文字Wordapplication1.Selection.TypeText('正常文字2');Wordapplication1.Selection.Font.Subscript:=-1;//设置为下标Wordapplication1.Selection.TypeText('下标文字');Wordapplication1.Selection.Font.Subscript:=0;//恢复正常文字//--------------创建各级标题--------------prop:='标题1';//varprop:olevariant;withWordapplication1.SelectiondobeginSet_Style(prop);//设为一级标题Font.Name:='宋体';//标题字体Font.Size:=14;//标题字号ParagraphFormat.Alignment:=wdAlignParagraphCenter;//标题对齐方式ParagraphFormat.SpaceBefore:=12;//标题段前距离ParagraphFormat.SpaceAfter:=6;//标题段后距离Font.Bold:=1;//加粗TypeText('标题文字');TypeParagraph;prop:='正文';//恢复正文格式Set_Style(prop);Font.Name:='宋体';Font.Size:=10.5;Font.Bold:=0;end;//--------------------表格处理---------------------------//其中:t:olevariant;TableName:string;row,col:integer;withWordapplication1.Selectiondo//写表名ParagraphFormat.Alignment:=wdAlignParagraphCenter;ParagraphFormat.SpaceBefore:=6;ParagraphFormat.SpaceAfter:=6;TypeText('表1'+TableName);TypeParagraph;//插入表格t:=doc.tables.Add(Range,row,col,1,0);//插入row行col列的表t.range.ParagraphFormat.Alignment:=wdAlignParagraphCenter;//表格居中t.Range.Font.Size:=9;//单元格字号为小五t.range.Cells.VerticalAlignment:=wdCellAlignVerticalCenter;//单元格垂直居中t.range.Rows.Alignment:=wdAlignRowCenter;//单元格水平居中//设置每列宽度t.Columns.Item(1).Width:=40;t.Columns.Item(2).Width:=92;……//合并单元格t.cell(9,4).merge(t.cell(9,5));//向单元格写入文字t.cell(1,1).range.text:='序号';//第一行第一列t.cell(1,2).range.text:='姓名';//第一行第二列end;//----------页面设置:修改纸张为A3横向------------withwordapplicatio
本文标题:在Delphi中很精确地控制生成的WORD文档的格式
链接地址:https://www.777doc.com/doc-2561711 .html