?
Solved

Excel

Posted on 2000-03-30
12
Medium Priority
?
488 Views
Last Modified: 2010-04-04
I want to open Excel sheet using OLE or Office servers, I'm not sure what is the best. I want an easy way to open such sheet and fill it automatically with data how can I do that?

Azzoz
0
Comment
Question by:Azzoz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 2668656
0
 
LVL 2

Expert Comment

by:florisb
ID: 2668693
some old code of mine. First form with some buttons, then Excell unit that starts excell and fill some fields.

some procedures are in dutch: localiseerEnVerander = localise and change.

Good luck!
Floris.

unit ExcelCom;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExcelUnit,Db, DBTables;



type
  TtoExcel = class(TForm)
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    GroupBox2: TGroupBox;
    OpenDialog1: TOpenDialog;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    GroupBox3: TGroupBox;
    chkNamen: TCheckBox;
    GroupBox4: TGroupBox;
    Label3: TLabel;
    edtL: TEdit;
    Label4: TLabel;
    edtN: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    function codeToInt(code:string):integer;
    function intToCode(intg:integer):string;
    procedure Button3Click(Sender: TObject);
    procedure chkNamenClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  toExcel: TtoExcel;

implementation

uses DirOrdDM;

{$R *.DFM}

procedure TtoExcel.Button1Click(Sender: TObject);

begin
if opendialog1.execute then
   begin
   ExcelActiveX.Start(True);
   ExcelActiveX.Open(opendialog1.filename);
   end;
end;

function TtoExcel.codeToInt(code:string):integer;
/// A->1..Z->26,AA->27,AB->28, enz.
begin
if length(code) = 1 then
   result := ord(code[1])-64
else
   result := (ord(code[1])-64)*26 +ord(code[2])-64;
end;

function TtoExcel.intToCode(intg:integer):string;
/// en 1->A..26->Z,26->AA, enz.
begin
if intg <= 26 then
   result := chr(intg+64)
else
   result := chr(((intg-1) div 26)+64) + chr(((intg-1) mod 26)+64+1);
end;



procedure TtoExcel.Button2Click(Sender: TObject);
var
beginletter,eindletter,beginnummer, eindnummer,x,y,z: integer;
bladwijzer, inhoud : string;
inhoudi,waarde,intNamenComp : integer;
inhoudr : real;
temp : char;
begin
if not((edtl.text = '' ) or (edtn.text = '' ))then
   begin
   beginletter := codeToInt(edtL.text);//ord(temp);
   eindletter := beginletter+ strToInt(edit1.text)-1;

   beginnummer := strToInt(edtN.text);
   eindnummer := beginnummer+strToInt(edit2.text)-1;
   dm.qryOverzicht.First;
   ///
   intNamenComp := 0;
   if chkNamen.checked then
      begin
      ///veld namen naar excel
      intNamenComp := 1;
      z := 0;
      for y := beginletter to eindletter do
          begin
          bladWijzer := intToCode(y)+intToStr(beginNummer);
          waarde := 0; ////int / str. waarde = 0 voor strings.
          inhoud := dm.qryOverzicht.fields[z].fieldName;
          ExcelActiveX.SelecteerEnVerander(bladwijzer,waarde,Inhoud);
          z := z + 1;
          end;
      end;

   for x := beginnummer+intNamenComp to eindnummer do
     begin
     /// waarden naar excel.
     z := 0;
     for y := beginletter to eindletter do
         begin
         bladwijzer := intToCode(y)+intToStr(x);
         /// ' voor veld als het geen numeriek veld is, zie excelUnit.
         if (dm.qryOverzicht.Fields[z] is TNumericField) then
            begin
            waarde := 1;

            inhoud := dm.qryOverzicht.Fields[z].asString;
            if not(pos(',',inhoud) = 0) then
               /// komma in getal
               begin
               if not ((pos(',',inhoud)+3) >= length(inhoud)) then
                   begin
                   /// meer dan twee cijfers achter komma.
                   if strToInt(inhoud[pos(',',inhoud)+3]) >= 5 then
                      begin
                      ///afronden, 0.005 bij optellen en daarna afkappen
                      inhoudr := dm.qryOverzicht.Fields[z].asFloat;
                      inhoudr := inhoudr + 0.005;
                      inhoud := floatToStr(inhoudr);
                      end;
                   ///afkappen.
                   inhoud := Copy(inhoud,1,pos(',',inhoud)+2);
                   end;
               end;
            end
         else
            begin
            waarde := 0;
            inhoud := dm.qryOverzicht.Fields[z].asString;
            end;
         if inhoud = '' then
            waarde := 1;  
         ExcelActiveX.SelecteerEnVerander(bladwijzer,waarde,Inhoud);
         z := z+1;
         end;
     dm.qryOverzicht.Next;
     end;
   end
else
   showMessage('Voer informatie begin-cel Excel in.');
end;

procedure TtoExcel.FormActivate(Sender: TObject);
begin
chkNamen.checked := false;
edit1.text := inttostr(dm.qryOverzicht.fieldcount);
edit2.text := inttostr(dm.qryOverzicht.recordCount);
end;

