| |
将Excel文件转入数据库
Delphi 中将 Excel 数据转入其他数据库:
将Excel的数据转入其他数据库的关键在于用数据库的方法打开Excel表。从Excel表的一般布局来看,分为简单表和复杂表。我们对简单表的定义是表的第一排数据是字段名,如果表的第一排字段名,则称为复杂表。
对于简单表,可以直接用数据库的方式打开,但对于复杂表,首先要对Excel表的数据区进行定义,可以用定义打印区域的方法,也可以用定义或指定的方法。用定义的方法比较方便,可以为Excel每个表的数据区定义并命名,这个命名就是将来用数据库方式打开Excel表的表名。对于简单表,Excel每个表的名称就是表名。有一点需要注意,在用数据库方式打开Excel表时会自动为每个表名加上”$”,如Excel的某个表名为Sheet1,那么他在数据库中的表名是”Sheet1$”。可以用BDE来操作,也可以用ADO。使用ADO时还有一点要注意,Excel的表名要加方括号[Sheet1$],例如在ADODataSet中需要这样来写:
ADODataSet.CommandText := ‘SELECT * FROM [Sheet1$]’;
下面给出的例子使用的是BDE:
假设有一Excel文件中有一个表Sheet1,表中的第一排为:
ID NAME PRICE 1 AAA 1.1 2 BBB 1.2 3 CCC 1.3 4 DDD 1.4
假设有一个Access 数据库 DB1.MDB 中有一表 Test 有如下字段: ID NAME PRICE
程序如下:
MexlToA.DMF object Form1: TForm1 Left = 272 Top = 155 Width = 559 Height = 268 Caption = ’Form1’ Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = ’MS Sans Serif’ Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object ButtonCreateODBC: TButton Left = 64 Top = 192 Width = 75 Height = 25 Caption = ’CreateODBC’ TabOrder = 0 OnClick = ButtonCreateODBCClick end object ButtonBatchMove: TButton Left = 192 Top = 192 Width = 121 Height = 25 Caption = ’MoveExcelToAccess’ TabOrder = 1 OnClick = ButtonBatchMoveClick end object DBGrid1: TDBGrid Left = 0 Top = 0 Width = 551 Height = 177 Align = alTop DataSource = DataSource1 TabOrder = 2 TitleFont.Charset = DEFAULT_CHARSET TitleFont.Color = clWindowText TitleFont.Height = -11 TitleFont.Name = ’MS Sans Serif’ TitleFont.Style = [] Columns = < item Expanded = False FieldName = ’ID’ Visible = True end item Expanded = False FieldName = ’NAME’ Width = 349 Visible = True end item Expanded = False FieldName = ’PRICE’ Width = 76 Visible = True end> end object OpenDialog1: TOpenDialog Filter = ’Excel(*.xls]|*.xls’ Left = 88 Top = 48 end object DataSource1: TDataSource DataSet = Table1 Left = 224 Top = 48 end object BatchMove1: TBatchMove Left = 224 Top = 88 end object Table1: TTable Left = 184 Top = 48 end object Table2: TTable TableName = ’Test’ Left = 184 Top = 88 end object Database1: TDatabase ReadOnly = True SessionName = ’Default’ TransIsolation = tiDirtyRead Left = 144 Top = 48 end end
MexlToA.PAS
private { Private declarations } public { Public declarations } end;
var Form1: TForm1;
implementation
{$R *.DFM} //工作条件: //假设有一个Excel表,表名是【Sheet1】,表中有三个字段,ID,NAME,PRICE
procedure TForm1.ButtonCreateODBCClick(Sender: TObject); var FDN, FDP, FN: string; Reg: TRegistry; begin OpenDialog1.Filter := ’Excel(*.xls)|*.xls’; if OpenDialog1.Execute then begin FDN := OpenDialog1.FileName; FN := Copy(FDN, LastDelimiter(’\’, FDN) + 1, Length(FDN) - LastDelimiter(’\’, FDN)); FN := Copy(FN, 1, Length(FN) - 4); FDP := Copy(FDN, 1, LastDelimiter(’\’, FDN) - 1); Reg := TRegistry.Create; with Reg do begin RootKey := HKEY_LOCAL_MACHINE; if OpenKey(’Software\ODBC\ODBC.INI\ODBC Data Sources’, True) then WriteString(FN, ’Microsoft Excel-Treiber (*.xls)’) else begin Application.MessageBox(’ODBC 初始化错误’, pChar(’Information’), mb_ok + MB_ICONEXCLAMATION); exit; end; CloseKey; if OpenKey(’Software\ODBC\ODBC.INI\’ + FN, True) then begin WriteString(’Driver’, ’C:\WINNT\System32\odbcjt32.dll’); WriteString(’DBQ’, FDN); WriteString(’DefaultDir’, FDP); WriteInteger(’DriverId’, 790); WriteString(’FIL’, ’excel 8.0’); WriteInteger(’ReadOnly’, 1); WriteInteger(’SafeTransactions’, 0); WriteString(’UID’, ’’); end else begin Application.MessageBox(’ODBC 初始化错误’, pChar(’Error’), mb_ok + MB_ICONEXCLAMATION); exit; end; CloseKey; if OpenKey(’Software\ODBC\ODBC.INI\’ + FN + ’\Engines\Excel’, True) then begin WriteString(’ImplicitCommitSync’, ’’); WriteInteger(’MaxBufferSize’, 8); WriteInteger(’Threads’, 3); WriteString(’UserCommitSync’, ’Yes’); WriteInteger(’FirstRowHasNames’, 1); Application.MessageBox(’用户信息注册成功’, pChar(’SUCCEED’), mb_ok + mb_iconinformation); end else begin Application.MessageBox(’ODBC 初始化错误’, pChar(’Error’), mb_ok + MB_ICONEXCLAMATION); exit; end; CloseKey; Free; end; //在生成ODBC时,对Excel表的命名会自动加上’$’ //这里假定你知道Table的命名是Sheet1。 //当然可以使用别的方法动态获得TableName,这里我就不多说了。 with Database1 do begin AliasName := FN; LoginPrompt := False; ReadOnly := True; DatabaseName := ’EXCEL’; Connected := True; end; with Table1 do begin DatabaseName := ’EXCEL’; TableName := ’Sheet1$’; Active := True; end; DataSource1.DataSet := Table1; //假设你知道Excel表的每个字段。 with DBGrid1 do begin DataSource := DataSource1; Columns[0].FieldName := ’ID’; Columns[0].Width := 60; Columns[1].FieldName := ’Name’; Columns[1].Width := 200; Columns[2].FieldName := ’Price’; Columns[2].Width := 60; end; end; OpenDialog1.Filter := ’Access DB(*.mdb)|*.mdb’; if OpenDialog1.Execute then begin FDN := OpenDialog1.FileName; FN := Copy(FDN, LastDelimiter(’\’, FDN) + 1, Length(FDN) - LastDelimiter(’\’, FDN)); FN := Copy(FN, 1, Length(FN) - 4); FDP := Copy(FDN, 1, LastDelimiter(’\’, FDN) - 1); Reg := TRegistry.Create; with Reg do begin RootKey := HKEY_LOCAL_MACHINE; if OpenKey(’Software\ODBC\ODBC.INI\ODBC Data Sources’, True) then WriteString(FN, ’Microsoft Access-Treiber (*.mdb)’) else begin Application.MessageBox(’ODBC 初始化错误’, pChar(’Information’), mb_ok + MB_ICONEXCLAMATION); exit; end; CloseKey; if OpenKey(’Software\ODBC\ODBC.INI\’ + FN, True) then begin WriteString(’Driver’, ’C:\WINNT\System32\odbcjt32.dll’); WriteString(’DBQ’, FDN); WriteString(’DefaultDir’, FDP); WriteInteger(’DriverId’, 25); WriteString(’FIL’, ’MS Access;’); WriteInteger(’ReadOnly’, 0); WriteInteger(’SafeTransactions’, 0); end else begin Application.MessageBox(’ODBC 初始化错误’, pChar(’Error’), mb_ok + MB_ICONEXCLAMATION); exit; end; CloseKey; if OpenKey(’Software\ODBC\ODBC.INI\’ + FN + ’\Engines\Jet’, True) then begin WriteString(’ImplicitCommitSync’, ’’); WriteInteger(’MaxBufferSize’, 2048); WriteInteger(’PageTimeout’, 5); WriteInteger(’Threads’, 3); WriteString(’UserCommitSync’, ’Yes’); Application.MessageBox(’用户信息注册成功’, pChar(’SUCCEED’), mb_ok + mb_iconinformation); end else begin Application.MessageBox(’ODBC 初始化错误’, pChar(’Error’), mb_ok + MB_ICONEXCLAMATION); exit; end; CloseKey; Free; end; with Table2 do begin DatabaseName := FN; TableName := ’Test’; Active := True; end; end; end;
procedure TForm1.ButtonBatchMoveClick(Sender: TObject); begin with BatchMove1 do begin Source := Table1; Destination := Table2; Mode := batAppend; Execute; end; end;
//使用ADO的例子: { with ADODataSet1 do begin ConnectionString := ’Provider=MSDASQL.1;’ + ’Persist Security Info=False;’ + ’Data Source=’ + FN; CommandType := cmdText; CommandText := ’SELECT * FROM [Sheet1$]’; Active := True; end;}
end.
| 出处: Delphi程序员之家 日期: 2004-10-18 |
好: 一般: 差: |
|
|