参考一下下面的代码吧。!1 var MSExcel:Variant; i,j:Integer; begin SaveDialog1.Filter:='*.XLS|*.XLS'; SaveDialog1.DefaultExt:='XLS'; if SaveDialog1.Execute then begin MSExcel:=CreateOLEObject('Excel.Application'); MSExcel.WorkBooks.Add; MSExcel.Visible:=False; Table1.Open; j:=Table1.RecordCount; Table1.First; for i:=1 to j do begin MSExcel.Cells[i,1].NumberFormat:='@'; MSExcel.Cells[i,1].Value:=Table1.FieldByName('CODE').AsString; MSExcel.Cells[i,2].Value:=Table1.FieldByName('COLOR').AsString; Table1.Next; end; MSExcel.ActiveWorkBook.SaveAs(SaveDialog1.FileName); MSExcel.ActiveWorkBook.Saved:=True; MSExcel.Quit; end;
unit utoexcel;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, Grids, DBGrids, DBCtrls, ExtCtrls;type Tfrmtoexcel = class(TForm) Panel1: TPanel; Label1: TLabel; BitBtn1: TBitBtn; DBLookupComboBox1: TDBLookupComboBox; DBGrid1: TDBGrid; SaveDialog1: TSaveDialog; DBNavigator1: TDBNavigator; procedure DBLookupComboBox1Click(Sender: TObject); procedure BitBtn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var frmtoexcel: Tfrmtoexcel;implementation uses udm; {$R *.dfm}procedure Tfrmtoexcel.DBLookupComboBox1Click(Sender: TObject); begin dm.tbtoexcel.close; dm.tbtoexcel.sql.clear; dm.tbtoexcel.sql.add('select * from '+trim(DBLookupComboBox1.Text)); dm.tbtoexcel.open; dm.tbtoexcel.active:=true; dbgrid1.DataSource:=dm.dstoexcel; dbgrid1.DataSource.DataSet:=dm.tbtoexcel; end;procedure Tfrmtoexcel.BitBtn1Click(Sender: TObject); var i,j:integer; f:TextFile; sl:TStringList; begin with dm.tbtoexcel do begin if (not dm.tbtoexcel.Active) then Exit; if (not SaveDialog1.Execute) then Exit; AssignFile(f,SaveDialog1.FileName); try Rewrite(f); except Showmessage('保存文件失敗!'); CloseFile(f); Exit; end; Screen.Cursor := crHourGlass; DisableControls; sl := TStringList.Create; sl.Clear; for i:=0 to FieldCount-1 do begin if (Fields[i].Visible) then sl.Add(Fields[i].DisplayLabel); end; writeln(f,sl.CommaText); First; for j:=0 to RecordCount-1 do begin sl.Clear; for i:=0 to FieldCount-1 do begin if (Fields[i].Visible) then sl.Add(VarToStr(Fields[i].Value)); end; writeln(f,sl.CommaText); Next; end; sl.Free; CloseFile(f); EnableControls; Screen.Cursor := crDefault; end; // withend;end.
implementation uses udm; {$R *.dfm}procedure Tfrmtoexcel.DBLookupComboBox1Click(Sender: TObject); begin dm.tbtoexcel.close; dm.tbtoexcel.sql.clear; dm.tbtoexcel.sql.add('select * from '+trim(DBLookupComboBox1.Text)); dm.tbtoexcel.open; dm.tbtoexcel.active:=true; dbgrid1.DataSource:=dm.dstoexcel; dbgrid1.DataSource.DataSet:=dm.tbtoexcel; end;procedure Tfrmtoexcel.BitBtn1Click(Sender: TObject); var i,j:integer; f:TextFile; sl:TStringList; begin with dm.tbtoexcel do begin if (not dm.tbtoexcel.Active) then Exit; if (not SaveDialog1.Execute) then Exit; AssignFile(f,SaveDialog1.FileName); try Rewrite(f); except Showmessage('保存文件失敗!'); CloseFile(f); Exit; end; Screen.Cursor := crHourGlass; DisableControls; sl := TStringList.Create; sl.Clear; for i:=0 to FieldCount-1 do begin if (Fields[i].Visible) then sl.Add(Fields[i].DisplayLabel); end; writeln(f,sl.CommaText); First; for j:=0 to RecordCount-1 do begin sl.Clear; for i:=0 to FieldCount-1 do begin if (Fields[i].Visible) then sl.Add(VarToStr(Fields[i].Value)); end; writeln(f,sl.CommaText); Next; end; sl.Free; CloseFile(f); EnableControls; Screen.Cursor := crDefault; end; // withend;end.
我有个控件!可以将DATASET的结果,EXPORT成几种文件!
to ihihonline(小小->不要再流浪!!) : 我用过控件,也自己编过,不过就你所说的用自带的,是不是SERVER项。 我不怎么用,能否给点资料:[email protected]
var MSExcel:Variant;
i,j:Integer;
begin
SaveDialog1.Filter:='*.XLS|*.XLS';
SaveDialog1.DefaultExt:='XLS';
if SaveDialog1.Execute then
begin
MSExcel:=CreateOLEObject('Excel.Application');
MSExcel.WorkBooks.Add;
MSExcel.Visible:=False;
Table1.Open;
j:=Table1.RecordCount;
Table1.First;
for i:=1 to j do
begin
MSExcel.Cells[i,1].NumberFormat:='@';
MSExcel.Cells[i,1].Value:=Table1.FieldByName('CODE').AsString;
MSExcel.Cells[i,2].Value:=Table1.FieldByName('COLOR').AsString;
Table1.Next;
end;
MSExcel.ActiveWorkBook.SaveAs(SaveDialog1.FileName);
MSExcel.ActiveWorkBook.Saved:=True;
MSExcel.Quit;
end;
[email protected]
万分感谢
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, Grids, DBGrids, DBCtrls, ExtCtrls;type
Tfrmtoexcel = class(TForm)
Panel1: TPanel;
Label1: TLabel;
BitBtn1: TBitBtn;
DBLookupComboBox1: TDBLookupComboBox;
DBGrid1: TDBGrid;
SaveDialog1: TSaveDialog;
DBNavigator1: TDBNavigator;
procedure DBLookupComboBox1Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
frmtoexcel: Tfrmtoexcel;implementation
uses udm;
{$R *.dfm}procedure Tfrmtoexcel.DBLookupComboBox1Click(Sender: TObject);
begin
dm.tbtoexcel.close;
dm.tbtoexcel.sql.clear;
dm.tbtoexcel.sql.add('select * from '+trim(DBLookupComboBox1.Text));
dm.tbtoexcel.open;
dm.tbtoexcel.active:=true;
dbgrid1.DataSource:=dm.dstoexcel;
dbgrid1.DataSource.DataSet:=dm.tbtoexcel;
end;procedure Tfrmtoexcel.BitBtn1Click(Sender: TObject);
var
i,j:integer;
f:TextFile;
sl:TStringList;
begin
with dm.tbtoexcel do
begin
if (not dm.tbtoexcel.Active) then
Exit; if (not SaveDialog1.Execute) then
Exit; AssignFile(f,SaveDialog1.FileName);
try
Rewrite(f);
except
Showmessage('保存文件失敗!');
CloseFile(f);
Exit;
end; Screen.Cursor := crHourGlass;
DisableControls;
sl := TStringList.Create;
sl.Clear;
for i:=0 to FieldCount-1 do
begin
if (Fields[i].Visible) then
sl.Add(Fields[i].DisplayLabel);
end;
writeln(f,sl.CommaText); First;
for j:=0 to RecordCount-1 do
begin
sl.Clear;
for i:=0 to FieldCount-1 do
begin
if (Fields[i].Visible) then
sl.Add(VarToStr(Fields[i].Value));
end;
writeln(f,sl.CommaText);
Next;
end; sl.Free;
CloseFile(f);
EnableControls;
Screen.Cursor := crDefault;
end; // withend;end.
uses udm;
{$R *.dfm}procedure Tfrmtoexcel.DBLookupComboBox1Click(Sender: TObject);
begin
dm.tbtoexcel.close;
dm.tbtoexcel.sql.clear;
dm.tbtoexcel.sql.add('select * from '+trim(DBLookupComboBox1.Text));
dm.tbtoexcel.open;
dm.tbtoexcel.active:=true;
dbgrid1.DataSource:=dm.dstoexcel;
dbgrid1.DataSource.DataSet:=dm.tbtoexcel;
end;procedure Tfrmtoexcel.BitBtn1Click(Sender: TObject);
var
i,j:integer;
f:TextFile;
sl:TStringList;
begin
with dm.tbtoexcel do
begin
if (not dm.tbtoexcel.Active) then
Exit; if (not SaveDialog1.Execute) then
Exit; AssignFile(f,SaveDialog1.FileName);
try
Rewrite(f);
except
Showmessage('保存文件失敗!');
CloseFile(f);
Exit;
end; Screen.Cursor := crHourGlass;
DisableControls;
sl := TStringList.Create;
sl.Clear;
for i:=0 to FieldCount-1 do
begin
if (Fields[i].Visible) then
sl.Add(Fields[i].DisplayLabel);
end;
writeln(f,sl.CommaText); First;
for j:=0 to RecordCount-1 do
begin
sl.Clear;
for i:=0 to FieldCount-1 do
begin
if (Fields[i].Visible) then
sl.Add(VarToStr(Fields[i].Value));
end;
writeln(f,sl.CommaText);
Next;
end; sl.Free;
CloseFile(f);
EnableControls;
Screen.Cursor := crDefault;
end; // withend;end.
我用过控件,也自己编过,不过就你所说的用自带的,是不是SERVER项。
我不怎么用,能否给点资料:[email protected]
[email protected]
thanks
发来呢
var
ExcelApplication1: TExcelApplication;
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
i, j: integer;
filename: string;
begin
filename := concat(extractfilepath(application.exename), sName, ’.xls’);
try
ExcelApplication1 := TExcelApplication.Create(Application);
ExcelWorksheet1 := TExcelWorksheet.Create(Application);
ExcelWorkbook1 := TExcelWorkbook.Create(Application);
ExcelApplication1.Connect;
except
Application.Messagebox(’Excel 没有安装!’, ’Hello’, MB_ICONERROR + mb_Ok);
Abort;
end;
try
ExcelApplication1.Workbooks.Add(EmptyParam, 0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _worksheet);
AdsData.First;
for j := 0 to AdsData.Fields.Count - 1 do
begin
ExcelWorksheet1.Cells.item[3, j + 1] := AdsData.Fields[j].DisplayLabel;
ExcelWorksheet1.Cells.item[3, j + 1].font.size := ’10’;
end;
for i := 4 to AdsData.RecordCount + 3 do
begin
for j := 0 to AdsData.Fields.Count - 1 do
begin
ExcelWorksheet1.Cells.item[i, j + 1] :=
AdsData.Fields[j].Asstring;
ExcelWorksheet1.Cells.item[i, j + 1].font.size := ’10’;
end;
AdsData.Next;
end;
ExcelWorksheet1.Columns.AutoFit;
ExcelWorksheet1.Cells.item[1, 2] := Title;
ExcelWorksheet1.Cells.Item[1, 2].font.size := ’14’;
ExcelWorksheet1.SaveAs(filename);
Application.Messagebox(pchar(’数据成功导出’ + filename), ’Hello’,
mb_Ok);
finally
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
ExcelApplication1.Free;
ExcelWorksheet1.Free;
ExcelWorkbook1.Free;
end;
end;