library Excel_SQL_Txt;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Classes,
Dialogs,
StdCtrls,
ADODB;
{$R *.res}
//找出数据库中的表
function FindTable(var TmpAdoCn: TADOConnection;TableName:string) : Boolean;
var
I : Integer
List : TStringList;
begin
Result:=False;
List := TStringList.Create;
TmpAdoCn.GetTableNames(List,True);
for I := 0 to List.Count -1 do
if UpperCase(List.Strings) = UpperCase(TableName) then
Result := True;
List.Free
end;
//Excel表导入SQL表
procedure ExcelToSQL(const SQLTableName :string);stdcall;
var
SQLStr,ExcelFileName:string;
StarTime:TdateTime;
SQLADOConn:TADOConnection;
ExcelOpenDlg:TOpenDialog;
begin
try
//连接SQL数据库
SQLADOConn:=TADOConnection.Create(nil);
if SQLADOConn.Connected then
SQLADOConn.Connected := False;
SQLADOConn.ConnectionString :='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=cdj;Data Source=YE';
StarTime:=now;
//找出SQL数据库中存在的表并删除该表
if FindTable(SQLADOConn,SQLTableName)=true then
begin
SQLStr:='drop TABLE '+SQLTableName;
SQLADOConn.Execute(SQLStr);
showmessage('kkk');
end;
ExcelOpenDlg:= TOpenDialog.Create(nil);
ExcelOpenDlg.Filter:='Excel files(*.xls)|*.xls';
if ExcelOpenDlg.Execute then
ExcelFileName:= ExcelOpenDlg.FileName;
if not FileExists(ExcelFileName) then
exit;
SQLStr:= 'SELECT * into '+ SQLTableName + ' FROM OpenDataSource('''+'Microsoft.Jet.OLEDB.4.0'+''','''+'Data Source=%s
User ID=Admin;Password=;Extended properties=Excel 8.0'''+')...[Sheet1$]';
Showmessage(SQLStr);
SQLADOConn.Execute(format(SQLStr,[ExcelFileName]));
Showmessage(formatDateTime('hh:nn:ss zzz',Now-StarTime));
finally
SQLADOConn.Free;
ExcelOpenDlg.Free;
end;
end;
//SQL表导出到TXT
procedure SQLToTxt();stdcall;
var
SQLStr,TxtFileName:string;
SQLADOConn:TADOConnection;
TxtSaveDlg:TSaveDialog;
StarTime:TdateTime;
begin
try
//连接SQL数据库
SQLADOConn:=TADOConnection.Create(nil);
if SQLADOConn.Connected then
SQLADOConn.Connected := False;
SQLADOConn.ConnectionString :='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=cdj;Data Source=YE';
Showmessage('连接成功');
TxtSaveDlg:= TSaveDialog.Create(nil);
TxtSaveDlg.Filter:='Text files(*.txt)|*.txt';
if TxtSaveDlg.Execute then
TxtFileName:= TxtSaveDlg.FileName;
if not FileExists(TxtFileName) then
exit;
StarTime:=now;
SQLStr:='EXEC master..xp_cmdshell '+#39+'bcp "Select * from cdj..test" queryout %s -c -t/t -Sye -Usa -P'+#39 ;
Showmessage(SQLStr);
SQLADOConn.Execute(format(SQLStr,[TxtFileName]));
Showmessage(formatDateTime('hh:nn:ss zzz',Now-StarTime));
finally
SQLADOConn.Free;
TxtSaveDlg.Free;
end;
end;
//导出表
exports
ExcelToSQL,SQLToTxt;
begin
end.