Entwickler-Ecke

Sonstiges (Delphi) - Daten aus Open Office Datei auslesen?


Seven of Nine - So 05.09.10 07:35
Titel: Daten aus Open Office Datei auslesen?
Ich bin auf der Suche nach Informationen, oder noch besser eine Unit, wie man ein Open-Office "Calc"-Dokument (*.ods) unter Delphi ausliest.

Ich benötige folgende Grund-Funktionen:
- Inhalt einzelner Zellen auslesen (z.B. "A1" auslesen)
- letzte beschriebene Zelle eines Calc-Dokumentes herausfinden

herzlichen Dank im voraus für jegliches Feedback & lG
Martin


Stundenplan - So 05.09.10 11:47

Hallo!

Das [http://www.delphi-forum.de/topic_Delphi+und+OpenOfficeLoesung_99982,0.html] könnte dir weiterhelfen!

Viele Grüße,
Stundenplan.


Seven of Nine - Sa 18.09.10 06:05

Durch den Hinweis von Stundenplan, sowie etwas weiteres stöbern in den Tiefen des WWW habe ich inzw. eine Lösung die meinen Bedarf soweit abdeckt.

ich bin mir sicher, dass man das noch sauberer coden kann, so funktioniert's aber zumindest schon mal Recht passabel


zuerst definieren wir einige gloabale Variablen die wir später noch häufiger benötigen


Delphi-Quelltext
1:
2:
3:
  //OpenOffice: Global-Vars
  Service, Desktop, Document : Variant;
  p_Doc_Type : String;


und hier der Code einiger grundlegende Funktionen zur Ansteuerung von OpenOffice-CALC



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:
procedure TImportTIForm.SetDocType(s:string);
var aResult:String;
begin
  aResult := '';
  if (Document.SupportsService('com.sun.star.sheet.SpreadsheetDocument')) then
    aResult := 'scalc';
  if (Document.SupportsService('com.sun.star.text.TextDocument')) then
    aResult := 'swriter';
  if (Document.SupportsService('com.sun.star.drawing.DrawingDocument')) then
    aResult := 'sdraw';
  if (Document.SupportsService('com.sun.star.formula.FormulaProperties')) then
    aResult := 'smath';
  if (aResult = ''then aResult := 'unknown';

  p_Doc_Type:= aResult;
end;

function TImportTIForm.ToOOPath(FilePath:String):String;
var ReturnSTR : string;
    i : integer;
begin
  ReturnSTR := 'file:///';
  while (POS('\',FilePath)>0do
  begin
    i := POS('\',FilePath);
    delete(FilePath,i,1);
    Insert('/',FilePath,i);
  end;
  ReturnSTR := ReturnSTR + FilePath;
  result := ReturnSTR;
end;


function TImportTIForm.OpenOODoc(Filename:string):boolean;
var ReturnValue : boolean;
begin
  Filename := ToOOPath(Filename);
  try
    Document := Desktop.LoadComponentFromURL(Filename,'_blank',0,VarArrayCreate([0, - 1], varVariant));
    ReturnValue := true;
    SetDocType('dummy');
  except
    ReturnValue := false;
  end;
  result := ReturnValue;
end;


function TImportTIForm.CreateOODoc(DocType:Integer):boolean;  //DocType -> 0:Calc ; 1:Writer; 2: Draw; 3: Math
Var
  URL : String;
  ReturnValue : boolean;

begin
  Case DocType of
    0: URL := 'private:factory/' + 'scalc';
    1: URL := 'private:factory/' + 'swriter';
    2: URL := 'private:factory/' + 'sdraw';
    3: URL := 'private:factory/' + 'smath';
    else begin
           Result := False;
           exit;
         end;
  end;
  //----------------
  try
    Document := desktop.LoadComponentFromURL(URL,'_blank'0, VarArrayCreate([0, - 1], varVariant));
    ReturnValue := true;
  except
    ReturnValue := false;
  end;
  result := ReturnValue;
end;


function TImportTIForm.SaveOODoc(Filename:string):boolean;
Var
  VariantArr : Variant;
  ReturnValue : boolean;
begin
  try
    VariantArr := VarArrayCreate([00], varVariant);
    Document.StoreToURL(ToOOPath(Filename), VariantArr);
    ReturnValue := true;
  except
    ReturnValue := false;
  end;
  result := ReturnValue;
end;



function TImportTIForm.CloseOODoc(Filename:string):boolean;
begin
  Document.Close(True);
end;





function TImportTIForm.GetOOCellContent(worksheetNr:Integer;aCol,aRow:integer):String;
var sh,bm : Variant;
begin
  sh := Document.Sheets.getByIndex(worksheetNr);
  bm := sh.getCellbyPosition(aCol,aRow);
  result := bm.getString;
end;


function TImportTIForm.WriteOOCellContent(worksheetNr:Integer;aCol,aRow:integer;AString:String):boolean;
var sh,bm,tc : Variant;
begin
  sh := Document.Sheets.getByIndex(worksheetNr);
  bm := sh.getCellbyPosition(aCol,aRow);
  bm.setString(AString);
end;


lG Martin