1、添加ADOTable1控件,设置下列属性:
ConnectionString = Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Persist Security Info=false;Data Source=c:/abc.xls;
TableDirect=true;
TableName=sheet1$;
Active=true;
2、添加DataSource1控件,设置下列属性:
DataSet=ADOTable1
3、添加DBGrid1控件,设置下列属性:
DataSource=DataSource1
try
SL := TStringList.Create;
Rs := VIewData.CommonFind.Recordset.GetString(adClipString, -1, #44,#13, '');
SL.Text := Rs;
SL.SaveToFile(SaveDialog1.FileName+'.csv');
finally
SL.Free;
end;
*********************************************************************************************
//将query控件查找的纪录迅速保存
uses ADOInt; //必须添加
procedure TForm1.Button1Click(Sender: TObject);
var
Rs : string;
SL: TStringList;
begin
try
SL := TStringList.Create;
Rs := AdoQuery1.Recordset.GetString(adClipString, -1,#59,#13,'');
SL.Text := Rs;
SL.SaveToFile('C:/MyQry.txt'); //感觉保存为文本更好些。还可csv
finally
SL.Free;
end;
end;
*************************************************************************************
//导出为EXCEl文件
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, Grids, DBGrids, StdCtrls, ComObj;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button1: TButton;
ADOTable1: TADOTable;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
ole:olevariant;
ir,ic,i:integer;
begin
adotable1.Active:=false;
//adotable1.DatabaseName:=form1.pathdata;
//adotable1.TableName:='xmsqk.db';
try
ole:=createoleobject('excel.application');
except
showmessage('EXCEL没有安装');
exit;
ole.quit;
end;
if fileexists('c:/test.xls') then
begin
copyfile(pchar('c:/test.xls'), pchar('c:/testold.xls'),true);
// deletefile('c:/test.xls');
end
else
begin
//winexec(pchar(form1.pathdata +'/excel/test.exe'),0);
end;
ole.visible:=false;
ole.workbooks.open('c:/test.xls');
ole.visible:=true;
//==========
//*******
adotable1.Active:=true;
for ic:=0 to adotable1.FieldCount-1 do
begin
ole.cells.item[1,ic+1]:= adotable1.Fields[ic].FieldName;
end;
for ir:=2 to adotable1.RecordCount+1 do
begin//
for ic:=0 to adotable1.FieldCount-1 do
begin
ole.cells.item[ir,ic+1]:= adotable1.Fields[ic].AsString;
end;
adotable1.Next;
end; //
adotable1.Active:=false;
ole.quit;
end;
end.
sl : TStringList;
i,j : Integer;
sLineText : string;
begin
sl := TStringList.Create;
begin
//导出字段名
for i:=0 to Columns.Count-1 do
begin
sLineText := sLineText + Columns.Items[i].FieldName;
sLineText := sLineText + ',';
end;
sl.Add(Copy(sLineText,1,Length(sLineText)-1));
SHOWMESSAGE('OK');
//导出字段值
with DataSource.DataSet do
begin
First;
for i:=0 to RecordCount-1 do
begin
sLineText := '';
begin
SHOWMESSAGE(sLineText);
sLineText := sLineText + Fields.FieldByName(Columns.Items[J].FieldName).AsString;
sLineText := sLineText + ',';
end;
sl.Add(Copy(sLineText,1,Length(sLineText)-1));
end;
end;
end;
sl.SaveToFile('C:/aaa.csv');
end;