DelphiでOpenOffice Calcのドキュメントを作成して、値を設定してからファイルに保存するサンプル

Delphi XE6とOpenOffice 4.1.0で動作を確認した。

DelphiでOpenOffice Calcのドキュメントを作成して、値を設定してからファイルに保存する

uses Vcl.OleAuto;

var
  ServiceManager: OleVariant;
  Desktop: OleVariant;
  Document: OleVariant;
  Sheet: Variant;
  Cell: Variant;
begin
  try
    // OpenOfficeを起動する
    ServiceManager := CreateOleObject('com.sun.star.ServiceManager');
    Desktop := ServiceManager.createInstance('com.sun.star.frame.Desktop');

    // 新規ドキュメントの作成
    Document := Desktop.LoadComponentFromURL('private:factory/scalc', '_blank',
      0, VarArrayCreate([0, -1], varVariant));

    // セルに値を入れる
    Sheet := Document.Sheets.getByName('Sheet1');
    Cell := Sheet.getCellByPosition(2, 3);
    Cell.String := 'セルC4';

    // ファイルに保存する
    Document.StoreAsURL('file:///C:/test/sample.ods',
      VarArrayCreate([0, -1], varVariant));

    // OpenOfficeを閉じる
    Desktop.Terminate;
  except
    on E: Exception do
      ShowMessage(E.Message);
  end;
  Desktop := Unassigned;
  ServiceManager := Unassigned;
end;

プロパティを作成する関数

function MakeProperty(PropertyName:string; PropertyValue: Variant): Variant;
begin
  Result := ServiceManager.Bridge_GetStruct
    ('com.sun.star.beans.PropertyValue');
  Result.Name := PropertyName;
  Result.Value := PropertyValue;
end;

新規ドキュメントを非表示で作成する

Properties := VarArrayCreate([0, 0], varVariant);
Properties[0] := MakeProperty('Hidden', True); // 非表示
Document := Desktop.LoadComponentFromURL('private:factory/scalc', '_blank',
  0, Properties);

罫線を引く

// シートを選択する
Sheet := Document.Sheets.getByIndex(0);

// 範囲を指定する
Range := Sheet.getCellRangeByName('B2:D4');

// 罫線を設定する
BorderLine := ServiceManager.Bridge_GetStruct
  ('com.sun.star.table.BorderLine');
BorderLine.Color := 255;
BorderLine.LineDistance := 0;
BorderLine.InnerLineWidth := 0;
BorderLine.OuterLineWidth := 35;

// 罫線を適用する
Range.leftBorder := BorderLine;
Range.rightBorder := BorderLine;
Range.topBorder := BorderLine;
Range.bottomBorder := BorderLine;

Microsoft Excel 97-2003形式でファイルに保存する

Properties := VarArrayCreate([0, 0], varVariant);
Properties[0] := MakeProperty('FilterName', 'MS Excel 97');
Document.StoreAsURL('file:///C:/test/sample.xls',
  Properties);

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください