Excel import/export

Hello Servoy pro’s

I am new to the game and I want to initiate the excel import/export functions from a form button instead of making the client sift through menus for it.

I know that I can enable/disable with a method… but can I start the actual process?

Sorry if this concept has already been covered.

-Nick-

Hi Nick

A good place to start is the excellent tutorial by Harjo at Servoy Magazine

http://www.servoymagazine.com/home/2004/11/tip_a_scriptabl.html

This should get you going with scripted exporting and you’ll pick up some good Servoy concepts as well.

Regards

Graham Greensall
Worxinfo Ltd

Thanks for the pointer.
It seems to work well since it exports without error.

The instructed code you reffered to saves the output file as .mer file type.

“.mer
File type:
Format for interchanging spreadsheet/database data
Used in
Filemaker, Excel, WordPerfect for Windows Library”

But It wont open with excel. What do I actualy need to do to view the output?
thanks

save it as *.xls

that also works! :-)

That was my initail thought, so I have already tried changing the method to assign extensions .xls and .txt. With the .xls extension excel launches but nothing opens (not even a blank worksheet). With the .txt extension I see one row of box’s and comma’s which I assume to mean that the file is not actualy saving in .txt format. I also assume the same for .xls.

I am sure you have made it work before so there must be something I am overlooking?

thanks
-Nick-

It looks like your file is empty, or you are exporting just one row???

which method are you using?

I thought that might be it so I did 2 exports from different size foundsets. They produced different size xls files which leads me to believe that the export files are not empty.
the code I am using is this

Global Method “createExportFile”

var form = arguments[0]            //string, formname
var fieldnames = arguments[1]    // boolean, export with ColumnNames at top
var seperator = arguments[2]    // string, choose seperator, for example:  "\t" for TAB "," or ";"
var quotes = arguments[3]       // boolean, ColumnNames with quotes
var columnnames = arguments[4]   // string, ColumnNames seperated by: ";"
var exportfile = ""          //  create a text variable

// set the variable insertquotes to " or nothing
if (quotes == true) var insertquotes = '"'
else var insertquotes = "";

// split the columnames into an Array
var columnArray = columnnames.split(";")
// determine how many columns are used
var columnCount = columnArray.length

// if fieldnames is true than place the ColumnNames at top
if(fieldnames == true)
{  
   for(var x = 0;x <= columnCount - 1;x++)
   {
      if(x != columnCount-1) exportfile += columnArray[x] + seperator
      else exportfile += columnArray[x]
   }
// make an 'Enter'  
exportfile += "\n"
}

// loop thru the foundset
for(var i = 1;i <= forms[form].foundset.getSize();i++)
{
   var record = forms[form].foundset.getRecord(i);
   for(var x=0; x <= columnCount-1;x++)
   {
      if(record[columnArray[x]])
      {
        utils.stringReplace(record[columnArray[x]],'\n', '')
        if(x != columnCount-1) exportfile += insertquotes + record[columnArray[x]] + insertquotes + seperator
         else exportfile += insertquotes + record[columnArray[x]] + insertquotes
      }
      else
      {
         if(x != columnCount-1) exportfile += insertquotes + seperator
         else exportfile += insertquotes
      }
   }
   exportfile += "\n"
}

//open dialog, with a suggested filename
var save = application.showFileSaveDialog('file_export.xls')
if(save != null)
{
   var success = application.writeTXTFile(save,exportfile);
   if (success)
   {
      plugins.dialogs.showInfoDialog('Info', 'The File is saved!','OK');
   }
   else
   {
      plugins.dialogs.showErrorDialog('Warning', 'The File is NOT saved because of an error!','OK');
   }
}

Global Method “startExportFile”

//first argument: give the form name
//show select,returns nothing when canceled 
var form = plugins.dialogs.showSelectDialog('Select','please select a form for export','DataSheets','Software','Equipment');

//second argument: boolean, export with ColumnNames at top
//third argument: string, choose seperator, for example: "\t" for TAB "," or ";"
//fourth argument: boolean, ColumnNames with quotes.
//fifth argument: string, ColumnNames seperated by: ";"

createExportFile(form,true,"\t",true,";");

It doesn’t look like there is any platform specific code in the “createExportMethod” So maybe I am sending blanks but why would the export files be different sizes?
What do you think?

Thanks
-Nick-

just open it with a text-editor (notepad or whatever) and see!

they are just text-files

the smaller founset export results in
box" “box” “box” “box” “box”

the larger foundset export returns
box" “box” “box” “box” “box” “box” “box” “box” “box” “box”

both opened in Notepad
It looks like the box characters correlates with the number of records in the respective exports ie the first export was a 5 record foundset

-Nick-

Nick

Assuming that you can see the data in a text file open Excel then choose File/Open (all text files or all readable documents depending on your OS) - you should find that Excel will recognise the data and show the popup window so that you can select the data seperator.

You could also try saving the file as a .CSV or .TXT and then try opening in Excel.

Graham Greensall
Worxinfo Ltd

As I mentioned above I have tried saving as a .txt file. I have also tried opening the export in notepad and textpad. My last post shows what I see when I open the exports as a text file… “box”
I used the word box to indicate the box character. The quotation marks were also displayed.

So unfortunately we can not assume the data is there or shows up as plain text.

thanks anyway
-Nick-

The plugin that will do the logic (and in the near future even more) for you is the data plugin. More info can be found here: Servoy Components – Plugins, Beans, Web Components, Angular Services & Servoy Developer Consulting.

Hope this adds a little to the discussion.

Hi,

I’ve looked at the export script above, but what I really want to do is bring up the nice export dialog that is available from the file menu, so that the user can select and order the fields to export. It would be great to call this dialog from a button so that I can do other stuff before going to the export. I do exactly this type of thing for the sort dialog, which has the same sort of options as the export dialog and so is very useful.

Is there an equivalent call for the export dialog as there is for the sort dialogue ( ie. controller.sortDialog() ) ?

Thanks,

Nick

but you can do that, the default import/export is a plugin so you can call that one.

When you want more you shall have to wait until the summer. By that time we will have added those dialogs to the data plugin.

Sorry, I’m sure I’m being rather slow here, but how do I call the default plugin? In the plugins dir I can see textxport and excelxport but these just seem to have the functions exportEnabled and importEnabled. How do I actually call the export dialogue?

Nick

Yes indeed, you can’t call the excel plugin (of Servoy) by method.
You only can enable/disable it by method.