Solved

How to copy excel cells to delphi

Posted on 2001-08-30
4
2,111 Views
Last Modified: 2012-06-21
Copy Excel Data  to ClipBoard
=============================
Col1    Col2    Col3
1     2     2002/10/10
1     2     2002/1/1
2     2     2002/10/10
2     2     2002/1/1
3     1     2002/10/10
3     2     2002/1/1

How can capture data and format as (n x n) matrix of data ?
(eg.  TSTRINGLIST ....)
0
Comment
Question by:aavictor
  • 2
4 Comments
 

Accepted Solution

by:
jmbonet earned 25 total points
ID: 6440572
Hi, I use the next piece of code in a more complex component designed to perform I/O width TDataSource's. The procedure do a "Paste" operation to the associated TDataSource. I think that can be easily modified to accomplish your requirements.

By.

__________________________________________________________________
procedure TIBDTable.pasteClipboard;
var List: TStrings;
    cp: TClipboard;
begin
     cp := Clipboard;
     List := TStringList.Create;
     List.Text := cp.AsText;
     SetClipFmt(List);
     List.Destroy;
end;

procedure TIBDTable.SetClipFmt(l: TStrings);
var s, sidx: string;
    tmp, vals: TStrings;
    pos: Integer;
    es_taula: Boolean;
    ti: String;
//    ti: TIndexDef;

    procedure Tabs2TStrings(st: String; var l: TStrings);
    var i: integer;
        s: String;
    begin
         l.Clear;
         s := '';
         for i := 1 to length(st) do begin
             if st[i] = #9 then begin
                l.Add(s);
                s := '';
             end else begin
                 s := s + st[i];
             end;
         end;
         if s <> '' then
            l.Add(s);
    end;
begin
     if not Assigned(FDataSet) then
        Exit;

     es_taula := (FDataSet is TTable);
     FDataSet.Close;
     FDataSet.Open;
     ti := '';
     if es_taula then begin
        ti := IndexPrimari;
        if ti = '' then
           es_taula := False
        else
            sidx := LowerCase(ti);
     end;
     FDataSet.DisableControls;
     // Si cal, esborrar el contingut actual ...
     if FClearBefore then
        with FDataSet do begin
             First;
             while not FDataSet.EOF do begin
                   Delete;
                   Inc(FNDel);
             end;

        end;
     // Refrescar les dades altra vegada ...
     FDataSet.Close;
     FDataSet.Open;

     tmp := TStringList.Create;
     vals := TStringList.Create;

     l.BeginUpdate;

     try

        FDataSet.GetFieldNames(tmp);
        FDataSet.GetFieldNames(vals);
        pos := 0;
        if FUseFieldNames then begin
           s := l.Strings[pos];
           inc(pos);
           Tabs2TStrings(s,tmp);
        end;
        PreparaIndexos(es_taula,tmp,ti);
        while (pos < l.Count) do begin
          s := l.Strings[pos];
          if s = '<NULL>' then
             s := '';
          Tabs2TStrings(s,vals);
          TractarImportRegistre(es_taula, tmp, vals);
          inc(pos);
          Inc(FNRegs);
        end;
     finally
        l.EndUpdate;
        tmp.Destroy;
        vals.Destroy;
        FDataSet.EnableControls;
     end;
end;
0
 
LVL 22

Assisted Solution

by:mnasman
mnasman earned 25 total points
ID: 6441236
Hello

  This code will help you, it's create worksheet in execl and insert no from 1 to 10 in excel cells, then copy them to the clipboard and past the cell into memo box

uses ComObj;

procedure TForm1.Button1Click(Sender: TObject);
var
  XLApp : Variant;
  WordApp : Variant;
  Sheets : Variant;
  i : Integer;

const
  xlWBATWorksheet    = -4167;
  wdDoNotSaveChanges = 0;
begin
  XLApp := CreateOleObject('Excel.Application');
  XLApp.Visible := True;
  XLApp.Workbooks.Add[XLWBatWorksheet];
  XLApp.Workbooks[1].Worksheets[1].Name := 'Delphi Test';
  Sheets := XLApp.Workbooks[1].Worksheets['Delphi Test'];
  for i := 1 to 10 do
    Sheets.Cells[i, 1] := i;
  Sheets := XLApp.Sheets;
  Sheets.Item['Delphi Test'].Activate;
  Sheets.Item['Delphi Test'].Range['A1:A10'].Select;
  Sheets.Item['Delphi Test'].UsedRange.Copy;
  Memo1.PasteFromClipboard;
end;

Best regards
Mohammed Nasman
0
 
LVL 17

Expert Comment

by:geobul
ID: 9289425
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

accept mnasman's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thanks,

geobul
EE Cleanup Volunteer
0
 
LVL 17

Expert Comment

by:geobul
ID: 9294057
Thank you aavictor

Regards, Geo
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now