Autor Beitrag
D. Annies
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1843

windows 7
D6 Enterprise, D7 Pers und TD 2006
BeitragVerfasst: Di 08.06.10 16:49 
Hi, Delpher,
ich habe hier eine Lösung, wie man aus Delphi OpenOffice-Calc ansteuern kann!

ausblenden volle Höhe Delphi-Quelltext
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:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
unit DelOOcalc;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, ComObj;

type
  TForm1 = class(TForm)
    Button1: TButton;
    StringGrid1: TStringGrid;
    procedure Button1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure leg_los;
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}




const
  FONT_WEIGHT :      record
                       DONTKNOW,
                       THIN,
                       ULTRALIGHT,
                       LIGHT,
                       SEMILIGHT,
                       NORMAL,
                       SEMIBOLD,
                       BOLD,
                       ULTRABOLD,
                       BLACK       : extended;
                     end = (DONTKNOW: 0.000000; THIN: 50.000000; ULTRALIGHT: 60.000000;
                            LIGHT: 75.000000; SEMILIGHT: 90.000000; NORMAL: 100.000000; SEMIBOLD:110.000000;
                            BOLD: 150.000000; ULTRABOLD: 175.000000; BLACK: 200.000000);

  CELL_HORI_JUSTIFY: record
                       STANDARD,
                       LEFT,
                       CENTER,
                       RIGHT,
                       BLOCK: Integer;
                     end = (STANDARD: 0; LEFT: 1; CENTER: 2; RIGHT: 3; BLOCK: 4);

  CELL_VERT_JUSTIFY: record
                       STANDARD,
                       TOP,
                       CENTER,
                       BOTTOM: integer;
                     end = (STANDARD: 0; TOP: 1; CENTER: 2; BOTTOM: 3);

// Hier ist eine neue Klasse definiert, um nicht immer alles schreiben zu müssen:

type
  TOpenOffice = class
    public
      servicemanager: Variant;
      desktop: Variant;
      corereflection: Variant;
      dispatcherhelper: Variant;
    constructor Create;
  end;

constructor TOpenOffice.Create;
begin
  inherited Create;
  servicemanager   := CreateOleObject('com.sun.star.ServiceManager');
  desktop          := servicemanager.createInstance('com.sun.star.frame.Desktop');
  corereflection   := servicemanager.createInstance('com.sun.star.reflection.CoreReflection');
  dispatcherhelper := servicemanager.createInstance('com.sun.star.frame.DispatchHelper');
end;

//So, und jetzt noch ein Beispiel zur Steuerung von Calc. Ich mixe rein OLE und UNO (weiter unten):

procedure TForm1.leg_los;
var
  _calc: Variant;
  _sheet: Variant;
  _args: Variant;
  _arg: Variant;
  _range: Variant;
  _j: Integer;
  i, j: smallint;
begin
  with TOpenOffice.Create do begin
    try
      _calc := desktop.LoadComponentFromURL('private:factory/scalc',
                 '_blank'0, VarArrayCreate([0, - 1], varVariant));
      _sheet := _calc.getSheets.getByIndex(0);

      // Überschriften eintragen
      _sheet.getCellByPosition (00).setFormula('Objektnummer');
      _sheet.getCellByPosition (10).setFormula('Fremdschlüssel');

      // Daten eintragen
{     _sheet.getCellByPosition (0, 1).setFormula('12345');
      _sheet.getCellByPosition (1, 1).setFormula('ABC');
      _sheet.getCellByPosition (0, 2).setFormula('98765');
      _sheet.getCellByPosition (1, 2).setFormula('Test');   }


      for j := 1 to stringgrid1.rowcount - 1 do
        for i := 1 to stringgrid1.ColCount - 1 do
          _sheet.getCellByPosition(i,j).setFormula(stringgrid1.cells[i,j]);

      // Allgemeine Formatierung
      _range := _sheet.getCellRangeByPosition(0012);
      _range.setpropertyValue('VertJustify', CELL_VERT_JUSTIFY.TOP); //
         //Damit bei Textfeldern alles oben steht

      // Überschriften formatieren
      _range := _sheet.getCellRangeByPosition(00140);
      _range.setpropertyValue('CharWeight', FONT_WEIGHT.BOLD);
      _range.setpropertyValue('HoriJustify', CELL_HORI_JUSTIFY.CENTER);

      // Spalten formatieren
      for _j := 0 to 1 do begin
        if (_j in [0]) then begin
          _range := _sheet.getCellRangeByPosition(_j, 1, _j, 2);
          _range.setpropertyValue('HoriJustify', CELL_HORI_JUSTIFY.RIGHT);
        end;
        if (_j in [1]) then begin
          _range := _sheet.getCellRangeByPosition(_j, 1, _j, 2);
          _range.setpropertyValue('HoriJustify', CELL_HORI_JUSTIFY.LEFT);
        end;
      end;

      // Optimale Breite durch UNO
      dispatcherhelper.executeDispatch(desktop.ActiveFrame,
        '.uno:SelectAll'''0, VarArrayCreate([0, - 1], varVariant));
      _args := VarArrayCreate([00], varVariant);

      corereflection.forName('com.sun.star.beans.PropertyValue').createObject(_arg);
      _arg.Name  := 'aExtraWidth';
      _arg.Value := 200;
      _args[0]   := _arg;
      dispatcherhelper.executeDispatch(desktop.ActiveFrame,
        '.uno:SetOptimalColumnWidth'''0, _args);
      _arg.Name  := 'ToPoint';
      _arg.Value := '$A$2';
      _args[0]   := _arg;
      dispatcherhelper.executeDispatch(desktop.ActiveFrame,
        '.uno:GoToCell'''0, _args);

    finally
      Free;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  showmessage('hi');
  leg_los;
end;

procedure TForm1.FormActivate(Sender: TObject);
var i, j : smallint;
begin
  with stringgrid1 do
  begin
    for i := 1 to 5 do
      for j := 1 to 5 do
        cells[i,j] := inttostr(4 * (i+j));
  end;
end;

end.


Es funktioniert! Aber leider weiß ich nicht, ob das so die beste Reihenfolge im Delphi-Code ist.
Für einen Beautyfier wäre ich also dankbar! :))

Viele Grüße, Detlef

_________________
ut vires desint, tamen est laudanda voluntas