Solved

How to copy excel cells to delphi

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 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