smartgwt导出listgrid数据源到excel文件

时间:2011-09-06 09:51:39

标签: smartgwt

我想将ListGrid DataSource导出到Excel文件,我正在使用excel文件,因为我想填充excel文件中的listgrid字段。有谁知道怎么做。这对我很有帮助。

感谢Ankit。

3 个答案:

答案 0 :(得分:3)

作为一名业余爱好者,即使我想做同样的事情,但在SmartGwt中只有很少的选择。只有SmartGwt Pro / Power / EE版本具有此类功能。但是我已经找到了解决方案并附上了代码,因为还没有任何示例,如果有人像我一样对GWT很新,你将不得不花费一整个时间来解决这个问题。以下示例代码简单易懂。

 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;

    }
}

以上代码有效且易于理解。我已经使用了GWT的默认测试项目结构来让所有人理解。用于ListGrid部分的代码是从smartgwt showcase ListGrid JSON Integration中使用的。如果您对所提供的解决方案更加清晰,请告诉我。

答案 1 :(得分:0)

您可以设置一个seperat(非rpc)Servlet,并将必须通过http POST导出的数据发送到servlet(例如通过GWT FormPanel)。在服务器端,您可以使用Apache POI创建一个exel文件并提供下载。

SmartGwt的Pro / Power / EE包含这样的功能(参见this展示)。

答案 2 :(得分:-1)

您无需手动执行此操作....已经为您完成了....

检查一下:

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

这是一个有效的例子