如何在delphi中按照一定的格式生成一篇word文档如果已知组件中的内容
比方说Tedit中存放的是标题
Tmemo中存放的是正文
……
如何将他们按照一定的格式组成一篇word文档
譬如说标题必须是多少号黑体,正文必须是宋体之类的
--
先在word中通过录制宏,获取VBA代码.然后转换成delphi代码就可以啦.---
给您一个写好的类,您可以在添加您需要的内容就可以啦.
字体,之类的通过在word中录制宏就可以啦,很简单的.
unit Word2003;
interface
uses Microsoft Windows,ComObj,Forms,Variants,DBClient,Messages, SysUtils,
Classes, Graphics, Controls,Dialogs,StdCtrls, jpeg,
ExtCtrls,ComCtrls,Word2000,data,sqlFunction,declare;
type
TWord2003=class(TObject)
vWordApp:OLEVariant;
vSelect:OLEVariant;
private
public
//初始化word2003
Function Init_Word2003:Boolean;
//添加一段文字
Function Add_Text(AddStr:String;FontSize:integer;IsFontBold:Boolean;align:Integer):Boolean;
//添加一个图片
Function Add_Image(ImagePath:String;Align:Integer):Boolean;
//添加一个表格
Function Add_Table(lv:tlistview;FontSize:integer):Boolean;
end;
implementation
{TwordWord2003}
//添加一张图片
function Tword200III.Add_Image(ImagePath:String;Align:Integer): Boolean;
begin
result:=true;
try
if imagePath='' then exit;
vSelect.Font.Bold:=false;
vSelect.InlineShapes.AddPicture(FileName:=imagePath,
LinkToFile:=False,SaveWithDocument:=True);
//显示位置(左,中间,右)
case align of
integer(align_left): vSelect.ParagraphFormat.Alignment:=wdAlignParagraphleft;
integer(align_center): vSelect.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
integer(align_right): vSelect.ParagraphFormat.Alignment:=wdAlignParagraphRight;
end;
//文字尾,回车
vSelect.EndKey(Unit:=wdLine);
vSelect.TypeParagraph;
except
end;
end;
//添加一个表
function Tword200III.Add_Table(lv:tlistview;FontSize:integer): Boolean;
var
vTable,vCell:OLEVariant;
nRowCount,nColCount:Integer;
i,j:integer;
begin
result:=true;
try
//字体
vSelect.Font.Bold:=false;
vSelect.Font.Size :=FontSize;
// 要插入表格的行数
nRowCount:=lv.Items.Count+1;
if nRowCount <2 then nRowCount:=2;
// 要插入表格的列数
nColCount:=lv.Columns.Count;
if ncolcount <1 then ncolcount:=1;
// 在Word文档中插入与ListView行数列数相同的一个表格
vTable:=vWordApp.ActiveDocument.Tables.Add(Range:=vSelect.Range,
NumRows:=nRowCount,NumColumns:=ncolcount,
DefaultTableBehavior:=wdWord9TableBehavior,
AutoFitBehavior:=wdAutoFitFixed);
// 将列名写入Word表格先
for i:=0 to lv.Columns.Count-1 do
begin
vcell:=vtable.cell(1,i+1);
vcell.range:=lv.Columns.items[i].Caption;
// 列名单元格背景颜色 // wdColorGray125
vcell.shading.backgroundPatternColor:=14737632;
end;
// 将ListView中的数据(Data)写入Word表格(如果记录数不为0,执行)
if lv.Items.Count <>0 then
begin
for i:=0 to nrowcount-1-1 do
begin
vcell:=vtable.cell(i+2,1);
vcell.range:=lv.Items[i].Caption;
for j:=0 to lv.Items[i].SubItems.Count-1 do
begin
if lv.Columns.Count>j then
begin
vcell:=vtable.cell(i+2,j+2);
vcell.range:=lv.Items[i].SubItems.Strings[j];
end;
end;
end;
end;
//文档最后
vwordApp.Selection.MoveDown(Unit:=wdLine,Count:=nRowCount);
vwordApp.Selection.MoveDown(Unit:=wdLine,Count:=nRowCount);
vwordApp.Selection.MoveDown(Unit:=wdLine,Count:=nRowCount);
vwordApp.Selection.MoveDown(Unit:=wdLine,Count:=nRowCount);
vwordApp.Selection.MoveDown(Unit:=wdLine,Count:=nRowCount);
except
end;
end;
//添加一段文字
function TWord200III.Add_Text(AddStr:String;FontSize:integer;IsFontBold:Boolean;align:integer):Boolean;
begin
result:=true;
try
vSelect.Font.Bold:=false;
vSelect.Font.Size :=FontSize;
if isFontBold then
vSelect.Font.Bold:=wdToggle
else
vSelect.Font.Bold:=false;
vSelect.TypeText(Text:=AddStr);
//显示位置(左,中间,右)
case align of
integer(align_left): vSelect.ParagraphFormat.Alignment:=wdAlignParagraphleft;
integer(align_center):vSelect.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
integer(align_right): vSelect.ParagraphFormat.Alignment:=wdAlignParagraphRight;
end;
//文字尾,回车
vSelect.EndKey(Unit:=wdLine);
vSelect.TypeParagraph;
except
end;
end;
function TWord200III.Init_Word2003: Boolean;
begin
result:=true;
try
vWordApp:=GetActiveOleObject('Word.Application');
except
//未运行则运行之
vWordApp:=CreateOleObject('Word.Application');
end;
try
//显示Word界面
vwordApp.visible:=true;
//新建一个文档
vwordApp.Documents.Add;
vselect:=vwordApp.Selection;
vselect.Font.Size:=12;
vselect.Font.Name:='宋体';
except
vWordApp:=Unassigned;
result:=false;
MessageDlg('不能生成文档,请确认是否安装了Word 2003!',mtError,[mbOK],0);
end;
end;
end.
//-----------------------
涉及到的变量定义:
//插入数据(Data)位置(0:左,1:中间,2:右)
Type TAlign_State=(Align_Left,align_Center,align_Right);---
上面的代码调用的单元,data,sqlfunction,delcare,用不到,您去掉就可以啦.---
学习了,我只知道怎么从word获取,到过来还没遇过呢.---
强烈谢2楼的家伙!!!---
还有一个问题,如何在word文挡中插入一条横线,如何设置他的颜色,粗细
还有其他类似的在delphi中控制word的代码