/* * Isomorphic SmartGWT web presentation layer * Copyright 2000 and beyond Isomorphic Software, Inc. * * OWNERSHIP NOTICE * Isomorphic Software owns and reserves all rights not expressly granted in this source code, * including all intellectual property rights to the structure, sequence, and format of this code * and to all designs, interfaces, algorithms, schema, protocols, and inventions expressed herein. * * If you have any questions, please email
. * * This entire comment must accompany any portion of Isomorphic Software source code that is * copied or moved from this file. */ package com.smartgwt.sample.showcase.client.dataintegration.java.grid; import com.smartgwt.client.data.DataSource; import com.smartgwt.client.data.DSRequest; import com.smartgwt.client.widgets.Canvas; import com.smartgwt.client.widgets.IButton; import com.smartgwt.client.widgets.layout.VLayout; import com.smartgwt.client.widgets.layout.HLayout; import com.smartgwt.client.widgets.events.ClickHandler; import com.smartgwt.client.widgets.form.DynamicForm; import com.smartgwt.client.widgets.form.fields.SelectItem; import com.smartgwt.client.widgets.form.fields.BooleanItem; import com.smartgwt.client.widgets.grid.ListGrid; import com.smartgwt.client.widgets.grid.ListGridField; import com.smartgwt.client.types.Autofit; import com.smartgwt.client.types.Alignment; import com.smartgwt.client.types.ExportDisplay; import com.smartgwt.client.types.ExportFormat; import com.smartgwt.client.util.EnumUtil; import com.smartgwt.sample.showcase.client.PanelFactory; import com.smartgwt.sample.showcase.client.ShowcasePanel; import java.util.LinkedHashMap; public class ExcelExportSample implements EntryPoint { public void onModuleLoad() { DataSource worldDSExport = DataSource.get("worldDSExport"); final ListGrid countryList = new ListGrid(); countryList.setWidth(500); countryList.setAlternateRecordStyles(true); countryList.setDataSource(worldDSExport); countryList.setAutoFetchData(true); ListGridField countryName = new ListGridField("countryName", "Country"); ListGridField capital = new ListGridField("capital", "Capital"); ListGridField continent = new ListGridField("continent", "Continent"); countryList.setFields(countryName, capital, continent); countryList.setAutoFitData(Autofit.VERTICAL); countryList.setShowFilterEditor(true); countryList.setAutoFitMaxRecords(10); final DynamicForm exportForm = new DynamicForm(); exportForm.setWidth(300); SelectItem exportTypeItem = new SelectItem("exportType", "Export Type"); exportTypeItem.setWidth("*"); exportTypeItem.setDefaultToFirstOption(true); LinkedHashMap valueMap = new LinkedHashMap(); valueMap.put("csv", "CSV (Excel)"); valueMap.put("xml", "XML"); valueMap.put("json", "JSON"); exportTypeItem.setValueMap(valueMap); BooleanItem showInWindowItem = new BooleanItem(); showInWindowItem.setName("showInWindow"); showInWindowItem.setTitle("Show in Window"); showInWindowItem.setAlign(Alignment.LEFT); exportForm.setItems(exportTypeItem, showInWindowItem); IButton exportButton = new IButton("Export"); exportButton.addClickHandler(new ClickHandler() { public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) { String exportAs = (String) exportForm.getField("exportType").getValue(); boolean showInWindow = (Boolean) exportForm.getField("showInWindow").getValue(); if(exportAs.equals("json")) { // JSON exports are server-side only, so use the OperationBinding on the DataSource DSRequest dsRequestProperties = new DSRequest(); dsRequestProperties.setOperationId("customJSONExport"); dsRequestProperties.setExportDisplay(showInWindow ? ExportDisplay.WINDOW : ExportDisplay.DOWNLOAD); countryList.exportData(dsRequestProperties); } else { // exportAs is either XML or CSV, which we can do with requestProperties DSRequest dsRequestProperties = new DSRequest(); dsRequestProperties.setExportAs((ExportFormat)EnumUtil.getEnum(ExportFormat.values(), exportAs)); dsRequestProperties.setExportDisplay(showInWindow ? ExportDisplay.WINDOW : ExportDisplay.DOWNLOAD); countryList.exportData(dsRequestProperties); } } }); VLayout layout = new VLayout(15); layout.setAutoHeight(); HLayout formLayout = new HLayout(15); formLayout.addMember(exportForm); formLayout.addMember(exportButton); layout.addMember(formLayout); layout.addMember(countryList); layout.draw(); } }