200分求Delphi7中将数据集导入到Excel和Word 最好有实现代码 (200分)

  • 主题发起人 主题发起人 xuzhsyf
  • 开始时间 开始时间
X

xuzhsyf

Unregistered / Unconfirmed
GUEST, unregistred user!
200分求Delphi7中将数据集导入到Excel和Word 最好有实现代码
 
http://delphibbs.com/delphibbs/dispq.asp?lid=1691966
导入导出一个道理
 
unit Unit1;

interface



uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables, Menus, Grids, DBGrids, ExtCtrls,comobj, Gauges, ComCtrls;

type
TForm1 = class(TForm)
Panel1: TPanel;
DBGrid1: TDBGrid;
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
DataSource1: TDataSource;
Query1: TQuery;
SaveDialog1: TSaveDialog;
ProgressBar1: TProgressBar;
Gauge1: TGauge;
procedure N2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
const
charArray:array[1..26] of char=
('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');

{$R *.DFM}

procedure TForm1.N2Click(Sender: TObject);
var
ss:string;
exc:variant;
excworkbook,excworksheet:variant;
i,j:integer;
str:tstrings;
begin
ss:='temp2';
with query1 do
begin
close;
sql.clear;
sql.add('select * from "'+ss+'"');
open;
end;
try
exc:=CreateOleObject('Excel.Application');
excworkbook:=exc.WorkBooks.Add;
excworksheet:=exc.worksheets.add;
except
showmessage('no install excel');
exit;
end;

str:=tstringlist.Create; //?才﹃??
query1.GetFieldNames(str);

for j:=1 to str.Count do
begin
excworksheet.range[chararray[j]+'1'].value:=str.Strings[j-1] ; //?excel??い糶??琿
end;

query1.First;
//_______progressbar and gauge
progressbar1.Visible:=true;
gauge1.Visible:=true;

progressbar1.Position:=0;
gauge1.Progress:=0;

progressbar1.Max:=query1.RecordCount;
gauge1.MaxValue:=query1.RecordCount;

//_______progressbar and gauge initlization;

i:=1;
while not query1.Eof do
begin
inc(i);
try
for j:=1 to str.Count do
begin
excworksheet.range[chararray[j]+inttostr(i)].value:=query1.Fields[j-1].AsString;
end;
except
showmessage('cann''t export excel');
end;
query1.Next;
//________

progressbar1.StepIt;
gauge1.AddProgress(1);
end;

savedialog1.Filter:='excel(*.xls)|*.xls';
if savedialog1.Execute then
begin
try
excworksheet.saveas(savedialog1.FileName);
except
showmessage('save error');
exit;
end;

end;
exc.quit;
//___________

progressbar1.Visible:=false;
gauge1.Visible:=false;
end;

end.
 
var
ExcelId : Variant;

在 uses 中加上 ComObj单元;
下面是一导出从dbgrid中导出到EXCEL中的过程,我就是用这个导数据的!
Procedure OutToExcel(qry: Tquery; grd: TDBgrid);
var
ss:string;
n,i:integer;
begin

n := 1;
ExcelId := createOleObject('Excel.application');
Excelid. Workbooks. Add;
for I := 1 to grd. FieldCount do
begin
ss := grd. fields[i-1]. DisplayName;
ExcelId. cells[n,i].value := ss;
end;
n := n+1;
qry. First;
while not qry. Eof do
begin
for I := 1 to grd. FieldCount do
begin
ss := grd. fields[i-1]. AsString;
ExcelId. Cells[n,i]. value := ss;
end;
qry. next;
n := n+1;
end;
ExcelId. visible := true;
try
ExcelId. Quit;
finally
messagedlg('成功导出!',mtinformation,[mbok],0);
end;
end;
 
后退
顶部