procedure TtoExcel.Button3Click(Sender: TObject);
begin
Close;
end;

procedure TtoExcel.chkNamenClick(Sender: TObject);
begin
if chkNamen.checked then
   edit2.Text := inttoStr(strToInt(edit2.text) +1)
else
   edit2.Text := inttoStr(strToInt(edit2.text) -1);
end;

procedure TtoExcel.Button4Click(Sender: TObject);
begin
ExcelActiveX.SelecteerEnVerander('C2',1,'1234567890');
ExcelActiveX.SelecteerEnVerander('C3',1,'123456789');
ExcelActiveX.SelecteerEnVerander('C4',1,'12345678');
ExcelActiveX.SelecteerEnVerander('C5',1,'1234567');
ExcelActiveX.SelecteerEnVerander('C6',1,'123456');
ExcelActiveX.SelecteerEnVerander('C7',1,'12345');
ExcelActiveX.SelecteerEnVerander('C8',1,'1234');
ExcelActiveX.SelecteerEnVerander('C9',1,'123');
ExcelActiveX.SelecteerEnVerander('C10',1,'12');
end;

end.


unit ExcelUnit;

interface

uses Excel_TLB, sysUtils;

type
  TExcelActiveX = class
  public
    procedure Start(Visible: Boolean);
    procedure Open(Bestand: string);
    procedure SelecteerEnVerander(EenCell: String; Waarde: integer; tekst:string);
    procedure LocaliseerEnVeranderInt(Bladwijzer:string; Waarde: integer; tekst:string);
    procedure Stop;

    procedure Bewaar(Bestand: String);
    procedure Print;

  end;

var
  ExcelActiveX: TExcelActiveX;
  FExcel: _Application = nil;

implementation

procedure TExcelActiveX.Start(Visible: Boolean);
begin
FExcel := CoApplication.Create;
FExcel.Visible[0] := True;
end;

procedure TExcelActiveX.Stop;
begin
if not assigned(FExcel) then exit;
FExcel.Quit;
end;

procedure TExcelActiveX.Open(Bestand: string);
var
  FileName, UpdateLinks, ReadOnly, Format, Password,
  WriteResPassword, IgnoreReadOnlyRecommended, Origin,
  Delimiter, Editable, Notify, Converter, AddToMRU: OleVariant;
begin
if not assigned(FExcel) then exit;
FileName := Bestand;
UpdateLinks := '0';
ReadOnly := False;
Format := 5; {geen scheidingsteken}
PassWord := '';
WriteResPassWord := '';
IgnoreReadOnlyRecommended := False;
Origin := xlWindows;
Delimiter := '';
Editable := True;
Notify := True;
Converter := 1; {was 0}
AddToMRU := False;
FExcel.Workbooks.Open(FileName, UpdateLinks, ReadOnly, Format, Password,
  WriteResPassword, IgnoreReadOnlyRecommended, Origin,
  Delimiter, Editable, Notify, Converter, AddToMRU, 0);
end;

procedure TExcelActiveX.Print;
var
  From, To_, Copies, Preview, ActivePrinter, PrintToFile, Collate, CLID: OleVariant;
begin
{Sub Macro1()
    ActiveWindow.SelectedSheets.PrintOut From:=1, To:=1, Copies:=1, _
    Preview:=False, ActivePrinter:="", PrintToFile:=False, Collate:=False
End Sub}
if not assigned(FExcel) then exit;
From := Null;
To_ := Null;
Copies := '1';
Preview := False;
ActivePrinter := FExcel.ActivePrinter[0];
PrintToFile := False;
Collate := False;
CLID := 0;
FExcel.ActiveWindow.SelectedSheets.PrintOut(From, To_, Copies, Preview, ActivePrinter, PrintToFile, Collate, CLID);
end;

procedure TExcelActiveX.LocaliseerEnVeranderInt(Bladwijzer:string;Waarde:integer;tekst:string);
var
  Scroll, Reference: OleVariant;
begin
if not assigned(FExcel) then
   exit;
///
Scroll := 0;
Reference := Bladwijzer;
FExcel.Application.Goto_(Reference, Scroll, 0);
///
if tekst = '' then
   FExcel.ActiveCell.Value := waarde
else
   begin
   tekst := ''''+tekst;
   FExcel.ActiveCell.Value := tekst;
   end;
end;

procedure TExcelActiveX.SelecteerEnVerander(EenCell:String;Waarde:integer;tekst:string);
var
  Cell1, Cell2: OleVariant;
begin
if not assigned(FExcel) then
   exit;
///
Cell1 := EenCell;
Cell2 := EenCell;
FExcel.Range[Cell1, Cell2].Select;
///
if not( waarde = 1) then
   tekst := ''''+tekst;
FExcel.ActiveCell.Value := tekst;
end;

procedure TExcelActiveX.Bewaar(Bestand: String);
var
  Filename, FileFormat, Password, WriteResPassword,
  ReadOnlyRecommended, CreateBackup, AccessMode, ConflictResolution,
  AddToMru, TextCodePage, TextVisualLayout : OleVariant;
