呢, 可要给我 90 分哟!!!!, 刚刚好我也在做delphi+word+web报表问题,看看我们用的例程,应该可以了! procedure TForm1.Button1Click(Sender: TObject); var templateName: OleVariant; newTemplate: OleVariant; ItemIndex: OleVariant; vSelection: Selection; vBookMark: BookMark; vTable: Table; I : Integer; begin //构造打印模板文件名全路径 case ListBox1.ItemIndex of 0: begin templateName := folderName + 'cld.dot'; end; 1: begin templateName := folderName + 'cljg.dot'; end; end; newTemplate := False; try wordApp.Connect(); except MessageDlg('您的计算机上还未安装Microsoft Office Word97或更高的版本!', mtError, [mbOK], 0); Abort; end; //以指定的模板文件创建新Word文档 wordApp.Documents.AddOld(templateName, newTemplate); wordApp.Caption := ListBox1.Items.Strings[ListBox1.ItemIndex]; vSelection := wordApp.Selection; case ListBox1.ItemIndex of 0: begin //取文档中的第1张表 vTable := wordApp.ActiveDocument.Tables.Item(1); vTable.Cell(1, 2).Range.Text := '李鑫 于 北京供电公司'; vTable.Cell(1, 4).Range.Text := '昆明锐祺软件'; vTable.Cell(2, 2).Range.Text := '昆明市白塔路延长线387号星耀大厦1102B座 李鑫(收) 650051 (0871)3126628'; vTable.Cell(3, 2).Range.Text := DateToStr(Now); vTable.Cell(3, 4).Range.Text := DateToStr(Now); vTable.Cell(5, 1).Range.Text := '哈哈哈哈哈哈,我也不知道了。'; vTable.Cell(7, 1).Range.Text := '不知道了'; vTable.Cell(9, 2).Range.Text := '不知道'; end; 1: begin ItemIndex := 1; end; end; for I := 1 to wordApp.ActiveDocument.Books.Count do begin ItemIndex := I; //取指定Index的书签 vBookMark := wordApp.ActiveDocument.Books.Item(ItemIndex); //ShowMessage(vBookMark.Name+IntToStr(wordApp.ActiveDocument.Books.Count)+'Now='+IntToStr(i)); if LowerCase(vBookMark.Name) = 'nd' then begin vBookMark.Select(); vSelection.InsertAfter('2003'); end; if LowerCase(vBookMark.Name) = 'bh' then begin vBookMark.Select(); vSelection.InsertAfter('1'); end; end;
wordApp.Visible := CheckBox1.Checked; wordApp.UserName := '李鑫'; //先最小化 wordApp.WindowState := 2; //再最大化 wordApp.WindowState := 1; //打印预览 wordApp.PrintPreview := CheckBox2.Checked; //立即打印 if CheckBox3.Checked then wordApp.PrintOutOld; wordApp.Disconnect(); end;procedure TForm1.FormCreate(Sender: TObject); var templateName: String; fileStream: TResourceStream; begin //获得应用程序所在的目录 folderName := ExtractFilePath(Application.ExeName); if not FileExists(folderName+'template.res') then begin MessageDlg('错误:找不到模板资源文件template.res!', mtError, [mbOK], 0); Close; end; //构造WORD打印模板的文件名全路径 templateName := folderName + 'cld.dot'; //获得资源文件里的“处理单”打印模板 fileStream := TResourceStream.Create(hInstance, 'cld', 'WORD'); if not FileExists(templateName) then begin //拆离文件 fileStream.SaveToFile(templateName); //释放 fileStream.Free; end; //构造WORD打印模板的文件名全路径 templateName := folderName + 'cljg.dot'; //获得资源文件里的“处理结果”打印模板 fileStream := TResourceStream.Create(hInstance, 'cljg', 'WORD'); if not FileExists(templateName) then begin //拆离文件 fileStream.SaveToFile(templateName); //释放 fileStream.Free; end; ListBox1.Items.Add('处理单'); ListBox1.Items.Add('处理结果'); ListBox1.Selected[0] := True; end;end.
if you want use table in word,u must declare a table in word. just like this.vTable: Table;//declare the table u want use in Word;WordApplication.activedocument.tables.add(wordappliction.activedocument.rang,row_count,Col_count,,)// you can get the rest two parameters when u type the left quote in Delphi Unit during ur promgraming!then u use the table to add the data from database to word document;just like this: vTable:=wordApplication.activedocument.tables.items(0) ; vTable.cells(row,col).range.text:='string';可要多给我分哟,这是我昨天晚上的心血呀, 一直到晚上12点哟。
如果你要用Excel 的话,就相对简单了,
你可以用OWC组件,那里操作电子表格很简单,当然把数据库中的数据
导入到TSpreadSheet组件中,也同样简单,生成报表导入到电子表格打印
就容易了;
procedure TForm1.Button1Click(Sender: TObject);
var
templateName: OleVariant;
newTemplate: OleVariant;
ItemIndex: OleVariant;
vSelection: Selection;
vBookMark: BookMark;
vTable: Table;
I : Integer;
begin
//构造打印模板文件名全路径
case ListBox1.ItemIndex of
0:
begin
templateName := folderName + 'cld.dot';
end;
1:
begin
templateName := folderName + 'cljg.dot';
end;
end; newTemplate := False; try
wordApp.Connect();
except
MessageDlg('您的计算机上还未安装Microsoft Office Word97或更高的版本!', mtError, [mbOK], 0);
Abort;
end; //以指定的模板文件创建新Word文档
wordApp.Documents.AddOld(templateName, newTemplate);
wordApp.Caption := ListBox1.Items.Strings[ListBox1.ItemIndex]; vSelection := wordApp.Selection; case ListBox1.ItemIndex of
0:
begin
//取文档中的第1张表
vTable := wordApp.ActiveDocument.Tables.Item(1); vTable.Cell(1, 2).Range.Text := '李鑫 于 北京供电公司';
vTable.Cell(1, 4).Range.Text := '昆明锐祺软件';
vTable.Cell(2, 2).Range.Text := '昆明市白塔路延长线387号星耀大厦1102B座 李鑫(收) 650051 (0871)3126628';
vTable.Cell(3, 2).Range.Text := DateToStr(Now);
vTable.Cell(3, 4).Range.Text := DateToStr(Now); vTable.Cell(5, 1).Range.Text := '哈哈哈哈哈哈,我也不知道了。';
vTable.Cell(7, 1).Range.Text := '不知道了';
vTable.Cell(9, 2).Range.Text := '不知道';
end;
1:
begin
ItemIndex := 1;
end;
end; for I := 1 to wordApp.ActiveDocument.Books.Count do
begin
ItemIndex := I;
//取指定Index的书签
vBookMark := wordApp.ActiveDocument.Books.Item(ItemIndex);
//ShowMessage(vBookMark.Name+IntToStr(wordApp.ActiveDocument.Books.Count)+'Now='+IntToStr(i));
if LowerCase(vBookMark.Name) = 'nd' then
begin
vBookMark.Select();
vSelection.InsertAfter('2003');
end; if LowerCase(vBookMark.Name) = 'bh' then
begin
vBookMark.Select();
vSelection.InsertAfter('1');
end;
end;
wordApp.Visible := CheckBox1.Checked;
wordApp.UserName := '李鑫';
//先最小化
wordApp.WindowState := 2;
//再最大化
wordApp.WindowState := 1;
//打印预览
wordApp.PrintPreview := CheckBox2.Checked; //立即打印
if CheckBox3.Checked then
wordApp.PrintOutOld; wordApp.Disconnect();
end;procedure TForm1.FormCreate(Sender: TObject);
var
templateName: String;
fileStream: TResourceStream;
begin
//获得应用程序所在的目录
folderName := ExtractFilePath(Application.ExeName); if not FileExists(folderName+'template.res') then
begin
MessageDlg('错误:找不到模板资源文件template.res!', mtError, [mbOK], 0);
Close;
end; //构造WORD打印模板的文件名全路径
templateName := folderName + 'cld.dot';
//获得资源文件里的“处理单”打印模板
fileStream := TResourceStream.Create(hInstance, 'cld', 'WORD'); if not FileExists(templateName) then
begin
//拆离文件
fileStream.SaveToFile(templateName);
//释放
fileStream.Free;
end; //构造WORD打印模板的文件名全路径
templateName := folderName + 'cljg.dot';
//获得资源文件里的“处理结果”打印模板
fileStream := TResourceStream.Create(hInstance, 'cljg', 'WORD');
if not FileExists(templateName) then
begin
//拆离文件
fileStream.SaveToFile(templateName);
//释放
fileStream.Free;
end; ListBox1.Items.Add('处理单');
ListBox1.Items.Add('处理结果');
ListBox1.Selected[0] := True;
end;end.
if you want use table in word,u must declare a table in word.
just like this.vTable: Table;//declare the table u want use in Word;WordApplication.activedocument.tables.add(wordappliction.activedocument.rang,row_count,Col_count,,)// you can get the rest two parameters when u type the left quote in Delphi Unit during ur promgraming!then u use the table to add the data from database to word document;just like this:
vTable:=wordApplication.activedocument.tables.items(0) ;
vTable.cells(row,col).range.text:='string';可要多给我分哟,这是我昨天晚上的心血呀, 一直到晚上12点哟。