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 b. Excel export :
// Get the dataset by quering to database
var ds = databaseManager.getDataSetByQuery(‘example_data’, ‘select firstname, lastname from employees’, [], 10000); var htm = ‘<html>’+ ds.getAsHTML()+'</html>’ var js = plugins.file.showFileSaveDialog(‘export.xls’, ‘Select location to save export file’); if (!js) return; // Write the file in the selected location |
3. Using Jasper Reporting :
– We can also use jasper reporting to export as xls file. For this design a report using iReport. Use the Column Header band to put static texts for the column names. Use the Details band to put the corresponding fields.
– While running the report use the outputFormat parameter as OUTPUT_FORMAT.XLS. This will create an excel file for those fields which are placed in the report design.
According to the situation and requirement we can decide the best option to use for exporting.
|