Solved

How to copy excel cells to delphi

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

749 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