Home Database Mysql Tutorial 将数据导出至 M$ Access

将数据导出至 M$ Access

Jun 07, 2016 pm 03:32 PM
access dev express Export data

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to disable background applications in Windows 11_Windows 11 tutorial to disable background applications How to disable background applications in Windows 11_Windows 11 tutorial to disable background applications May 07, 2024 pm 04:20 PM

1. Open settings in Windows 11. You can use Win+I shortcut or any other method. 2. Go to the Apps section and click Apps & Features. 3. Find the application you want to prevent from running in the background. Click the three-dot button and select Advanced Options. 4. Find the [Background Application Permissions] section and select the desired value. By default, Windows 11 sets power optimization mode. It allows Windows to manage how applications work in the background. For example, once you enable battery saver mode to preserve battery, the system will automatically close all apps. 5. Select [Never] to prevent the application from running in the background. Please note that if you notice that the program is not sending you notifications, failing to update data, etc., you can

Slow Cellular Data Internet Speeds on iPhone: Fixes Slow Cellular Data Internet Speeds on iPhone: Fixes May 03, 2024 pm 09:01 PM

Facing lag, slow mobile data connection on iPhone? Typically, the strength of cellular internet on your phone depends on several factors such as region, cellular network type, roaming type, etc. There are some things you can do to get a faster, more reliable cellular Internet connection. Fix 1 – Force Restart iPhone Sometimes, force restarting your device just resets a lot of things, including the cellular connection. Step 1 – Just press the volume up key once and release. Next, press the Volume Down key and release it again. Step 2 – The next part of the process is to hold the button on the right side. Let the iPhone finish restarting. Enable cellular data and check network speed. Check again Fix 2 – Change data mode While 5G offers better network speeds, it works better when the signal is weaker

The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks Apr 29, 2024 pm 06:55 PM

I cry to death. The world is madly building big models. The data on the Internet is not enough. It is not enough at all. The training model looks like "The Hunger Games", and AI researchers around the world are worrying about how to feed these data voracious eaters. This problem is particularly prominent in multi-modal tasks. At a time when nothing could be done, a start-up team from the Department of Renmin University of China used its own new model to become the first in China to make "model-generated data feed itself" a reality. Moreover, it is a two-pronged approach on the understanding side and the generation side. Both sides can generate high-quality, multi-modal new data and provide data feedback to the model itself. What is a model? Awaker 1.0, a large multi-modal model that just appeared on the Zhongguancun Forum. Who is the team? Sophon engine. Founded by Gao Yizhao, a doctoral student at Renmin University’s Hillhouse School of Artificial Intelligence.

The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. May 07, 2024 pm 05:00 PM

Recently, the military circle has been overwhelmed by the news: US military fighter jets can now complete fully automatic air combat using AI. Yes, just recently, the US military’s AI fighter jet was made public for the first time and the mystery was unveiled. The full name of this fighter is the Variable Stability Simulator Test Aircraft (VISTA). It was personally flown by the Secretary of the US Air Force to simulate a one-on-one air battle. On May 2, U.S. Air Force Secretary Frank Kendall took off in an X-62AVISTA at Edwards Air Force Base. Note that during the one-hour flight, all flight actions were completed autonomously by AI! Kendall said - "For the past few decades, we have been thinking about the unlimited potential of autonomous air-to-air combat, but it has always seemed out of reach." However now,

How to convert deepseek pdf How to convert deepseek pdf Feb 19, 2025 pm 05:24 PM

DeepSeek cannot convert files directly to PDF. Depending on the file type, you can use different methods: Common documents (Word, Excel, PowerPoint): Use Microsoft Office, LibreOffice and other software to export as PDF. Image: Save as PDF using image viewer or image processing software. Web pages: Use the browser's "Print into PDF" function or the dedicated web page to PDF tool. Uncommon formats: Find the right converter and convert it to PDF. It is crucial to choose the right tools and develop a plan based on the actual situation.

Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year! Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year! May 06, 2024 pm 04:13 PM

The latest video of Tesla's robot Optimus is released, and it can already work in the factory. At normal speed, it sorts batteries (Tesla's 4680 batteries) like this: The official also released what it looks like at 20x speed - on a small "workstation", picking and picking and picking: This time it is released One of the highlights of the video is that Optimus completes this work in the factory, completely autonomously, without human intervention throughout the process. And from the perspective of Optimus, it can also pick up and place the crooked battery, focusing on automatic error correction: Regarding Optimus's hand, NVIDIA scientist Jim Fan gave a high evaluation: Optimus's hand is the world's five-fingered robot. One of the most dexterous. Its hands are not only tactile

Single card running Llama 70B is faster than dual card, Microsoft forced FP6 into A100 | Open source Single card running Llama 70B is faster than dual card, Microsoft forced FP6 into A100 | Open source Apr 29, 2024 pm 04:55 PM

FP8 and lower floating point quantification precision are no longer the "patent" of H100! Lao Huang wanted everyone to use INT8/INT4, and the Microsoft DeepSpeed ​​team started running FP6 on A100 without official support from NVIDIA. Test results show that the new method TC-FPx's FP6 quantization on A100 is close to or occasionally faster than INT4, and has higher accuracy than the latter. On top of this, there is also end-to-end large model support, which has been open sourced and integrated into deep learning inference frameworks such as DeepSpeed. This result also has an immediate effect on accelerating large models - under this framework, using a single card to run Llama, the throughput is 2.65 times higher than that of dual cards. one

How to read dbf file in oracle How to read dbf file in oracle May 10, 2024 am 01:27 AM

Oracle can read dbf files through the following steps: create an external table and reference the dbf file; query the external table to retrieve data; import the data into the Oracle table.

See all articles