开发者

smartgwt export listgrid datasource to excel file

I want to export my ListGrid DataSource to Excel file and I am using excel file because i want to populate my listgrid fields in excel file. does any one know how to do i开发者_开发技巧t. it will be great help to me.

Thanks Ankit.


Being an amateur, even I wanted to do the same thing but had very few option in SmartGwt. Only the SmartGwt Pro/Power/EE version has such functionality. However I have found a solution to this and the code is attached as there are no examples out there yet and if anyone is new to GWT like me, you would have to spend a whole time cracking this. The following sample code is simple and easy to understand.

 public class GridToCSV implements EntryPoint {



    /**
     * Create a remote service proxy to talk to the server-side Greeting
     * service.
     */
    private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);

    public void onModuleLoad() {

        DataSource dataSource = new DataSource();
        dataSource.setDataFormat(DSDataFormat.JSON);
        dataSource.setDataURL("data/countries_small.js");

        DataSourceTextField nameField = new DataSourceTextField("name", "Name");
        DataSourceTextField populationfield = new DataSourceTextField(
                "population", "Population");
        DataSourceTextField areaField = new DataSourceTextField("total_area",
                "Total Area");
        DataSourceTextField governmentField = new DataSourceTextField(
                "government", "Government");

        dataSource.setFields(nameField, populationfield, areaField,
                governmentField);

        final ListGrid grid = new ListGrid();
        grid.setDataSource(dataSource);
        grid.setWidth100();
        grid.setHeight(150);
        grid.setAutoFetchData(true);

        IButton button = new IButton("Export CSV");
        button.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                StringBuilder exportedCSV = exportCSV(grid);
                System.out.println(exportedCSV);
                sendTOServer(exportedCSV);
                // Window.Location(listGrid.exportData());
            }

        });
        //grid.draw();
        //button.draw();

        VLayout layout = new VLayout(); layout.addMember(button);
        layout.addMember(grid); 
        layout.addMember(button);
        layout.setWidth100();
        layout.draw();


    }

    private StringBuilder exportCSV(ListGrid listGrid) {
        StringBuilder stringBuilder = new StringBuilder(); // csv data in here

        // column names
        ListGridField[] fields = listGrid.getFields();
        for (int i = 0; i < fields.length; i++) {
            ListGridField listGridField = fields[i];
            stringBuilder.append("\"");
            stringBuilder.append(listGridField.getName());
            stringBuilder.append("\",");
        }
        stringBuilder.deleteCharAt(stringBuilder.length() - 1); // remove last
                                                                // ","
        stringBuilder.append("\n");

        // column data
        ListGridRecord[] records = listGrid.getRecords();
        for (int i = 0; i < records.length; i++) {
            ListGridRecord listGridRecord = records[i];
            ListGridField[] listGridFields = listGrid.getFields();
            for (int j = 0; j < listGridFields.length; j++) {
                ListGridField listGridField = listGridFields[j];
                stringBuilder.append("\"");
                stringBuilder.append(listGridRecord.getAttribute(listGridField
                        .getName()));
                stringBuilder.append("\",");
            }
            stringBuilder.deleteCharAt(stringBuilder.length() - 1); // remove
                                                                    // last ","
            stringBuilder.append("\n");
        }
        return stringBuilder;
    }

    private void sendTOServer(StringBuilder csvText) {
        // TODO Auto-generated method stub
        String csv = csvText.toString();
        final HTML serverResponseLabel = new HTML();
        greetingService.greetServer(csv, new AsyncCallback<String>() {
            public void onFailure(Throwable caught) {

            }

            public void onSuccess(String result) {
                // dialogBox.setText("Remote Procedure Call");
                serverResponseLabel.removeStyleName("serverResponseLabelError");
                serverResponseLabel.setHTML(result);
                 Window.open(result, "_blank", "");
                // dialogBox.center();
                // closeButton.setFocus(true);
            }
        });
    }

}

GreetingService.java

    @RemoteServiceRelativePath("greet")
public interface GreetingService extends RemoteService {
    String greetServer(String name) throws IllegalArgumentException;
}

GreetingServiceAsync.java

    public interface GreetingServiceAsync {
    void greetServer(String input, AsyncCallback<String> callback)
            throws IllegalArgumentException;
}

GreetingServiceImpl.java

    @SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
        GreetingService {

    String url;

    public String greetServer(String input) throws IllegalArgumentException {
        FileWriter fw;
        try {
            fw = new FileWriter("WriteTest.csv");
            PrintWriter pw = new PrintWriter(fw);
            // Write to file for the first row
            url = "http://127.0.0.1:8888/WriteTest.csv";
            pw.print(input);
            pw.flush();
            // Close the Print Writer
            pw.close();
            // Close the File Writer
            fw.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return url;

    }
}

The above code works and its easy and simple to understand. I have used the default Test Project Structure of GWT for all to understand. The code used for the ListGrid part was used from the smartgwt showcase ListGrid JSON Integration. Kindly let me know if you need anymore clarity for the solution provided.


You could set up a seperat (non rpc) Servlet and send the data which has to be exported via http POST to the servlet (e.g. via a GWT FormPanel). On server side you can create an exel file with Apache POI and provide it for downloading.

Pro/Power/EE of SmartGwt has such a function included (see this showcase).


You do not need to do this manually.... It has been done for you....

check this out:

http://www.smartclient.com/smartgwtee/showcase/#excel_export

It is a working example

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