델파이/프로그래밍 Tip

TExcelApplication 을 이용한 엑셀파일 열기

지병철 2010. 12. 2. 15:06

Early binding:

  var
    WBk: _Workbook;
    WS: _WorkSheet;
    Filename: OleVariant;
  ...
    Filename := 'C:\Test.xls';
    WBk := Excel.Workbooks.Open(Filename, EmptyParam, EmptyParam,
                                EmptyParam, EmptyParam, EmptyParam, 
                                EmptyParam, EmptyParam, EmptyParam, 
                                EmptyParam, EmptyParam, EmptyParam, 
                                EmptyParam, LCID);
    WS := WBk.Worksheets.Item['Sheet1'] as _Worksheet;  
    WS.Activate(LCID);


Late binding:

In late binding, you don't have to specify optional parameters, so you can just do this:

  var
    WBk, WS, SheetName: OleVariant;
  ...
    WBk := Excel.WorkBooks.Open('C:\Test.xls');
    WS := WBk.Worksheets.Item['SheetName'];
    WS.Activate;