mydelphiwithexcel

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.

  *********************************************************************************************++++++++++++
var
sl : TStringList;
i,j : Integer;
sLineText : string;
begin
sl := TStringList.Create;
sLineText := '';
with DBGrid1 do
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 := '';
for j:=0 to Columns.Count-1 do
begin
SHOWMESSAGE(sLineText);
sLineText := sLineText + Fields.FieldByName(Columns.Items[J].FieldName).AsString;
sLineText := sLineText + ',';
end;
sl.Add(Copy(sLineText,1,Length(sLineText)-1));
Next;
end;
end;
end;
//存文件
sl.SaveToFile('C:/aaa.csv');
sl.Free;
end;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值