begin
Filename := Bestand;
FileFormat := xlNormal;
Password := '';
WriteResPassword := '';
ReadOnlyRecommended := False;
CreateBackup := False;
AccessMode := xlNoChange;
ConflictResolution := xlLocalSessionChanges;
AddToMru := True;
TextCodePage := '';
TextVisualLayout := '';
FExcel.ActiveWorkbook.SaveAs(Filename, FileFormat, Password, WriteResPassword,
                      ReadOnlyRecommended, CreateBackup, AccessMode, ConflictResolution,
                      AddToMru, TextCodePage, TextVisualLayout, 0);
end;

end.



0
 
LVL 2

Expert Comment

by:aubs
ID: 2668721
(uses OleAuto;)

var
  v: variant;

procedure CreateExcel;
begin
   v:=CreateOleObject('Excel.Application');
   v.Application.Workbooks.Open('c:\My Documents\excel.xls');
   v.Visible := True;//only needed if you want to see the sheet
end;

procedure writetocell(Arow,Acol: integer; AValue: variant);                             begin
  v.ActiveWorkBook.ActiveSheet.cells(Arow, Acol).Value:= AValue; A1
end;

procedure QuitExcel;
begin
  v.Quit; //will destroy the Excel object!
end;

Regards
Aubs
0
Technology Partners: 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!

 
LVL 2

Expert Comment

by:aubs
ID: 2668724
Sorry a few typos:

(uses OleAuto;)

var
  v: variant;

procedure CreateExcel;
begin
  v:=CreateOleObject('Excel.Application');
  v.Application.Workbooks.Open('c:\My Documents\excel.xls');
  v.Visible := True;//only needed if you want to see the sheet
end;

procedure writetocell(Arow,Acol: integer; AValue: variant);                             begin
  v.ActiveWorkBook.ActiveSheet.cells(Arow, Acol).Value:= AValue;
end;

procedure QuitExcel;
begin
 v.Quit; //will destroy the Excel object!
 end;

Regards
Aubs
0
 

Author Comment

by:Azzoz
ID: 2669002
Aubs,

1.It seem that it works, but there is a problem in puting data in cells, how can I do that without using Variant, I need  a small example of sending Numberic data.

2. How I open new document

3. Where can I find another command? (Optional)

Azzoz
0
 
LVL 15

Expert Comment

by:simonet
ID: 2669111
following...®
0
 
LVL 2

Expert Comment

by:aubs
ID: 2669681
Does this help?

var
  v: variant;

procedure CreateExcel;
const
  xlWBatWorkSheet = -4167;
var
  WorkBook        : variant;
begin
  v:=CreateOleObject('Excel.Application');
  WorkBook := V.Workbooks.Add(xlWBatWorkSheet);
  WorkBook.WorkSheets[1].name := 'Test Sheet';
  v.Visible := True;//only needed if you want to see the sheet
end;

procedure PutDataAt(strSheet: String; Row, Col: Integer; strData: String);
var
  Sheet   : variant;
begin
  Sheet := V.WorkBooks[1].WorkSheets[strSheet];
  Sheet.Cells[Row, Col].value := strData;
end;

procedure QuitExcel;
begin
  v.Quit; //will destroy the Excel object!
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  CreateExcel;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  putDataAt('Test Sheet',3,3,'23');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  QuitExcel;
end;


Aubs
0
 
LVL 2

Accepted Solution

by:
aubs earned 400 total points
ID: 2669683
Does this help?

(uses ComObj;)

var
  v: variant;

procedure CreateExcel;
const
  xlWBatWorkSheet = -4167;
var
  WorkBook        : variant;
begin
  v:=CreateOleObject('Excel.Application');
  WorkBook := V.Workbooks.Add(xlWBatWorkSheet);
  WorkBook.WorkSheets[1].name := 'Test Sheet';
  v.Visible := True;//only needed if you want to see the sheet
end;

procedure PutDataAt(strSheet: String; Row, Col: Integer; strData: String);
var
  Sheet   : variant;
begin
  Sheet := V.WorkBooks[1].WorkSheets[strSheet];
  Sheet.Cells[Row, Col].value := strData;
end;

procedure QuitExcel;
begin
  v.Quit; //will destroy the Excel object!
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  CreateExcel;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  putDataAt('Test Sheet',3,3,'23');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  QuitExcel;
end;


Aubs
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2670135
Azzoz, did you look at the page I gave you in the first comment?
0
 

Author Comment

by:Azzoz
ID: 2675541
Thanks Epsylon and Abus, I'll give both of the the points

But befor that, what about different versions of Excel (2000, 95 and 97)? Did it work with all versions?

Azzoz
0
 

Author Comment

by:Azzoz
ID: 2675580
Thanks, a very concies solution.

But I'm still asking about different versions of Excel!!

Epsylon I'll ask a new quesion (For Epsylon) to give you 100 points for your usifull site.

Azzoz
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2675774
Hi Azzoz, about the different versions, as always with software development, nothing is sure until tested and approved. But the most common features are (should be) upward compatible.

I don't know if Excel 95 supports automation at all...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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