EXCEL表导入到数据库,数据库导入文本,做成DLL。200分(200分)

  • 主题发起人 主题发起人 jhlt1314
  • 开始时间 开始时间
J

jhlt1314

Unregistered / Unconfirmed
GUEST, unregistred user!
关键词:DELPHI 7 ADO SQL  EXCEL TXT
  将EXCEL表导入到数据库(SQL SERVER 2000)中,然后从数据库中导出成TXT文件。TXT文件中字段与字段之间用TAB间隔开。
将该功能写成独立的DLL。调用DLL弹出一个界面:包括有调出选择EXCEL和保存TXT的路径,按“确定”。
其中EXCEL表中的数据有空值的,有用逗号分开的数值,有时间类型的数据。各位帮帮忙,尽可能要完整的源码,和必要的操作步骤。本人初学Delphi,问题问的太简单不要见笑!
 
请提出具体的数据表的字段,是否应包含excel字段和数据表的字段对应啊
 
数据表的字段和excel字段对应。如:
SQL表 :ID Name Addr Num ......
001 张三 广州市 98,100,000 ......
excel表 :ID Name Addr Num ......(excel表第一行)
002 李四 深圳市 123,100,000 ......(excel表第二行)
003 王五 江门市 88,100,000 ......(excel表第三行)
... ... ... ...
 
没人会吗?各位大虾帮帮忙!
 
自己先学着做,有问题查资料和请教!
 
有专用的xls格式控件 但是不如自动化对象兼容性好(要求机器上必须安装有EXCEL)
 
实现其实不难,就是费点儿时间。
你的问题可以拆成3部分:
1.在DLL中弹出窗体实现数据库操作
前面有很多这样的帖子了,你可以翻翻看。
2.EXCEL导入到数据库
下面是从一个ExcelFile里攫取字段信息到Lst的过程,仅作参考。
攫取记录信息的方法与此相同。
代码:
  procedure GetFieldsFromXls(ExcelFile: string
var Lst: TStrings);
  var
    MsExcel, MsExcelWorkBook, MsExcelWorkSheet: Variant;
    i: Integer;
  begin
    MsExcel := CreateOleObject('Excel.Application');
    try
      MsExcelWorkBook := msExcel.Workbooks.Open(ExcelFile);
      MsExcelWorkSheet := msExcel.Worksheets.Item[1];
      with Lst do
      begin
        Clear;
        for i := 1 to MsExcelWorkSheet.UsedRange.Columns.Count do
          Add(MsExcel.Cells[1, i].Value);
      end;
    finally
      MsExcel.Quit;
    end;
  end;
3.文本导入到数据库
这个应该没有技术难度吧,你自己根据分隔符获取字段信息就是了。
 
先看看这里
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1691966
 
下面的代码是Excel导入SQL Server,请问各位大虾,Excel文件要是很小,测试时几行几列就可以导入。要是选择一个大一点的Excel文件(70列,2000行)时会报错:list index out of bounds(67)应该怎么改呢?帮帮忙!


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB;

type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
ExcelADOConn: TADOConnection;
ExcelADOQry: TADOQuery;
SQLADOConn:TADOConnection;
SQLADOQry: TADOQuery;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
SQLstr, ExcelFileName: string;
num: integer;
begin
try
ExcelADOConn:= TADOConnection.Create(self);
ExcelADOQry:= TADOQuery.Create(self);
ExcelADOQry.Connection:= ExcelADOConn;

SQLADOConn:=TADOConnection.Create(self);
if SQLADOConn.Connected then
SQLADOConn.Connected := False;
SQLADOConn.ConnectionString :='Provider=SQLOLEDB.1;Persist SecurityInfo=False;User ID=sa;Initial Catalog=kk;Data Source=YE';
SQLADOConn.LoginPrompt:=False;

SQLADOQry:= TADOQuery.Create(self);
SQLADOQry.Connection:= SQLADOConn;

if OpenDialog1.Execute then
begin
ExcelFileName:= OpenDialog1.FileName;
if ExcelADOConn.Connected then
ExcelADOConn.Connected := False;
ExcelADOConn.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;'
+ 'Data Source=' + ExcelFileName + ';Mode=Share Deny None;Extended Properties=excel 8.0;';
ExcelADOConn.Connected := True;
ExcelADOQry.Close;
ExcelADOQry.SQL.Clear;
SQLstr :='select * from [sheet1$]';
ExcelADOQry.SQL.Add(SQLstr);
ExcelADOQry.Open;

ExcelADOQry.First;
while (not ExcelADOQry.eof) do
begin
SQLADOQry.Close;
SQLADOQry.SQL.Clear;
SQLADOQry.SQL.Add('select * from test');
SQLADOQry.open;
SQLADOQry.append;
for num := 0 to ExcelADOQry.FieldCount - 1 do
begin
SQLADOQry.Fields[num].Value := ExcelADOQry.Fields[num].Value;
end;
SQLADOQry.Post;
ExcelADOQry.Next;
end;
end;
finally
SQLADOConn.Free;
SQLADOQry.Free;
ExcelADOConn.Free;
ExcelADOQry.Free;
end;
end;
end.
 
/** 导入文本文件
EXEC master..xp_cmdshell ’bcp "dbname..tablename" in c:/DT.txt -c -Sservername -Usa -Ppassword’

改为如下,不需引号
EXEC master..xp_cmdshell ’bcp dbname..tablename in c:/DT.txt -c -Sservername -Usa -Ppassword’

/** 导出文本文件
EXEC master..xp_cmdshell ’bcp "dbname..tablename" out c:/DT.txt -c -Sservername -Usa -Ppassword’
此句需加引号


上面的导出文本文件的语句在Delphi程序中是在哪执行的?ADOConnection属性吗?在哪个控件中插入的语句呢,急!帮帮忙!(SQL->TXT)
 
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.
 
谢谢名位![:)]
 
后退
顶部