在Delphi中很精确地控制生成的WORD文档的格式.docx
《在Delphi中很精确地控制生成的WORD文档的格式.docx》由会员分享,可在线阅读,更多相关《在Delphi中很精确地控制生成的WORD文档的格式.docx(6页珍藏版)》请在冰豆网上搜索。
在Delphi中很精确地控制生成的WORD文档的格式
在Delphi中很精确地控制生成的WORD文档的格式
varWordApplication1:
TWordApplication;通过以下的方式就可以在DELPHI中很精确地控制生成的WORD文档的格式。
//-----------------打开WORD-------------------------------
Wordapplication1.visible:
=true;//显示WROD
Worddocument1.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.Selectiondo
begin
Set_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: