0

0

将数据导出至 M$ Access

php中文网

php中文网

发布时间:2016-06-07 15:32:26

|

1305人浏览过

|

来源于php中文网

原创

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
      ComObj, Gauges, ShellAPI;

    const
      ExportTabName_MDB = '营销数据';
      MDBStr = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s';

    var
      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)');

      try
        ExportToMDB(ExportName, ExportColumnLst);
      finally
        FreeAndNil(ExportColumnLst);
      end;
    end;

    procedure ExportToMDB(ExportMDBName: string; ExportColumnLst);
      function CreateMDB(MDBFileName: string): Boolean;
      var
        vMDB: Variant;
      begin
        Result:= False;

        vMDB:= CreateOleObject('ADOX.Catalog');
        vMDB.Create(Format(MDBStr, [MDBFileName]));
        vMDB:= UnAssigned;

        Result:= True;
      end;

      function CreateTab(MDBAndTabName: string; ExportColumnLst: TStringList;
        aqy_ExecSQL: TADOQuery): Boolean;
      var
        i: Integer;
        StrTmp: string;
        SQLTxt: string;
        MDBName: string;
        TabName: string;
      begin
        Result:= False;

        SQLTxt:= '';
        for i:= 0 to ExportColumnLst.Count - 1 do
        begin
          StrTmp:= ExportColumnLst.Strings;

          if SQLTxt = '' then
            SQLTxt:= Copy(StrTmp, Pos(';', StrTmp) + 1, Length(StrTmp));
          else
            SQLTxt:= SQLTxt + ',' +
                       Copy(StrTmp, Pos(';', StrTmp) + 1, Length(StrTmp));
        end;

        MDBName:= Copy(MDBAndTabName, 1, Pos(';', MDBAndTabName) - 1);
        TabName:= Copy(
                       MDBAndTabName,
                       Pos(';', MDBAndTabName) + 1,
                       Length(MDBAndTabName)
                      );

        with aqy_ExecSQL do
        try
          Close;

          ConnectionString:=
            'Provider=MSDataShape.1;Data Provider=Microsoft.Jet.OLEDB.4.0;' +
            'Data Source=' + MDBName + ';Persist Security Info=false';

          SQL.Text:=
            'create table ' + TabName +
            '(' +
              SQLTxt +
            ')';

          try
            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;

        Result:= True;
      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;

            ExportColumn:= '';
            ExportColumnParam:= '';
            ExportParamLst:= TStringList.Create;
            for i:= 0 to ExportColumnLst.Count - 1 do
            begin
              StrTmp:= ExportColumnLst.Strings;

              if ExportColumn = '' then
              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;

            SQLTxt:=
              'select ' + ExportColumn + ' from TabName where ID=' +
              aqy_Tmp1.FieldByName('ID').AsString;  

            try
              with aqy_ExportData do //aqy_ExportData: TADOQuery;
              begin
                Close;
                SQL.Text:= SQLTxt;
                Open;

                //pnl_ExportFile: TPanel;
                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;

                CurrRec:= 0;
                while not Eof do
                begin
                  Inc(CurrRec);

                  if CurrRec mod 20 = 0 then
                  begin
                    GgTip.Progress:= CurrRec;
                    Update;

                    Application.ProcessMessages;
                  end;

                  with aqy_ExecSQL do
                  begin
                    Close;

                    SQL.Text:=
                      'Insert Into ' + ExportTabName_MDB +
                      ' Values(' + ExportColumnParam + ')';

                    for i:= 0 to ExportParamLst.Count - 1 do
                      Parameters.ParamByName(ExportParamLst.Strings).Value:=
                       aqy_ExportData.FieldByName(
                                                  ExportParamLst.Strings
                                                 ).AsString;

                    try
                      ExecSQL;                  
                    except
                      on E: Exception do
                      begin
                        Close;
                        GgTip.Visible:= False;
                        Update;

                        MessageBox(
                                   Handle,
                                   PChar('导出文件失败! ' + #13 + '失败原因:' +
                                         E.Message + ' '
                                        ),
                                   '错误',
                                   MB_OK + MB_ICONERROR
                                  );
                        Exit;
                      end;
                    end;
                  end; //End with

                  aqy_ExecSQL.Close;

                  Next;
                end; //End while

                Close; //aqy_ExportData
                GgTip.Visible:= False;

                if MessageBox(
                              Handle,
                              PChar('导出文件成功! ' + #13 +
                                    '现在查看导出结果(' + ExportMDBName + '吗?'
                                   ),
                              '提示',
                               MB_YESNO + MB_ICONINFORMATION
                             ) = IDYES then
                begin
                  ShellExecute(0, 'Open', PChar(ExportMDBName), nil, nil, SW_SHOW);
                end;
              end;
            except
              on E: Exception do
              begin
                pnl_ExportFile.Caption:= '';
                GgTip.Visible:= False;
                Update;

                MessageBox(
                           Handle,
                           PChar('导出文件过程中发生错误! ' + #13 +
                                 '错误描述:' + E.Message + ' '
                                ),
                           '导出失败',
                           MB_OK + MB_ICONERROR
                          );
              end;
            end;
          end;
        finally
          FreeAndNil(aqy_ExecSQL);
          FreeAndNil(ExportParamLst);
          FreeAndNil(GgTip);

          Screen.Cursor:= crDefault;
        end;
      end;
    end;

    OK,Done!

adelphicoder

AOXO_CMS建站系统企业通用版1.0
AOXO_CMS建站系统企业通用版1.0

一个功能强大、性能卓越的企业建站系统。使用静态网页技术大大减轻了服务器负担、加快网页的显示速度、提高搜索引擎推广效果。本系统的特点自定义模块多样化、速度快、占用服务器资源小、扩展性强,能方便快捷地建立您的企业展示平台。简便高效的管理操作从用户使用的角度考虑,对功能的操作方便性进行了设计改造。使用户管理的工作量减小。网站互动数据可导出Word文档,邮件同步发送功能可将互动信息推送到指定邮箱,加快企业

下载

相关专题

更多
虚拟号码教程汇总
虚拟号码教程汇总

本专题整合了虚拟号码接收验证码相关教程,阅读下面的文章了解更多详细操作。

29

2025.12.25

错误代码dns_probe_possible
错误代码dns_probe_possible

本专题整合了电脑无法打开网页显示错误代码dns_probe_possible解决方法,阅读专题下面的文章了解更多处理方案。

20

2025.12.25

网页undefined啥意思
网页undefined啥意思

本专题整合了undefined相关内容,阅读下面的文章了解更多详细内容。后续继续更新。

37

2025.12.25

word转换成ppt教程大全
word转换成ppt教程大全

本专题整合了word转换成ppt教程,阅读专题下面的文章了解更多详细操作。

6

2025.12.25

msvcp140.dll丢失相关教程
msvcp140.dll丢失相关教程

本专题整合了msvcp140.dll丢失相关解决方法,阅读专题下面的文章了解更多详细操作。

2

2025.12.25

笔记本电脑卡反应很慢处理方法汇总
笔记本电脑卡反应很慢处理方法汇总

本专题整合了笔记本电脑卡反应慢解决方法,阅读专题下面的文章了解更多详细内容。

6

2025.12.25

微信调黑色模式教程
微信调黑色模式教程

本专题整合了微信调黑色模式教程,阅读下面的文章了解更多详细内容。

5

2025.12.25

ps入门教程
ps入门教程

本专题整合了ps相关教程,阅读下面的文章了解更多详细内容。

4

2025.12.25

苹果官网入口直接访问
苹果官网入口直接访问

苹果官网直接访问入口是https://www.apple.com/cn/,该页面具备0.8秒首屏渲染、HTTP/3与Brotli加速、WebP+AVIF双格式图片、免登录浏览全参数等特性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

218

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
CSS3 教程
CSS3 教程

共18课时 | 4万人学习

PostgreSQL 教程
PostgreSQL 教程

共48课时 | 6.1万人学习

Git 教程
Git 教程

共21课时 | 2.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号