Excel Export/Import Problems

Hi

I’m working on a project where a excel spreadheet is stored in the database: one sheet in each record. What I’m trying to achieve is: write out the excel sheet on the filesystem, startup excel, let the user edit the sheet, and after the user closes excel: read back the sheet in the current record. The problem is that after application.executeProgram(…) the reading of the file (and the delete of the file) does not work. It works once (both the reading/writing and the delete) after the switch from designmode to client mode, but subsequent calls do not work. A code snippet:

workfile="C:/TEMP/workfile.xls"
var success = plugins.file.writeFile(workfile, xls_data)
if (!success) {
   var thePressedButton = plugins.dialogs.showInfoDialog('Warning', 'FileWrite Failed' ,'OK');
}

//application.executeProgram("rundll32", "url.dll,FileProtocolHandler",workfile) 
application.executeProgram("C:/Program Files/Microsoft Office/OFFICE11/EXCEL.EXE", workfile) 

xls_data = plugins.file.readFile(workfile)

if (!plugins.file.deleteFile(workfile)) {
	   var thePressedButton = plugins.dialogs.showInfoDialog('Warning', 'FileDelete failed','OK');
}

I tried serveral different code variations, RUNDLL32, sleeps etc. Nothing seem to make it work. Is there something I am missing here?

Thanks,

Gert-Jan Aaftink

Hi Gert-jan

welcome to Servoy.

there are a few options:

1.) the readFile is executed directly after application.executeProgram

so you have to close & save the excel sheet manually
and you have to put that piece of code into a separate button, that will load the file back again into the database.

2.) You can work with a scheduler, which is checking if the file is still open, and after that save it back to the database (is a little bit complicated, to explain here :-o )

3.) look at the data-plugin of IT2be. (www.it2be.com) There you can export en import files right into, or out of EXCEL.

Hope this helps

Harjo, thanks for your reply. But:

1.) the readFile is executed directly after application.executeProgram

so you have to close & save the excel sheet manually
and you have to put that piece of code into a separate button, that will load the file back again into the database.

Of cource the excel sheet has to be closed by the user first. But if I read the developer documentatation corectly: The execute.program function will stop the running script until the external program has been terminated. To me it looks like: the script will halt until excel is closed, then it will do the next steps. So if that is so: why is my code not correct?

I will work together with Marcel on this, but being able to read and write the file is essential for me to make this sollution work.

that the Servoy will halt, until the program is closed has never worked for me.

It does, work when call some kind of batch file, or VBscript en does stuff. But not with Word or Excel!

It looks like you are on Windows.
Have you considered my OLE plugin ?
It will allow you to edit your Office documnts in-form.

See:
http://forum.servoy.com/viewtopic.php?t=5443
and
http://forum.servoy.com/viewtopic.php?t=5164

Maarten Berkenbosch from Servoy investigated the matter, and came up with a rather surprising result: Excel will keep a file open from 1 to 30 seconds after the application itself is closed. So to do what I intended to do: use excel as a dataentry source, you have to write tempfiles instead of normal files. This will prevent the need to delete the file after excel is closed (temp files will be cleared automatically when servoy closes), and as a new tempfile is used for every edit, the problem of overwriting the workfile no longer exists.

Thanks Maarten for solving this one!