use OfficePartner components from this site:
  http://sourceforge.net/projects/tpofficepartner/
   
  a sample source code for exporting data to excel would be like
  assuming it is a button click
  procedure TForm1.btnEExportClick(Sender: TObject);
var
  Rng:TOPExcelRange;
  i:integer;
begin
    ADOQuery2.Close;
    ADOQuery2.SQL.Clear;
    ADOQuery2.SQL.Add('SELECT * FROM oldepts');//:TableName');
    
//ADOQuery2.Parameters.ParamByName('TableName').Value:=trim(EditETableName.Text);
    ADOQuery2.Open;
    OpExcel1.Connected:=true;
    OpExcel1.Visible:=true;
    OpExcel1.Workbooks.Add;
    for i:=1 to 2 do
    OpExcel1.Workbooks[0].Worksheets.Add;
    //ranges
    Rng:=OpExcel1.Workbooks[0].Worksheets[0].Ranges.Add;
    Rng.Name:='TableRange';
    Rng.Address:='A1';
    Rng.OfficeModel:=OpDatasetModel1;
    OpDatasetModel1.Dataset:=AdoQuery2;
    //fill worksheet start at anchor
    OpExcel1.RangeByName['TableRange'].Populate;
    //Activate worksheet
    OpExcel1.Workbooks[0].Worksheets[0].Activate;
    Rng.AutoFitColumns;
  end;
   
  then sample code for importing from excel would be like
   
  procedure TForm1.OpenExcelClick(Sender: TObject);
var
  ExistingWorkBook:TOpExcelWorkBook;
  r:TOpExcelRange;
  Alpha,CellAddr:string;
  i,Num,RCount:integer;
  z:TOpExcelRange;
begin
  Num:=1;
  RCount:=StrToInt(EditRows.Text);
  ExistingWorkBook:=OpExcel1.Workbooks.Add;
  ExistingWorkBook.Filename:=trim(EditFileName.Text);
  OpExcel1.Connected:=true;
  OpExcel1.Visible:=true;
  r:=ExistingWorkBook.Worksheets[0].Ranges.Add;
  r.Address:=trim(EditRange.Text);
  r.AutoFitColumns;
  r.Select;
  //Loop for putting to database
  for i:=0 to RCOunt-1 do begin
      Num:=Num+1;//The dataset 1st Record
      Alpha:='B';
      //Addressing
      cellAddr:=Alpha+IntToStr(Num);
      z:=ExistingWorkBook.Worksheets[0].Ranges.Add;
      z.Address:=cellAddr;//End of Addressing
      ADOQuery1.Edit;
      ADOQuery1.Append;
      ADOQuery1EmployerCode.Value:=z.AsRange.Value;
      ADOQuery1.Post;
      Alpha:='C';
      //Addressing
      cellAddr:=Alpha+IntToStr(Num);
      z:=ExistingWorkBook.Worksheets[0].Ranges.Add;
      z.Address:=cellAddr;//End of Addressing
      ADOQuery1.Edit;
      ADOQuery1Department.Value:=z.AsRange.Value;
      ADOQuery1.Post;
      Alpha:='D';
      //Addressing
      cellAddr:=Alpha+IntToStr(Num);
      z:=ExistingWorkBook.Worksheets[0].Ranges.Add;
      z.Address:=cellAddr;//End of Addressing
      ADOQuery1.Edit;
      ADOQuery1Description.Value:=z.AsRange.Value;
      ADOQuery1.Post;
  end;//for loop
      //close Excel
  ExistingWorkBook.AsWorkbook.Close(true,'C:\depts1.xls',EmptyParam,0);
end;
//written using delphi 7 on windows 2000 sp4. hope this helps.

                
---------------------------------
Get your email and more, right on the  new Yahoo.com 

[Non-text portions of this message have been removed]



-----------------------------------------------------
Home page: http://groups.yahoo.com/group/delphi-en/
To unsubscribe: [EMAIL PROTECTED] 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/delphi-en/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to