Solved

How to copy excel cells to delphi

Posted on 2001-08-30
4
2,113 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:Mohammed Nasman
Mohammed Nasman 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi OLE Error 8 93
Delphi procedure  - Unsatisfied forward or external declaration. 2 140
Help on project with Soap 10 48
Performance of SQL statement 37 97
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

911 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

19 Experts available now in Live!

Get 1:1 Help Now