导入到WORD?(130分)

T

tuanzi

Unregistered / Unconfirmed
GUEST, unregistred user!
我要从delphi中导出到word中
要导出到固定的表格中(是在word画的)
做一个例子或给出代码!
 
估计要看看vba
建立用word录制一个宏看看vba是怎么做的
然后在delphi中使用vba的做法
这类资料dfw上是搜得到的吧
 
vba,有没有关于这一方面的书,
网址?
 
word中的表格的格式是否是固定的还是不固定的
请讲的详细一点。
 
啊,我理解错了。
 
表格的格式是固定的 ,如
姓名 xxxx 性别 x 所学专业 xxxxx
 
你可作一个模板文件
模板文件中对每一表格作个书签
在delphi中根据书签的名称决定写什么数据
用wordApplication连接。到word然后用你的模板新建一个文档。
循环书签,填入数值。
 
可是这要怎么做啊,
我是一点头绪也没有
你可不可以做一个小例子
示范一下。给我看一下,
 
try
WordApplication1.Connect;
except
raise Exception.Create('Word程序您没有安装!');
Abort;
end;
WordApplication1.Visible := true;

Template := EmptyParam;
NewTemplate := True;
ItemIndex := 1;

WordApplication1.Documents.Add(Template, NewTemplate,sa,sd);

