一些代码,涉及到excel的简单读取操作

时间:2022-06-25 16:09:09

unit Mainfrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB, ComOBJ;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Label3: TLabel;
    Edit3: TEdit;
    OpenDialog1: TOpenDialog;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    Button1: TButton;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    Edit9: TEdit;
    Edit10: TEdit;
    Edit11: TEdit;
    Edit12: TEdit;
    Edit13: TEdit;
    Edit14: TEdit;
    Edit15: TEdit;
    Edit16: TEdit;
    Edit17: TEdit;
    Edit18: TEdit;
    Edit19: TEdit;
    Edit20: TEdit;
    Edit21: TEdit;
    Edit22: TEdit;
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    Label2: TLabel;
    Label23: TLabel;
    Edit23: TEdit;
    Button2: TButton;
    ADOConnection2: TADOConnection;
    ADOQuery2: TADOQuery;
    Label24: TLabel;
    Edit24: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
//var data_ip,user_id,pass_word:string;
var sql,sql1,sql2: string;
begin
   { //data_ip:=edit1.Text;
    //user_id:=edit2.Text;
    //pass_word:=edit23.Text; }

    adoconnection1.Close;
    adoconnection1.ConnectionString:= 'Provider=SQLOLEDB.1;Password='''+edit23.Text+''';Persist Security Info=true;User ID='''+edit2.Text+''';Initial Catalog=item;Data Source='''+edit1.text+'''';
    adoquery1.Connection:=adoconnection1;
    //showmessage('1');
    sql1:='insert into product select item,ref,other_item,rose_item,rose_len,leaf_item,leaf_len,base_item,base_len,others1,others2,others3,others4,item_check,qc_check,pic_date,add_date,adder,pic,pichave,item_sort';
    sql2:=' from product_temp where not exists(select * from product where item=product_temp.item)';
    sql:=sql1+sql2;
    adoquery1.SQL.Clear;
    //adoquery1.SQL.Add('insert into product select item,ref,other_item,rose_item,rost_len,leaf_item,leaf_len,base_item,base_len,others1,others2,others3,others4,item_check,qc_check,pic_date,add_date,adder,pic,pichave,item_sort from product_temp where not exists(select * from product where item=product_temp.item)');
    adoquery1.SQL.Add(sql);
    //showmessage('2');
    adoquery1.ExecSQL;
    showmessage('添加成功');
    //adoquery1.Open;

    adoquery1.SQL.Clear;
    adoquery1.SQL.Add('delete from product_temp');
    adoquery1.ExecSQL;
    showmessage('临时数据已经清除');

                                        

end;

procedure TForm1.Button2Click(Sender: TObject);

var   MsExcel, MsExcelWorkBook, MsExcelWorkSheet: Variant;
      Col1,Col2: String;
      s1: TStringList;
      Num: array[1..10000] of Integer;
      i,j: integer;
      k: integer;


begin
    //打开excel
    MsExcel := CreateOleObject('Excel.Application');
    MsExcelWorkBook := msExcel.Workbooks.Open(edit3.Text);
    MsExcelWorkSheet := msExcel.Worksheets.Item[1];
    //取得行数
    j:=MsExcel.Worksheets.Item[1].UsedRange.Rows.count;
    //将数据写入数组
    for i := 1 to j do Num[i]:= Integer(msExcel.Cells[i, 1]);
    msExcel.Quit;

    //for i:= 1 to j do showmessage(inttostr(num[i]));

    //写入数据库
    adoconnection1.Close;
    adoconnection1.ConnectionString:= 'Provider=SQLOLEDB.1;Password='''+edit23.Text+''';Persist Security Info=true;User ID='''+edit2.Text+''';Initial Catalog=item;Data Source='''+edit1.text+'''';
    adoquery1.Connection:=adoconnection1;
    adoquery1.SQL.Clear;

    //showmessage(inttostr(j));

    for i:=1 to j do
      begin
        k:=num[i];
        //showmessage(inttostr(k));
        adoquery1.SQL.Clear;
        adoquery1.SQL.Add('insert into product_temp values('+inttostr(num[i])+','''+edit4.Text+''','''+edit24.Text+''','''+edit5.Text+''','''+edit6.Text+''','''+edit7.Text+''','''+edit8.Text+''','''+edit9.Text+''','''+edit10.Text+''','''+edit11.Text+''','''+edit12.Text+''','''+edit13.Text+''','''+edit14.Text+''','''+edit15.Text+''','''+edit16.Text+''','''+edit17.Text+''','''+edit18.Text+''','''+edit19.Text+''','''+edit20.Text+''','''+edit21.Text+''','''+edit22.Text+''')');
        adoquery1.ExecSQL;
      end;

    showmessage('导入成功')


end;

end.