網頁

2014/02/18

Excel Component 的寫作技巧

在全域變數區可設定一個 LCID 變數
var _LCID: Cardinal;

在 TForm 上放置 ExcelApplication, ExcelWorkbook, ExcelWorksheet 三個元件。

開啟 EXCEL + 開啟活頁簿 + 指定工作表:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
var
  _WBK: _Workbook;
  _IndexSheet: Integer
begin
  _LCID := GetUserDefaultLCID();
  _WBK := ExcelApplication.Workbooks.Open(
          {Filename        =}aFileName,
          {UpdateLinks     =}EmptyParam,
          {ReadOnly        =}EmptyParam,
          {Format          =}EmptyParam,
          {Password        =}EmptyParam,
          {WriteResPassword=}EmptyParam,
          {IgnoreReadOnly  =}EmptyParam,
          {Origin          =}EmptyParam,
          {Delimiter       =}EmptyParam,
          {Editable        =}EmptyParam,
          {Notify          =}EmptyParam,
          {Converter       =}EmptyParam,
          {AddToMru        =}EmptyParam,
          {Local           =}EmptyParam,
          {CorruptLoad     =}EmptyParam,
          {LCID            =}_LCID);
  ExcelWorkbook.ConnectTo(_WBK);
  ExcelWorksheet.ConnectTo(ExcelWorkbook.Worksheets[_IndexSheet] as _Worksheet);
end;

關閉活頁簿 + 關閉 Excel:
1
2
3
4
5
6
7
8
9
begin
  ExcelWorkbook.Close(
    {SaveChanges  =}EmptyParam,
    {Filename     =}EmptyParam,
    {RouteWorkbook=}EmptyParam,
    {LCID         =}_LCID);
  ExcelApplication.Quit;
  ExcelApplication.Disconnect;
end;

沒有留言:

張貼留言