WordDocument1.ConnectTo(WordApplication1.Documents.Item(ItemIndex));
WordApplication1.Options.CheckSpellingAsYouType := False;
WordApplication1.Options.CheckGrammarAsYouType := False;
//******************************************************************************
WordFont1.ConnectTo(WordDocument1.Sentences.First.Font);
WordFont1.Size := 12;
WordDocument1.Range.InsertAfter(ShowCap(EditTest.Text)+#13);
WordDocument1.Range.InsertAfter(ShowCap('出题人:'+EditName.Text)+#13);
if CheckBox1.Checked then
WordDocument1.Range.InsertAfter(ShowCap('出题时间:'+DateToStr(now))+#13);

Query1.First;
k := 1;
l := 1;
itype := Query1.Fields[0].AsInteger;
stype := ISType(itype);
WordDocument1.Range.InsertAfter(IntToStr(k)+': '+stype+#13);
WordDocument1.Range.InsertAfter(*****)
你可以式式

 
procedure TForm1.Button1Click(Sender: TObject);
var
I : Integer;
BookMark : Word97.BookMark;
Range : Word97.Range;
Templete : Olevariant;
NewTemp : OleVariant;
Index : OleVariant;
FileName : OleVariant;
begin
wa.Connect ;
wa.Visible := True;//可以设置为False
Templete := 'F:/Programs/Test/word/aa.dot'//文件的书签要设置好;
NewTemp := False;
wa.Documents.Add(Templete,NewTemp);
for I := 1 to wa.ActiveDocument.BookMarks.Count do
begin
Index := I;
BookMark := wa.ActiveDocument.BookMarks.Item(Index);
if BookMark.Name ='One' then//判断书签决定填写内容
begin
Range := BookMark.Range;
Range.Text := Table1.Fields[0].AsString ;
end;
end;
FileName := 'F:/Programs/Test/word/aa.doc';//要保存的文件名称
wa.ActiveDocument.SaveAs(FileName,EmptyParam,EmptyParam,EmptyParam,//保存文件
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam);
wa.Disconnect ;
wa.Quit ;
end;
 
wa.Documents.Add(Templete,NewTemp);
提示出错
BookMark : Word97.BookMark;
这是什么意思?
 
procedure TfrmXmgl.ToolButton19Click(Sender: TObject);
var
aa,bb,cc,dd,Word, Doc, Table,Fields,
NumRows,NumColumns,DefaultTableBehavior,AutoFitBehavior,Unit_,Count,Extend
: olevariant;
i: integer;
begin
if frmDataModule.Xmgl_Query2.RecordCount=0 then
begin
MessageDlg('未从数据库取得数据!',mtError,[mbOk],0);
exit;
end;
try
try
WordApplication1.Connect;
except
MessageDlg('请检查你的电脑上是否装有Word!',mtError,[mbOk],0);
Abort;
end;
WordApplication1.Visible := True;
WordApplication1.Options.CheckSpellingAsYouType:=false;
WordApplication1.Options.CheckGrammarAsYouType:=false;
aa:='E:+'/摸板.doc';
bb:=false;
cc:=wdNewBlankDocument;
dd:=true;
WordApplication1.Documents.Add(aa,bb,cc,dd);
WordDocument1.Connect;
WordApplication1.ActiveWindow.View.Type_:= wdPrintView;

Unit_:=wdLine;
Count:=3;
Extend:=wdMove;
WordApplication1.Selection.MoveDown(Unit_,Count,Extend);//初始光标下移

Unit_:=wdCell;
Count:=3;
Extend:=wdMove;
WordApplication1.Selection.MoveRight(Unit_,Count,Extend);//右移count个TAB格

With frmDataModule.Xmgl_Query2 Do
begin
first;
While Not Eof do
begin
Unit_:=wdCell;
Count:=1;
Extend:=wdMove;
WordApplication1.Selection.MoveRight(Unit_,Count,Extend);
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignRowCenter;
WordApplication1.Selection.TypeText(fieldbyname('ProjNo').AsString);

Unit_:=wdCell;
Count:=1;
Extend:=wdMove;
WordApplication1.Selection.MoveRight(Unit_,Count,Extend);
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignRowCenter;
WordApplication1.Selection.TypeText(fieldbyname('ProjName').AsString);

Unit_:=wdCell;
Count:=1;
Extend:=wdMove;
WordApplication1.Selection.MoveRight(Unit_,Count,Extend);
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignRowCenter;
WordApplication1.Selection.TypeText(fieldbyname('UnitNo').AsString);

Unit_:=wdCell;
Count:=1;
Extend:=wdMove;
WordApplication1.Selection.MoveRight(Unit_,Count,Extend);
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignRowCenter;
WordApplication1.Selection.TypeText(fieldbyname('DepNo').AsString);

Unit_:=wdCell;
Count:=1;
Extend:=wdMove;
WordApplication1.Selection.MoveRight(Unit_,Count,Extend);
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignRowCenter;
WordApplication1.Selection.TypeText(fieldbyname('SortNo').AsString);

Unit_:=wdLine; //下移
Count:=1;
Extend:=wdMove;
WordApplication1.Selection.MoveDown(Unit_,Count,Extend);


Unit_:=wdCell;
Count:=1;
Extend:=wdMove;
WordApplication1.Selection.MoveRight(Unit_,Count,Extend);
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignRowCenter;
WordApplication1.Selection.TypeText(fieldbyname('Layout').AsString);

Unit_:=wdCell;
Count:=1;
Extend:=wdMove;
WordApplication1.Selection.MoveRight(Unit_,Count,Extend);
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignRowCenter;
WordApplication1.Selection.TypeText(fieldbyname('SituNo').AsString);

Unit_:=wdCell;
Count:=1;
Extend:=wdMove;
WordApplication1.Selection.MoveRight(Unit_,Count,Extend);
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignRowCenter;
WordApplication1.Selection.TypeText(fieldbyname('SymNo').AsString);

Unit_:=wdCell;
Count:=1;
Extend:=wdMove;
WordApplication1.Selection.MoveRight(Unit_,Count,Extend);
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignRowCenter;
WordApplication1.Selection.TypeText(fieldbyname('PropNo').AsString);

Unit_:=wdCell;
Count:=1;
Extend:=wdMove;
WordApplication1.Selection.MoveRight(Unit_,Count,Extend);
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignRowCenter;
WordApplication1.Selection.TypeText(fieldbyname('SourceNo').AsString);

Unit_:=wdLine; //下移
Count:=1;
Extend:=wdMove;
WordApplication1.Selection.MoveDown(Unit_,Count,Extend);

Unit_:=wdCell;
Count:=1;
Extend:=wdMove;
WordApplication1.Selection.MoveRight(Unit_,Count,Extend);
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignRowCenter;
WordApplication1.Selection.TypeText(fieldbyname('Cancel').AsString);

Unit_:=wdCell;
Count:=1;
Extend:=wdMove;
WordApplication1.Selection.MoveRight(Unit_,Count,Extend);
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignRowCenter;
WordApplication1.Selection.TypeText(fieldbyname('CreateDate').AsString);

Unit_:=wdCell;
Count:=1;
Extend:=wdMove;
WordApplication1.Selection.MoveRight(Unit_,Count,Extend);
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignRowCenter;
WordApplication1.Selection.TypeText(fieldbyname('Operator').AsString);
Next;
end;
end;
except
on E:Exception do
begin
Showmessage(E.Message);
WordDocument1.Disconnect;
WordApplication1.Disconnect;
end;
end;
WordDocument1.Disconnect;
WordApplication1.Disconnect;
end;
 
太简单了,用我的试试
procedure Texport_form.BitBtn8Click(Sender: TObject);
var
i,j:integer;
str,str_name:string;
WordApp,WordDoc,WordParagraph,WordRange,WordTable:variant;
SltRec,SltCol,ColIndex,RowIndex : Integer;
begin
//导出word文件
if listbox3.Items.Text='' then begin
application.MessageBox('数据信息不能为空,请选择“数据库字段名”!','系统',mb_iconerror+mb_ok);
exit;
end else begin
if SaveDialog1.Execute then begin
str_name:=listbox1.Items[listbox1.ItemIndex];
if listbox3.Count>0 then begin
with data.gg do begin
close;
sql.Text:='select * from '+str_name;
open;
end;
if application.MessageBox('确认要导出数据吗?如果数据量过大,将会有一段时间不能响应!','警告!',MB_OKCANCEL+MB_ICONQUESTION)=IDCANCEL then
exit;
try
WordApp := CreateOleObject('word.Application');
Worddoc := WordApp.documents.add;
// wordapp.application.visible:=true;
WordParagraph := WordApp.activedocument.paragraphs.add;
WordRange := WordParagraph.range;
except
showmessage('请确认WORD安装正确!');
end;
export_form.Cursor:=crSQLWait;
begin
statusbar1.Panels[3].Text:='正在导出...!';
i:=0;
SltRec:=data.gg.RecordCount-1;
SltCol :=ListBox3.Count;
WordTable:= WordApp.activedocument.tables.add(WordRange,SltRec + 1,SltCol);
ColIndex := 1;
data.gg.first;
while not data.gg.eof do
begin
inc(i);
for j:=0 to listbox3.Count-1 do begin
str:=listbox3.Items.Strings[j];
WordTable.Cell(ColIndex,j+1).Range.InsertAfter(data.gg.FieldByName(str).asstring);
end;
ColIndex:=ColIndex+1;
data.gg.next;
end;
WordApp.activedocument.saveas(SaveDialog1.FileName);
WordApp.activedocument.close;
statusbar1.Panels[3].Text:='导出成功!';
export_form.Cursor:=crDefault;
end;
end else exit;
end else exit;
end;
end;
 
这么长,怎么没有注释啊
可不可以加一下
 
看我的。绝对好使!
以下是我测试 宏 与delphi得转换时写的一段代码
功能是写入word及保存。有表格,字体,格式,页眉页角都有。
delphi6+word2000下通过
在窗体上放一个EA1: TExcelApplication;
Button4: TButton;
SaveDialog1: TSaveDialog; 即可
我是先在word中进行某项操作同时录下宏,然后在delphi中进行转换
procedure TForm1.Button4Click(Sender: TObject);
var empDoc,NewTempDoc,TempWord,fileformat,TempEmpty,temp1,temp2,unit1,count,Extend:OleVariant;
pagefoot,TempDoc,numrows,DocumentType,DefaultTableBehavior,AutoFitBehavior:OleVariant;
i,j:integer;
wordsnap:wordbool;
InputString,dir,dir1:string;
year,month,day:word;
begin
TempEmpty:=EmptyParam;
TempDoc:= EmptyParam;
FileFormat:=wdFormatDocument;
DocumentType:=wdNewBlankDocument;
DefaultTableBehavior:=wdWord9TableBehavior;
AutoFitBehavior:=wdAutoFitFixed;
NewTempDoc:= True;
temp1:=false;
unit1:=wdLine;
temp2:=true;
Extend:=wdExtend;
try
wa1.Connect;
except
Application.Messagebox('word2000不知有点啥问题,无法启动!','记账本',mb_Ok);
exit;
end;
WA1.Visible :=true;
count:=1;
InputString:= InputBox('签名框', '清把大名写在下面啦!', '');
savedialog1.Execute;
if SaveDialog1.FileName<>'noname' then
dir1:=SaveDialog1.FileName+'.doc'
else
dir1:='c:/ljh.doc';
for i:=length(dir1)-1 downto 0 do
begin
if dir1='/' then
begin
dir:=copy(dir1,1,i);
delete(dir1,1,i);
break;
end;
end;
wa1.Documents.Add(TempEmpty,TempEmpty,DocumentType,temp2);//新建一空文档
{开始写入}
wa1.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;//居中
wa1.ActiveWindow.Selection.Font.Bold := wdToggle;//粗体互换
wa1.ActiveWindow.Selection.Font.Size:=16;
wa1.Selection.TypeText('收支纪录');//写入
wa1.ActiveWindow.Selection.Font.Bold := wdToggle;
wa1.ActiveWindow.Selection.Font.Size:=12;
wa1.Selection.TypeParagraph;//换行,相当于 回车
//建表20*4
wa1.ActiveDocument.Tables.Add(wa1.ActiveWindow.Selection.Range,20,4,DefaultTableBehavior,AutoFitBehavior);
//以下是两种插入表格数据的方法。
for j:=1 to 4 do //先选中某个格然后写入
begin
wa1.Selection.Tables.Item(1).Cell(1,j).Select;
wa1.ActiveWindow.Selection.Font.Bold := wdToggle;
wa1.Selection.TypeText('哈哈'+inttostr(j));
end;
for j:=2 to 20 do //直接定位格写入
for i:=1 to 4 do
wa1.Selection.Tables.Item(1).Cell(j,i).Range.Text:='ljh'+inttostr(j)+','+inttostr(i);
wa1.Selection.Tables.Item(1).Cell(j-1,1).Select;//光标选定到表格最后一行第一格
unit1:=wdline;
wa1.ActiveWindow.Selection.MoveDown(unit1,count,TempEmpty);//光标下移
wa1.Selection.ParagraphFormat.Alignment:=wdAlignParagraphJustify;
wa1.Selection.TypeParagraph;
wa1.ActiveWindow.Selection.Font.Bold := wdToggle;
wa1.Selection.TypeText('各项收支统计结果:');
wa1.ActiveWindow.Selection.Font.Bold := wdToggle;
wa1.Selection.TypeParagraph;
wa1.ActiveWindow.Selection.Font.Bold := wdToggle;
wa1.Selection.TypeText('收支总计:100000');
wa1.Selection.TypeParagraph;
wa1.Selection.TypeText(' 大写:人民币十万');
wa1.Selection.TypeParagraph; //换行
wa1.ActiveWindow.Selection.Font.Bold := wdToggle;
wa1.Selection.ParagraphFormat.Alignment:= wdAlignParagraphRight;
wa1.ActiveWindow.Selection.Font.Size:=10;
wa1.Selection.TypeText(InputString);
wa1.Selection.TypeParagraph;
decodedate(now,year,month,day);
wa1.Selection.TypeText(format('%4d年%2d月%2d日',[year,month,day]));
//页眉页角
If (wa1.ActiveWindow.ActivePane.View.Type_ = wdNormalView) Or (wa1.ActiveWindow.ActivePane.View.Type_ = wdOutlineView) Then
wa1.ActiveWindow.ActivePane.View.Type_:= wdPrintView;
wa1.ActiveWindow.ActivePane.View.SeekView:=wdSeekCurrentPageHeader;//定位到页眉
wa1.Selection.TypeText(InputString+'报收支纪录');
wa1.ActiveWindow.ActivePane.View.SeekView:=wdSeekCurrentPageFooter;//切换到页角
wa1.Selection.TypeText('第');
pagefoot:=wdfieldpage;
wa1.Selection.Fields.Add(wa1.Selection.Range,pagefoot,TempEmpty,TempEmpty);//插入页码
wa1.Selection.TypeText('页');
wa1.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;//居中
wa1.ActiveWindow.ActivePane.View.SeekView:=wdSeekMainDocument;//返回到主页面
wa1.ChangeFileOpenDirectory(dir);
TempWord:=dir1;
wa1.ActiveDocument.SaveAs(TempWord,FileFormat,temp1,TempEmpty,temp2,TempEmpty,temp1,temp1,temp1,temp1,temp1);
Application.Messagebox(pchar('数据成功导出' + dir+dir1),'记账本',mb_Ok);
wa1.Disconnect;
end;
 
不对啊
我是要导到WORD
不是Excel
 
好像我还是搞不懂这么多代码是有什么作用?
 
简单的方法,在每个表格中设定一个特殊字符串(字体,格式等都设好),然后
查找该字符串,用相应内容进行替换旧可以了!!!
替换代码如下:
Procedure TForm1.Replace_string(Word_App:TWordApplication;Find_string,Replaced_string:string);
var //查找替换WORD中的指定字符串
FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike,MatchKashida,MatchDiacritics,
MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace,MatchAlefHamza,MatchControl: OleVariant;
Range_start,Range_end,i,Range1:OleVariant;
Document_range:Range;
begin
FindText := Find_string;
MatchCase := False;
MatchWholeWord := True;
MatchWildcards := False;
MatchSoundsLike := False;
MatchAllWordForms := False;
Forward := True;
Wrap := wdFindContinue;
Format := False;
ReplaceWith := Replaced_string;
Replace := wdReplaceAll;
MatchKashida:= False;
MatchDiacritics:= False;
MatchAlefHamza:= False;
MatchControl:= False;
Document_range:=Word_App.ActiveDocument.Content;
Document_range.Find.Execute( FindText, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
Wrap, Format, ReplaceWith, Replace,MatchKashida,MatchDiacritics,
MatchAlefHamza,MatchControl);
end;
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1902976
帮忙看看我的问题吧!!
 
我做了不少的这样的东东:把数据库里的数据直接插入到已经做好的WORD中,数据和WORD中相应的位置对应,相当于一个简易的办公文档管理。很简单,和heqian的原理一样,有需要的给我发MAIL,我奉献奉献!
 
顶部