⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cgf2.pas

📁 非常丰富的delphi小程序,相信会对你有帮助
💻 PAS
字号:
unit cgf2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, DB, DBTables, ADODB, ExtCtrls, StdCtrls, Buttons,
  Excel2000, OleServer;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    ADOTable1: TADOTable;
    Table1: TTable;
    DataSource1: TDataSource;
    Label1: TLabel;
    Edit1: TEdit;
    SpeedButton1: TSpeedButton;
    Button1: TButton;
    Button2: TButton;
    Panel2: TPanel;
    SpeedButton2: TSpeedButton;
    OpenDialog1: TOpenDialog;
    Label2: TLabel;
    ComboBox1: TComboBox;
    Edit2: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
   Function Legit1(const s:string):boolean;
   Procedure Auto_Import(Sender:Tobject);
   Function Legit2(Sender:Tobject):Boolean;{ Public declarations }
  end;

var
  Form1: TForm1;
  Ea1:TExcelApplication;
  Ew1:TExcelWorkbook;
  Es1:TExcelWorksheet ;
implementation

uses cgf3;

{$R *.dfm}

procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
 Form1.Close;
end;

function TForm1.Legit1(const s: string): boolean;
var str:string;count1:integer;
begin
  count1:=length(s);
  if count1<8 then  result:=false
   else
    begin
     str:=copy(s,count1-3,4);
     if (Str='.xls')or(str='.XLS')or(str='.Xls')or(str='.xLs')or(str='.xlS')
       or(str='.XLs')or(str='.xLS')or(str='.XLs') then  result:=true
     Else result:=false;
    end;

end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
var count1,i:integer;
begin
  If opendialog1.Execute  then
    begin
    Edit1.Enabled :=true;
    Edit1.Text:=Opendialog1.FileName ;
    Edit2.Visible :=true;
    Label2.visible:=true;
    Label3.Visible :=true;
    Label4.Visible :=true;
    Combobox1.Visible:=true;
    Combobox1.Clear;
    ea1:=TExcelApplication.Create(self);

    ew1:=TExcelworkbook.Create(self);

    es1:=TExcelWorksheet.Create(self);
    
    Try
    Ea1.Connect ;
    Except
    Showmessage('请检察是否正确安装了Excel,或者Excel应经运行了');
    Abort;
    end;
    ea1.Visible[0]:=false;
    Ea1.Workbooks.Open(edit1.Text,null,null,null,null,null,null,null,null,null,null,null,null,0);
    Ew1.ConnectTo(Ea1.Workbooks[1]);
    count1:=Ew1.Worksheets.Count;
    Edit2.Text:=Inttostr(count1);
    For i:=1 to count1 do
     begin
     Es1.ConnectTo(Ew1.Worksheets[i] as _worksheet);
     combobox1.Items.Add(Es1.Name);
     end;
    Ea1.Quit;
    Ea1.Disconnect ;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 If Legit1(Edit1.Text) and Legit2(Edit2) and (Combobox1.text<>'')  then
    begin
   
    Auto_Import(table1);

    end
   Else
    begin
     Showmessage('文件选择的有问题,请重新选择再试一次 !');
    end;
end;

procedure TForm1.Auto_Import(Sender: Tobject);
var str,str1,str2:string;count2,j,Flag:integer;
begin
 Str:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+Edit1.text+';'+'Extended Properties=Excel 8.0;Persist Security Info=False';
 AdoTable1.ConnectionString:=str;
 AdoTable1.TableName:=combobox1.Text;
 Adotable1.Active:=true;
 Table1.Active:=true;
 Count2:=Adotable1.FieldCount ;
 Table1.Active :=true;
 AdoTable1.first;
while not Adotable1.Eof  do
  begin//1
  Flag:=0;
  Str1:=Adotable1.Fields[0].AsString ;
  Table1.First ;
  While not Table1.Eof do
   begin //2
    str2:=Table1.Fields[0].AsString ;
    if str1=str2 then
      begin //3
       if messagedlg('员工编号为'+str1+'的姓名为'+Adotable1.Fields[1].AsString+'重复,是否替换',mtwarning,[mbyes,mbno],0)=mryes then
         begin//4
           Table1.Edit;
           For j:=1 to count2-1 do
           begin
           Table1.Fields[j].AsString:=Adotable1.Fields[j].AsString;
           end;
           Table1.post;
           Flag:=1;//代表已经作替换处理
           Adotable1.next;
           break;
         end  //4
        else
         begin //4
          Flag:=2;//代表不处理
          Adotable1.next;
          break;
         end;  //4
      end   //3
     else
      begin//3
        Flag:=0;
        Table1.next;
        continue;
      end; //3
   end; //2
   if Flag=0  then
    begin
     Table1.Append;
      For j:=0 to count2-1 do
      begin
      Table1.Fields[j].AsString:=Adotable1.Fields[j].AsString;
      end;
   Table1.post;
   AdoTable1.Next;
    end;
 end;//1
 end;


procedure TForm1.FormCreate(Sender: TObject);
begin

Edit1.Clear;
Edit1.Enabled :=false;
Edit2.Clear;
Combobox1.Clear;
end;

function TForm1.Legit2(Sender: Tobject): Boolean;
var count1,count2:integer;
begin
  count1:=strtoint(Edit2.Text);
  count2:=Combobox1.Items.Count;
  If (count1>0) and (count2>0) and (count1=count2) then
     begin
      Result:=true;
     end
    else
     begin
      Result:=false;
     end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
   table1.Active:=true;
   if table1.RecordCount=0 then
     begin
      showmessage('数据库中没有内容!');
      Table1.Active:=false;
     end
    Else
     begin
     Form2.visible:=true;
     Form1.Table1.Active:=true;
     Table1.first;
    end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -