如何将数据保存到word文件(表格形式)?(200分)

  • 主题发起人 主题发起人 lingb
  • 开始时间 开始时间
这个程序的关键就是对WORD中的表格项的操作,如果你熟悉WORD文档的打开和数据库的查询的话,我先将关键代码如下:
table.cell[i,j].range.text;=adoquery1.fields[0].asstring;//将数据表中的数据写入WORD的第I行J列中;
table.cell[i,j].range.font.color:=clred;//颜色设置
如果你不熟悉WORD文档的打开和数据库的查询的话,需要更详细的代码,我只能明天或周一给你比较完整的代码,我的代码都在服务器上,我专门作过很复杂的WORD的导入和导处
 
谢谢,当程序走到table;=doc.table.item[1];这一步就报错:
Method 'doc' not supported by automation object

期待着。。。谢谢:)
 
那你检查一下 doc的定义,没关系,我一定给你回复的,我有现成的代码,我还得开会,先下了.
 
前面李翔鹏提醒的要在uses 中 加入comobj单元是非常重要的;
看了你的帖子,你数据绑定好象用的是BDE,我用的是adoconnection,其实无所谓的,只要数据联上就可以,这个应该很好做的
先面我将一定关键原代码提供如下:
procedure writeht(sender:Tobject);
var
word,table,doc:olevariant;
begin
opendialog_drht.initialdir:=extractfiledir(paramstr(0));
if opendialog_drht.execute then
begin
word:=createoleobject('word.application');
word.document.open(filename:=opendialog_drht.filename,readonly:=false);
word.visible:=true;
doc:=word.activedocument;
with adoquery1 do
begin
sql.clear;
sql.add('select * from table1');
execsql;
end;
table:=doc.tables.item(1);
for i;=1 to adoquery1.recordcount do
begin
table.cell[1,1]:=adoquery.fields[0].asstring;
table.cell[1,2]:=adoquery.fields[1].asstring;
....
adoquery1.next;
end;
end;
end;
这样基本就可以了。有问题可以再联系
我的email:myblueocean@163.com
 
为何执行到word.document.open(filename:=Savedialog1.filename,readonly:=false);这句时报错如下:Method 'document' not supported by automation object
 
程序如下:
else if (Savedialog1.Execute)and (savedialog1.FilterIndex=2) then
begin
fs:=Savedialog1.Filename;
if not FileExists(fs) then //Îļþ²»´æÔÚ´´½¨¸ÃÎļþ
begin
fs:=Savedialog1.Filename+'.doc';
CreateFile(Pchar(fs),GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_NEW,FILE_ATTRIBUTE_ARCHIVE,4);
end;
word:=createoleobject('word.application');
{if pos('.doc',fs)>0 then
fs:=Copy(fs,1,length(fs)-4); }
word.document.open(filename:=Savedialog1.filename,readonly:=false);//fs,readonly:=false);//
word.visible:=false;
doc:=word.activedocument;
table:=doc.table.item[1];
for n:=0 to query1.FieldCount-1 do
table.cells[1,n+1].value:=query1.Fields[n].DisplayLabel;
query1.First;
j:=2;
while not query1.Eof do
begin
for i:=0 to query1.FieldCount-1 do
begin
Exc:=query1.Fields.AsString;
table.cells[j,i+1].range.text:=Exc;
table.cellls[j,i+1].range.font.color:=DBGrid1.Canvas.Font.Color;
end;
query1.next;
Inc(j);
end;
doc.saveas(savedialog1.FileName);
doc.quit;
Application.MessageBox('µ¼³öÍê±Ï!','Ìáʾ',Mb_ok or Mb_Iconinformation);
end;
 
我也试过了,保存到Word时也报错,请大侠指点
 
word.Documents.open(filename:=Savedialog1.filename,readonly:=false);//看清楚比你多了个s。
 
按楼上的方法试了,运行到此处仍然出错,错误如下:

word在试图打开文件时遇到错误。
请尝试下列方法:
*检查文档或驱动器的文件权限。
*确保有足够的内存和磁盘空间。
*用文本恢复转换器打开文件。
 
word.Documents.open('C;/try.doc');//直接指定文件试试,如果成功再
word.Documents.open(Opendialog1.filename);
 
仍然不行啊:(
 
当运行到table:=doc.table.item[1];时也报错:
Method 'table' not supported by automation object
 
table:=doc.tables.item[1];//看清楚又比你多了个s。
 
没有答案啦?
 

Similar threads

后退
顶部