There are different ways to export data from a table in Servoy. Lets start from the easiest one.
1. Using Servoy’s default Export menu :
– Servoy has its own menu to perform data Exports and Imports. It supports the xls and text (.csv, .tab, etc) file formats for Exporting and Importing. To use this feature we have to go to the File -> Export menu.
– In some applications we used to remove the menu bar. In that case we are not able to use this menu option. We can use this following code to activate this menu, even if the menu bar is invisible.
plugins.window.getMenuBar().reset(); // Reset the menu bar to default
// Get File Menu
var mnu = plugins.window.getMenuBar().getMenu(plugins.window.getMenuBar().getMenuIndexByText(‘File’));
// Get Export menu item
var mnuExp = mnu.getItem(mnu.getItemIndexByText(‘Export’));
mnuExp.doClick(); // Activate the Export menu
//Hide again the menus
plugins.window.getMenuBar().removeAllMenus();
2. Using dataset and File plugin :
– The same export can be done using dataset and file plugin.
a. CSV export :
// Get the dataset by quering to database var ds = databaseManager.getDataSetByQuery(‘example_data’, ‘select firstname, lastname from employees’, [], 10000); var str = ds.getAsText(‘,’, ‘\n’, ‘”‘, true); var js = plugins.file.showFileSaveDialog(‘export.csv’, ‘Select location to save export file’);
if (!js) return;
// Write the file in the selected location
plugins.file.writeTXTFile(js, str);
b. Excel export :
// Get the dataset by quering to database var ds = databaseManager.getDataSetByQuery(‘example_data’, ‘select firstname, lastname from employees’, [], 10000); var htm = ‘’+ ds.getAsHTML()+’’ var js = plugins.file.showFileSaveDialog(‘export.xls’, ‘Select location to save export file’);
if (!js) return;
// Write the file in the selected location
plugins.file.writeTXTFile(js, htm, ‘UTF8’, ‘application/vnd.ms-excel’);