详细代码在此请各位大虾赐教:
//SaveDataToExcel的输出值是因为pb50调用时必须有返回值,所以先暂时用
integer类型返回值1
library saveexcel;
uses
SysUtils,Classes,Dialogs,ole2,
Unit_data in 'Unit_data.pas' {Form_Data};
{$R *.RES}
Function SaveDataToExcel(P_begin_date
char;P_end_date
char;P_path
char):
integer;stdcall;
var
str_begin_date,str_end_date,str_path:string;
FileHandle : Integer;
begin
With TForm_data.Create(nil) do
begin
try
coinitialize(nil);
str_begin_date:=strpas(P_begin_date);
str_end_date :=strpas(P_end_date);
str_path :=strpas(p_path);
if fileexists(str_path) then
begin
FileHandle := FileOpen(str_path,fmOpenRead );
if FileHandle > 0 then
begin//如果文件存在并且没有被打开,提示是否覆盖
fileclose(filehandle);
if MessageDlg(str_path+'已经存在,是否覆盖?',
mtWarning, [mbYes, mbNo], 0) = 6 then
begin
//取数据
showdata(str_begin_date,str_end_date,str_path);
//
Grid_ship.SaveToXLS(p_path,true);
showmessage('保存成功!');
end
else
showmessage('文件未保存!');
end
else//如果文件被打开,提示出错
begin
showmessage(str_path+'已经被打开,请改名或关闭!
文件未保存!')
end
end
else //文件不存在,可以保存
begin
//去班轮数据
showdata(str_begin_date,str_end_date,str_path);
//
Grid_ship.SaveToXLS(p_path,true);
showmessage('保存成功!')
end;
close;
finally
Free;
couninitialize;
end;
end;
SaveDataToExcel:=1;
end ;
exports
SaveDataToExcel;
begin
end.
//以下是form_data中代码
unit Unit_data;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, ADODB, dxGrClms, dxTL, dxDBGrid, dxCntner, ExtCtrls, DBCtrls,
StdCtrls, Grids, DBGrids, dxDBTLCl, dxDBCtrl;
type
TForm_Data = class(TForm)
Grid_ship: TdxDBGrid;
Grid_shipETA_TIM: TdxDBGridDateColumn;
Grid_shipETD_TIM: TdxDBGridDateColumn;
Grid_shipSTOP_BILL_DTE: TdxDBGridDateColumn;
Grid_shipHANG_PORT_STR: TdxDBGridMaskColumn;
Grid_shipBEGIN_TIM: TdxDBGridDateColumn;
Grid_shipEND_TIM: TdxDBGridDateColumn;
Grid_shipIN_TEU_NUM: TdxDBGridMaskColumn;
Grid_shipLINE_NAM: TdxDBGridMaskColumn;
Grid_shipE_SHIP_NAM: TdxDBGridMaskColumn;
Grid_shipC_SHIP_NAM: TdxDBGridMaskColumn;
Grid_shipOUT_VOYAGE_NO: TdxDBGridMaskColumn;
Grid_shipIN_VOYAGE_NO: TdxDBGridMaskColumn;
Grid_shipctrustname: TdxDBGridColumn;
Grid_shipkaihang: TdxDBGridColumn;
Grid_shipWorkPlace: TdxDBGridColumn;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form_Data : TForm_Data;
ado_connect : Tadoconnection;
ado_query : Tadoquery;
ado_datasur : Tdatasource;
procedure showdata(str_begin_date:string;str_end_date:string;str_path
:string);
implementation
{$R *.DFM}
procedure showdata(str_begin_date:string;str_end_date:string;str_path:string);
begin
ado_query.Close;
ado_query.SQL.Clear;
ado_query.SQL.Append('SELECT "REGULAR_SHIP"."TRUST_COD", ');
ado_query.SQL.Append('"REGULAR_SHIP"."E_TRUST_NAM", ');
ado_query.SQL.Append('"REGULAR_SHIP"."ETA_TIM", ');
ado_query.SQL.Append('"REGULAR_SHIP"."ETD_TIM", ');
ado_query.SQL.Append('"REGULAR_SHIP"."REGULAR_DTE", ');
ado_query.SQL.Append('"REGULAR_SHIP"."STOP_BILL_DTE", ');
ado_query.SQL.Append('"REGULAR_SHIP"."HANG_PORT_STR", ');
ado_query.SQL.Append('"REGULAR_SHIP"."LOAD_PLACE_STR", ');
ado_query.SQL.Append('"REGULAR_SHIP"."BEGIN_TIM", ');
ado_query.SQL.Append('"REGULAR_SHIP"."END_TIM", ');
ado_query.SQL.Append('"REGULAR_SHIP"."IN_TEU_NUM", ');
ado_query.SQL.Append('"REGULAR_SHIP"."OUT_TEU_NUM", ');
ado_query.SQL.Append('"REGULAR_SHIP"."AGENCY_ID", ');
ado_query.SQL.Append('"REGULAR_SHIP"."TO_SHIP_ID", ');
ado_query.SQL.Append('"REGULAR_SHIP"."LINE_COD", ');
ado_query.SQL.Append('"REGULAR_SHIP"."LINE_NAM", ');
ado_query.SQL.Append('"REGULAR_SHIP"."RELET_MAN", ');
ado_query.SQL.Append('"REGULAR_SHIP"."PHONE_NO", ');
ado_query.SQL.Append('"SYS_PARAM"."E_COMPANY_NAM", ');
ado_query.SQL.Append('"SYS_PARAM"."C_COMPANY_NAM", ');
ado_query.SQL.Append('"WORK_PLACE_CODE"."C_WORK_PLACE_STR", ');
ado_query.SQL.Append('"SYS_PARAM"."ADD_STR", ');
ado_query.SQL.Append('"SYS_PARAM"."TEL_STR", ');
ado_query.SQL.Append('"SYS_PARAM"."FAX_STR", ');
ado_query.SQL.Append('"SYS_PARAM"."TLX_STR", ');
ado_query.SQL.Append('"REGULAR_SHIP"."E_SHIP_NAM", ');
ado_query.SQL.Append('"REGULAR_SHIP"."C_SHIP_NAM", ');
ado_query.SQL.Append('"REGULAR_SHIP"."OUT_VOYAGE_NO", ');
ado_query.SQL.Append('"REGULAR_SHIP"."IN_VOYAGE_NO", ');
ado_query.SQL.Append('"REGULAR_SHIP"."C_TRUST_NAM", ');
ado_query.SQL.Append('"TRUST_RECORD"."C_TRUST_NAM" ');
ado_query.SQL.Append('FROM "REGULAR_SHIP","SYS_PARAM", ');
ado_query.SQL.Append('"WORK_PLACE_CODE","TRUST_RECORD" ');
ado_query.SQL.Append('WHERE regular_ship.work_place_cod = work_place_code.work_place_cod (+) and ');
ado_query.SQL.Append('regular_ship.trust_cod = trust_record.trust_cod (+) and ');
ado_query.SQL.Append(' "REGULAR_SHIP"."ETA_TIM" >= TO_DATE('''+str_begin_date+''',''YYYY-MM-DD'') AND');
ado_query.SQL.Append(' "REGULAR_SHIP"."ETA_TIM" < TO_DATE('''+str_end_date+''',''YYYY-MM-DD'' ) ');
ado_query.ExecSQL;
ado_query.Active:=true;
end;
procedure TForm_Data.FormCreate(Sender: TObject);
begin
//create connection
ado_connect:=Tadoconnection.Create(parent);
ado_connect.ConnectionString:='Provider=MSDAORA.1;Password=ok;User ID=samis_hh;Data Source=samis_hh;Persist Security Info=True';
ado_connect.LoginPrompt:=false;
ado_connect.connected :=false;
//create adoquery
ado_query:=Tadoquery.Create(parent);
ado_query.Connection :=ado_connect;
//create datasource
ado_datasur :=Tdatasource.create(parent);
ado_datasur.DataSet:=ado_query;
//
Grid_ship.DataSource :=ado_datasur;
end;
end.