

Dev Express 中的 dxDBGrid/cxGrid 均提供了将表格中 数据 导出 到 M$ Excel 等中的方法,但大多时候,却需将 数据 导出 至 M$ Access 中... 于是便有了本文。 uses ComObj, Gauges, ShellAPI; const ExportTabName_MDB = '营销 数据 '; MDBStr = 'Provider=
 	 Dev Express 中的 dxDBGrid/cxGrid 均提供了将表格中数据导出到 M$ Excel 等中的方法,但大多时候,却需将数据导出至 M$ Access 中...   uses   const   var   //(示例)导出列列表,注意 格式   try   procedure ExportToMDB(ExportMDBName: string; ExportColumnLst);   vMDB:= CreateOleObject('ADOX.Catalog');   Result:= True;   function CreateTab(MDBAndTabName: string; ExportColumnLst: TStringList;   SQLTxt:= '';   if SQLTxt = '' then   MDBName:= Copy(MDBAndTabName, 1, Pos(';', MDBAndTabName) - 1);   with aqy_ExecSQL do   ConnectionString:=   SQL.Text:=   try   Result:= True;   ExportColumn:= '';   if ExportColumn = '' then   SQLTxt:=   try   //pnl_ExportFile: TPanel;   CurrRec:= 0;   if CurrRec mod 20 = 0 then   Application.ProcessMessages;   with aqy_ExecSQL do   SQL.Text:=   for i:= 0 to ExportParamLst.Count - 1 do   try   MessageBox(   aqy_ExecSQL.Close;   Next;   Close; //aqy_ExportData   if MessageBox( 
 于是便有了本文。 
 ComObj, Gauges, ShellAPI; 
 ExportTabName_MDB = '营销数据'; 
 MDBStr = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s'; 
 ExportName: string; 
 ExportColumnLst: TStringList; //列名;列类型(长度) 
 begin 
 ExportName:= '导出结果.MDB'; //use a SaveDialog to select the save name here 
 ExportColumnLst:= TStringList.Create; 
 ExportColumnLst.Add('Contact;联系人 varchar(30)'); 
 ExportColumnLst.Add('Gender;性别 varchar(2)'); 
 ExportColumnLst.Add('Address;地址 varchar(100)'); 
 ExportColumnLst.Add('PostCode;邮编 varchar(6)'); 
 ExportToMDB(ExportName, ExportColumnLst); 
 finally 
 FreeAndNil(ExportColumnLst); 
 end; 
 end; 
 function CreateMDB(MDBFileName: string): Boolean; 
 var 
 vMDB: Variant; 
 begin 
 Result:= False; 
 vMDB.Create(Format(MDBStr, [MDBFileName])); 
 vMDB:= UnAssigned; 
 end; 
 aqy_ExecSQL: TADOQuery): Boolean; 
 var 
 i: Integer; 
 StrTmp: string; 
 SQLTxt: string; 
 MDBName: string; 
 TabName: string; 
 begin 
 Result:= False; 
 for i:= 0 to ExportColumnLst.Count - 1 do 
 begin 
 StrTmp:= ExportColumnLst.Strings
 SQLTxt:= Copy(StrTmp, Pos(';', StrTmp) + 1, Length(StrTmp)); 
 else 
 SQLTxt:= SQLTxt + ',' + 
 Copy(StrTmp, Pos(';', StrTmp) + 1, Length(StrTmp)); 
 end; 
 TabName:= Copy( 
 MDBAndTabName, 
 Pos(';', MDBAndTabName) + 1, 
 Length(MDBAndTabName) 
 ); 
 try 
 Close; 
 'Provider=MSDataShape.1;Data Provider=Microsoft.Jet.OLEDB.4.0;' + 
 'Data Source=' + MDBName + ';Persist Security Info=false'; 
 'create table ' + TabName + 
 '(' + 
 SQLTxt + 
 ')'; 
 ExecSQL; 
 Close; 
 except 
 on E: Exception do 
 begin 
 MessageBox( 
 Handle, 
 PChar('创建表失败!' + #13 + '失败原因:' + E.Message), 
 '错误', 
 MB_OK + MB_ICONERROR 
 ); 
 Close; 
 Exit; 
 end; 
 end; 
 finally 
 //Free; 
 end; 
 end; 
 var 
 aqy_ExecSQL: TADOQuery; 
 SQLTxt: string; 
 i: Integer; 
 StrTmp: string; 
 ExportColumn: string; 
 ExportColumnParam: string; 
 ExportParamLst: TStringList; 
 GgTip: TGauge; 
 CurrRec: Integer; 
 begin 
 if CreateMDB(ExportMDBName) then 
 begin 
 aqy_ExecSQL:= TADOQuery.Create(Self); 
 try 
 if CreateTab( 
 ExportMDBName + ';' + ExportTabName_MDB, 
 ExportColumnLst, 
 aqy_ExecSQL 
 ) then 
 begin 
 Screen.Cursor:= crHourGlass; 
 ExportColumnParam:= ''; 
 ExportParamLst:= TStringList.Create; 
 for i:= 0 to ExportColumnLst.Count - 1 do 
 begin 
 StrTmp:= ExportColumnLst.Strings
 begin 
 ExportColumn:= Copy(StrTmp, 1, Pos(';', StrTmp) - 1); 
 ExportColumnParam:= ':' + ExportColumn; 
 ExportParamLst.Add(ExportColumn); 
 end 
 else 
 begin 
 ExportColumn:= ExportColumn + ',' + 
 Copy(StrTmp, 1, Pos(';', StrTmp) - 1); 
 ExportColumnParam:= ExportColumnParam + ',:' + 
 Copy(StrTmp, 1, Pos(';', StrTmp) - 1); 
 ExportParamLst.Add(Copy(StrTmp, 1, Pos(';', StrTmp) - 1)); 
 end; 
 end; 
 'select ' + ExportColumn + ' from TabName where ID=' + 
 aqy_Tmp1.FieldByName('ID').AsString; 
 with aqy_ExportData do //aqy_ExportData: TADOQuery; 
 begin 
 Close; 
 SQL.Text:= SQLTxt; 
 Open; 
 GgTip:= TGauge.Create(pnl_ExportFile); //Gauge 进度提示 
 with GgTip do 
 begin 
 Parent:= pnl_ExportFile; 
 Left:= 0; 
 Height:= 21; 
 Width:= pnl_ExportFile.Width; 
 ForeColor:= clFuchsia; 
 MinValue:= 0; 
 MaxValue:= RecordCount; 
 Visible:= True; 
 Update; 
 end; 
 while not Eof do 
 begin 
 Inc(CurrRec); 
 begin 
 GgTip.Progress:= CurrRec; 
 Update; 
 end; 
 begin 
 Close; 
 'Insert Into ' + ExportTabName_MDB + 
 ' Values(' + ExportColumnParam + ')'; 
 Parameters.ParamByName(ExportParamLst.Strings
 aqy_ExportData.FieldByName( 
 ExportParamLst.Strings 
 ).AsString; 
 ExecSQL; 
 except 
 on E: Exception do 
 begin 
 Close; 
 GgTip.Visible:= False; 
 Update; 
 Handle, 
 PChar('导出文件失败! ' + #13 + '失败原因:' + 
 E.Message + ' ' 
 ), 
 '错误', 
 MB_OK + MB_ICONERROR 
 ); 
 Exit; 
 end; 
 end; 
 end; //End with 
 end; //End while 
 GgTip.Visible:= False; 
 Handle, 
 PChar('导出文件成功! ' + #13 + 
 '现在查看导出结果(' + ExportMDBName + '吗?' 
 ), 
 '提示', 
 MB_YESNO + MB_ICONINFORMATION 
 ) = IDYES then