Solved

How to copy excel cells to delphi

Posted on 2001-08-30
4
2,114 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 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