Excel, TExcelApplication, Delphi7

Hi,

I'm looking for some documents/examples/help on using the TExcelApplication component on the servers tab of delphi7.

jamesr123456Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Greg RowlandSoftware Designer, SysDBA, WebMaster OwnerCommented:
http://www.djpate.freeserve.co.uk/Automation.htm

There is an old message thread I will try to locate for you.
0
BlackTigerXCommented:
0
Greg RowlandSoftware Designer, SysDBA, WebMaster OwnerCommented:
procedure TGeneralProceduresf.myStartExcelWorkBookSimple;
begin
  Screen.Cursor := crSqlWait;
  { No need to start Excel ourselves, since the ExcelApplication's AutoConnect
  property is True }
  lcid := GetUserDefaultLCID;
  ExcelApplication1.Visible[lcid]:=True;

  { Create a new workbook }
  { The TOleEnum cast in the next line just prevents compiler range warnings }
  ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(TOleEnum(xlWBATWorksheet), lcid));
  ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
// ExcelApplication1.ScreenUpdating[lcid] := False;

end;

procedure TGeneralProceduresf.myEndExcelWorkBook;
begin
 Screen.Cursor := crDefault;
 ExcelApplication1.ScreenUpdating[lcid] := True;

 ExcelWorksheet1.Disconnect;
 ExcelWorkbook1.Disconnect;
 ExcelApplication1.Disconnect;

end;


procedure myExcelData(myDataSource: TwwDataSource);
var
 myArray: Variant;
 c, r, i: integer;
 myCol: String;

begin
// Schedule report --------

 with myDataSource.DataSet do begin
   // myData.DataSet
   myArray := VarArrayCreate([1, RecordCount + 10, 1, FieldCount + 10], varVariant);
   myCol := myColumnAlpha(FieldCount + 10)  + IntToStr(RecordCount + 10);

   for  i := 0 to FieldCount - 1 do
     if not (Fields[i].IsBlob)
        and (Fields[i].FieldName <> 'NOTES')
        and (Fields[i].FieldName <> 'NEWNOTE') then
       myArray[ 1, i + 1] := Fields[i].FieldName;

   First;
   r := 2;
   repeat
     for  i := 0 to FieldCount - 1 do
       if not (Fields[i].IsBlob)
         and (Fields[i].FieldName <> 'NOTES')
         and (Fields[i].FieldName <> 'NEWNOTE') then
           myArray[ r, i + 1] :=  Fields[i].AsString;

     Inc(r);
     Next;
   until (Eof);

   with GeneralProceduresf, ExcelApplication1, ExcelWorksheet1 do begin
     myStartExcelWorkBookSimple;
     Range['A1', myCol].Value := myArray;
     Range['A1', 'A1'].Select;
     Range['A1', myColumnAlpha(FieldCount + 10) + '1'].Font.Bold := True;

     myEndExcelWorkBook;

   end;

 end;


end;


Good luck
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

calinutzCommented:
For documents on how to use Excel use the Deborah Pate's automation pages as suggested above, they are the best you'll get on the web.
But for specific questions... you are welcome to ask them here.
0
jamesr123456Author Commented:
Deborah's pages are old and dont work with ExcelXP
0
calinutzCommented:
:)
Then I guess you will need to ask a question.... ;)
0
jamesr123456Author Commented:
I did ask a question! I was hoping for some recent examples/links. Word 97 is 5 years old!
0
Greg RowlandSoftware Designer, SysDBA, WebMaster OwnerCommented:
The code examples run under Delphi 7.0 Office 97, 2000 and 2003, I will try them with Offce XP when I get a chance.

I started working automation in 1998 initially with a lot of frustration.
Ultimately “while not simple” the results have been awesome.
I would highly recommend you subscribe to this newsgroup
      borland.pulic.delphi.oleautomation

Deborah “The Goddess of automation” frequents there and is very generous with knowledge.

Cheers,

G
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jamesr123456Author Commented:
I seem to be getting somewhere, slowly! damn! was hoping I wouldnt have to get my feet wet, oh well...
0
jamesr123456Author Commented:
For anybody wanting another example:
uses OleServer, ExcelXp, Activex;

var XApp: TExcelApplication;
    LCID: Integer;
    XWorkBook: TExcelWorkBook;
    XWorkSheet: TExcelWorkSheet;
    XDataSet:TDataset;
    XStartCell:array[1..2] of string;
    LoopRows:Integer;
    LoopColumns:Integer;
    MyRange:String;
    temp:integer;
    Format: OleVariant;
    LargestColumn:integer;

begin
LargestColumn:=0;

// Limited to a range between A and Z

// Open Excel
LCID:=GetUserDefaultLCID;

XApp:=TExcelApplication.Create(Self);
XWorkBook:=TExcelWorkBook.Create(Self);
XWorkSheet:=TExcelWorkSheet.Create(Self);

XApp.ConnectKind:=ckNewInstance;
XApp.Connect;
XApp.Visible[LCID]:=True;
XApp.ScreenUpdating[LCID] := False;

XWorkBook.ConnectTo(XApp.Workbooks.Add(TOleEnum(xlWBATWorksheet),lcid));
XWorkSheet.ConnectTo(XApp.Worksheets[1] as _Worksheet);

XDataSet:=Cost.CostTable; //TClientDataset

// Copy dataset column headings to excel
XStartCell[1]:=Chr(Ord('A')-1);
XStartCell[2]:='4';
For LoopColumns:=0 to XDataSet.FieldCount-1 do
  begin
  MyRange:= Chr(Ord(XStartCell[1][1])+LoopColumns+1) + inttostr( strtoint(XStartCell[2])+1);
  XWorkSheet.Range[MyRange, MyRange].Value2:=XDataSet.Fields[LoopColumns].DisplayName;
  XWorkSheet.Range[MyRange, MyRange].EntireColumn.AutoFit;
  end;

// Copy rest of dataSet into excel
  begin
  XStartCell[1]:=Chr(Ord('A')-1);
  XStartCell[2]:='5';

  For LoopRows:=1 to XDataSet.RecordCount do
    begin
    XDataSet.RecNo:=LoopRows;

    For LoopColumns:=0 to XDataSet.FieldCount-1 do
      begin
      MyRange:= Chr(Ord(XStartCell[1][1])+LoopColumns+1) + inttostr( strtoint(XStartCell[2])+LoopRows);

      if LoopColumns=0 then
        begin
        XWorkSheet.Range[MyRange,MyRange].Value2:=XDataSet.Fields[LoopColumns].AsString;
        XWorkSheet.Range[MyRange,MyRange].NumberFormat:='@';
        end
      else
        begin
        Format := '#,##0.00_);[Red](#,##0.00)';
        XWorkSheet.Range[MyRange,MyRange].Value2:=XDataSet.Fields[LoopColumns].AsCurrency;
        XWorkSheet.Range[MyRange,MyRange].NumberFormat:=Format;
        if XWorkSheet.Range[MyRange, MyRange].ColumnWidth>LargestColumn then
          LargestColumn:=XWorkSheet.Range[MyRange, MyRange].ColumnWidth;
        end;


      end;

    end;
  end;

// Headings
XWorkSheet.Range['A1','A1'].Value2:='General Costing 2004';
XWorkSheet.Range['A2','A2'].Value2:='Product Category Blah';
XWorkSheet.Range['A1','A2'].Font.Size:=14;

XApp.ScreenUpdating[LCID] := True;

// Adjust Column Widths

XStartCell[1]:=Chr(Ord('B'));
XStartCell[2]:='5';
XWorkSheet.Range[XStartCell[1]+XStartCell[2], 'Z99'].ColumnWidth:=LargestColumn+3;

// Close excel and free objects

XWorkSheet.Disconnect;
XWorkBook.Disconnect;
XApp.Disconnect;

XWorkSheet.Free;
XWorkBook.Free;
XApp.Free;
end;
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.