2

I was using search engine and not just here, and got tired of it; just want a simple answer (or a link) for a simple question:

How do I open a calc sheet and write 123 into cell A1 from Delphi (7) code? (Or any hello worlds for calc?)

Warren P
  • 65,725
  • 40
  • 181
  • 316
Zéiksz
  • 688
  • 1
  • 11
  • 25
  • You could look at this answer on how to open a new sheet: http://stackoverflow.com/questions/5732253/exporting-a-list-to-openoffice-calc-from-delphi – The_Fox Feb 14 '12 at 09:25
  • I checked that one, but it was not detail enough for me - I am total new to openoffice programming. – Zéiksz Feb 14 '12 at 09:28
  • 2
    Problem is, there is no simple answer. Automating OpenOffice is rather complex. You can start here: http://www.openoffice.org/api/ and first read the Developers Guide, especially First Steps, Professional UNO and Advanced UNO. After that, take a look at the IDL Reference. – The_Fox Feb 14 '12 at 09:41
  • -1 this question is kind of lazy. Shows no effort. – Warren P Feb 14 '12 at 15:37
  • I do not think it is lazy. I do think it is simple. And about effort: I shared my knowledge about solving this problem by combining knowledge in comments here, excluding yours what seems to be lazy and showing no effort (to read at least the second comment on this topic, with a full and working source code about the problem). – Zéiksz Feb 15 '12 at 09:13

2 Answers2

4

You can take a look at this demo: http://sourceforge.net/projects/ooomacros/files/Delphi%20OOo/Version%201.2/Delphi_OOo_v12en.zip/download

I tested the Document part a month ago (which works), and I there is also some spreadsheet code in the examples.pas.

André
  • 8,920
  • 1
  • 24
  • 24
4

Ok, after some research and using the informations above for I thank yas a lot, here is a simple answer:

uses part

Uses ComObj, OOoMessages, OOoTools, OOoConstants, OOoXray;

main code

open blank document, write 'hello 123' text into a1 then save it on desktop

procedure HelloWorldExample;
var
  mentesiOpciok,oSheet,oSheets,myCalc : Variant;
begin
  ConnectOpenOffice;
  myCalc:=StarDesktop.loadComponentFromURL('private:factory/scalc', '_blank', 0, dummyArray);
  oSheets:=myCalc.getSheets;
  oSheet:=oSheets.getByIndex(0);
  //oSheet.getCellByPosition(0, 0).SetValue(123);
  oSheet.getCellByPosition(0, 0).SetFormula('hello 123!');

  mentesiOpciok:=CreateProperties(['FilterName', 'MS Excel 97']);
  myCalc.storeToURL('file:///C:/Documents and Settings/Zéiksz/Asztal/calcdoc.xls', mentesiOpciok);
  showMessage('kész :)');
  myCalc.close(true);
  DisconnectOpenOffice();
end;

use getcellbyposition(...).setvalue to set numeric values, or setformula for strings (not really sure, but there is a string in it LOL).

Péter

edit: most useful information I found on the Internet is in this forum: http://www.oooforum.org/forum/viewtopic.phtml?t=4996

Zéiksz
  • 688
  • 1
  • 11
  • 25