如题

解决方案 »

  1.   

    使用olecontainer可以实现把excel嵌入到程序中,你说得导出导入指什么?
      

  2.   

    导入:procedure TNewCardForm.Button2Click(Sender: TObject);
    var
        ExcelApp,MyWorkBook: OLEVariant;
        i,j,hykhlen,k,m,num: Integer;
        tmpName,
        tmpstr,
        newcardstr,
        tmphykh:String;begin
      XP_Button3.Enabled := False;
      num:=0;
      if opendialog1.Execute then
      begin
        try
          ExcelApp:=CreateOleObject('Excel.Application');
          MyWorkBook:=CreateOleobject('Excel.Sheet');
        except
          application.Messagebox('无法打开Xls文件,请确认已 经安装EXCEL.','',
               mb_OK+mb_IconStop);
          Exit;
        end;
        MyworkBook:= ExcelApp.workBooks.Open(opendialog1.FileName);
        //打开文件后,对文件进行操作
        i := 2;
        while true do
        begin
          j:= MyWorkBook.WorkSheets[1].Cells[i,1].Value;
          try
            tmphykh:=inttostr(j);
          except
            showmessage('can not convert to string, error at the'+inttostr(i)+'line');
            exit;
          end;
          tmpName:= MyWorkBook.WorkSheets[1].Cells[i,2].Value;
          hykhlen:= Length(tmphykh);
          newcardstr:=  MyWorkBook.WorkSheets[1].Cells[i,3].Value;
          {...}
            with adoquery2 do
            begin
              close;
              SQL.Clear;
              SQL.Add('select id,hykh,[name] from customer where hykh=:hykhvalue');
              Parameters.ParamByName('hykhvalue').Value:= tmphykh;
              Open;
              prior;
              if not eof then
                if trim(tmpName)  = trim(FieldByName('Name').AsString) then
                begin
                  close;
                  SQL.Clear;
                  SQL.Add('update customer set newcard='''+newcardstr+''' where hykh=:hykhvalue');
                  Parameters.ParamByName('hykhvalue').Value:= tmphykh;
                  ExecSQL;
                  num:=num+1;
                end;  {if}
            end;  {with}
          end;  {if}
        end;  { while}
        showmessage('你一共更新了'+InttoStr(num)+'行数据!');
        ADOQuery1.Requery();
        ExcelApp.quit;
        ExcelApp:=Unassigned; //释放VARIANT变量
      end;   {if}
      XP_Button3.Enabled := True;
    end;
      

  3.   

    导出:procedure TMemberBasicInfQuery.FlatSpeedButton4Click(Sender: TObject);
    var
    aSheet:Variant;
    i:integer;begin
    //显示Ms-excel的执行过程
    ExcelApplication1.Visible[0]:=True;
    ExcelApplication1.WorkBooks.Add(xlWBATWorksheet,1);
    aSheet:=ExcelApplication1.Worksheets.Item[1];aSheet.Cells[1,20].Value:='会员卡号';//asheet.cells[20,1].Value:='总计';
    i:=2;
    with adoquerycustomer do
     begin  first;   while not eof do begin
      
      aSheet.cells[i,20].NumberFormatLocal := '@';
      aSheet.cells[i,20].value:=FieldByName('NewCard').AsString;  i:=i+1;  next;
     //aSheet.SaveAs('d:\qqq.xls');
     // OleContainer1.CreateobjectFromFile('d:\qqq.xls',false);
    // aSheet.Application1.quit;  end;
    end;
    end;