How can I run an Openoffice application from Delphi?
For Excel I use the following procedure:
procedure TfmRapportBase.actExportExcelExecute(Sender: TObject);
var
XLApp, Sheet: Variant;
i, r, c: integer;
rng, fmt : string;
f : TField;
begin
FBusy := true;
//hh waarom 3x ?
Application.ProcessMessages;
Application.ProcessMessages;
FDataClone := TClientDataset.Create(self);
try
CloneDataset;
XLApp := CreateOleObject('Excel.Application');
odExcel.InitialDir := dmDatabase.UserSjablonen;
// nieuw werkblad in excel aanmaken
if (odExcel.InitialDir <> '') and odExcel.Execute
then XLApp.Workbooks.Add(odExcel.FileName)
else XLApp.Workbooks.Add(xlWBatWorkSheet);
// werkblad een naam geven
Sheet := XLApp.Workbooks[1].WorkSheets[1];
if Manager.DescribeData <> ''
then Sheet.Name := Copy(Manager.DescribeData, 1, 31); // Maximaal 31
karakters!
r := 1;
Sheet.Cells[r,1] := Manager.DescribeData;
if Manager.ProduktOmschrijving <> '' then Sheet.Cells[r+1,1] :=
Manager.ProduktOmschrijving;
if Manager.Filtering
then Sheet.Cells[r,4] := Manager.FilterDescription;
// PM r op 4 gezet ivm ProduktOmschrijving
r := 4;
c := 1;
for i := 0 to FDataClone.FieldCount-1 do
begin
f := FDataClone.Fields[i];
if f.Visible
then begin
rng := ColumName(c);
Sheet.Columns[rng+':'+rng].Select;
//TODO: column format
fmt := ''; //default
if (f is TIntegerField)
then fmt := '0'
else if (f is TNumericField)
then fmt := Fmt4Excel((f as TNumericField).DisplayFormat)
else if (f is TStringField)
then fmt := '@';
if fmt = '#.0' then
fmt := '#.##0,0'
else
if fmt = '#.#0' then
fmt := '#.##0,00'
else
if fmt = '#.##0' then
fmt := '#.##0,000'
else
if fmt = '#.###0' then
fmt := '#.##0,0000'
else
if fmt = '#.####0' then
fmt := '#.##0,00000'
else
if fmt = '#.#####0' then
fmt := '#.##0,000000';
if fmt <> ''
then xlapp.selection.NumberFormat := fmt;
Sheet.Cells[r,c] := f.DisplayName;
inc(c);
end;
end;
rng := ColumName(c); // overbodigjes
Sheet.PageSetup.PrintTitleRows := 'A1:'+rng+inttostr(r);
ggProgress.Visible := true;
try
FDataClone.First;
inc(r);
ggProgress.MaxValue := FDataClone.RecordCount;
// Manager.ClientDataSet.DisableControls;
// OldRecNo := Manager.ClientDataSet.RecNo;
FDataClone.First;
while Fbusy and not FDataClone.Eof
do begin
ggProgress.Progress := FDataClone.Recno;
Application.ProcessMessages;
c := 1;
for i := 0 to FDataClone.FieldCount-1 do
if FDataClone.Fields[i].Visible
then begin
try
Sheet.Cells[r,c] := GetExcelValue(FDataClone.Fields[i]);
except
on e: Exception do Sheet.Cells[r,c] := e.Message;
end;
inc(c);
end;
inc(r);
FDataClone.Next;
end;
Sheet.Columns.Autofit;
if Fbusy
then ModalResult := mrOk; // geen fouten en EOF, dan dialoog dicht
finally
XLApp.Visible := True;
end;
finally
FreeAndNil(FDataClone);
ggProgress.Visible := false;
FBusy := false;
end;
end;
With regards
M.J. Dusselaar