GWT get CellTable contents for printing or export

拜拜、爱过 提交于 2019-12-10 21:17:54

问题


I have a GWT CellTable that gets populated using somewhat of a complicated and tedious process. I want the user to be able to print or export the data from that table.

I would rather not re-render the table contents for export since it is a tedious process.

How can I get the contents of all the rows from all the pages of my CellTable so I can put together a document for printing or export?

I'd be fine with a method of grabbing the actual HTML of the table, or an algorithm for iterating through and grabbing the rendered contents from cells. If someone has a better suggestion, that'd be appreciated as well.


回答1:


It seems there is no viable way to get the CellTable to give me the data for export without re-rendering contents. Since that would cost the same execution time as doing it myself, I resorted to rendering it myself. I used the following code to render HTML and display it in a new popup for printing. The print() method gets called from my Print button.

/**
 * Print in a new popup.
 * http://www.coderanch.com/t/564198/GWT/GWT-injecting-HTML-text-browser
 */
public static native void printHTMLString(String htmlString)/*-{
    var win = $wnd.open("_blank", "Print", "");
    win.document.open("text/html", "replace");
    win.document.write("<html><head></head><body>" + htmlString + "</body></html>");
    win.document.close();
    win.focus();
    var headID = win.document.getElementsByTagName("head")[0];
    var fileref = win.document.createElement("link");
    fileref.setAttribute("rel", "stylesheet");
    fileref.setAttribute("type", "text/css");
    fileref.setAttribute("href", "tables-min.css");
    headID.appendChild(fileref);
    win.print();
}-*/;

private void print() {
    //get the list from the ColumnSortHandler, so it keeps the sorting on the screen
    if (columnSortHandler.getList() == null || columnSortHandler.getList().isEmpty()) {
        Window.alert("Nothing to print");
        return;
    }

    SafeHtmlBuilder b = new SafeHtmlBuilder();
    b.appendHtmlConstant("<table class=\"pure-table\">"
            + "<thead><tr><th>Timestamp</th><th>Type</th><th>User</th>"
            + "<th>Screen</th><th>Client</th></tr></thead>");
    int count = 1;
    for (Record r : columnSortHandler.getList()) {
        b.appendHtmlConstant("<tr" + (count%2==0 ? ">" : " class='pure-table-odd'>"));

        b.appendHtmlConstant("<td>");
        b.appendEscaped(timestampFormat.format(timeStampColumn.getValue(r)));
        b.appendHtmlConstant("</td>");

        b.appendHtmlConstant("<td>");
        b.appendEscaped(typeColumn.getValue(r));
        b.appendHtmlConstant("</td>");

        b.appendHtmlConstant("<td>");
        b.appendEscaped(userColumn.getValue(r));
        b.appendHtmlConstant("</td>");

        b.appendHtmlConstant("<td>");
        b.appendEscaped(screenColumn.getValue(r));
        b.appendHtmlConstant("</td>");

        b.appendHtmlConstant("<td>");
        b.appendEscaped(clientColumn.getValue(r));
        b.appendHtmlConstant("</td>");


        b.appendHtmlConstant("</tr>");

        count++;
    }
    b.appendHtmlConstant("</table>");
    printHTMLString(b.toSafeHtml().asString());
}



回答2:


To export XLS use the class below.

To print use gwt-print-it.

The class below do that without server side.

public class TableToExcel {
    public static final <T> void save(final CellTable<T> table, String filename) {
        final AnchorElement a = Document.get().createAnchorElement();
        a.setHref("data:application/vnd.ms-excel;base64," + base64(table.getElement().getString()));
        a.setPropertyString("download", (filename.endsWith(".xls") || filename.endsWith(".xlsx")) ? filename : filename + ".xls");

        Document.get().getBody().appendChild(a);
        Scheduler.get().scheduleEntry(new ScheduledCommand() {
            @Override
            public void execute() {
                click(a);
                a.removeFromParent();
            }
        });
    }

    private static native void click(Element elem) /*-{
        elem.click();
    }-*/;

    public static native String base64(String data) /*-{
        return btoa(data);
    }-*/;
}



回答3:


You can use getInnerHTML or getInnerText

for(int i=0; i<cellTable.getRowCount();i++)
{
    TableRowElement rowElement = cellTable.getRowElement(i);
    for(int j =0; j<rowElement.getCells().getLength(); j++)
    {
        //System.out.println( rowElement.getCells().getItem(j).getInnerHTML() );
        System.out.println( rowElement.getCells().getItem(j).getInnerText() );

    }       
}



回答4:


You could try the gwt-table-to-excel module.

The main point is that excel and other modern spreadsheet knows how to render an html table, so just open the dynamically built html table with the correct mime-type, and that's all.

I never used it, but the description sounds good.



来源:https://stackoverflow.com/questions/19712559/gwt-get-celltable-contents-for-printing-or-export

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!