Javafx copy table to Excel


Question

I am creating an application, where I display some data in a chart and a table. Now I want the user, to be able, to copy the data, from the table and insert it into Excel. My copy function works fine, but the output is somewhat questionable.

For instance, if I want to copy the following table:

My table

I get the following output in Excel:

enter image description here

if you can't tell, the data is displayed in a single column in Excel.

Does anyone know, how I can achieve the right output, so that each data goes into a separate column, when pasted into Excel?

The following is the code I use to copy:

table.getSelectionModel().setCellSelectionEnabled(true);
table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
MenuItem item = new MenuItem("Kopiér");
item.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        ObservableList<TablePosition> posList = table.getSelectionModel().getSelectedCells();
        int old_r = -1;
        StringBuilder clipboardString = new StringBuilder();
        for (TablePosition p : posList) {
            int r = p.getRow();
            int c = p.getColumn();
            Object cell = table.getColumns().get(c).getCellData(r);
            if (cell == null)
                cell = "";
            if (old_r == r)
                clipboardString.append('\t');
            else if (old_r != -1)
                clipboardString.append('\n');
            clipboardString.append(cell);
            old_r = r;
        }
        final ClipboardContent content = new ClipboardContent();
        System.out.println(clipboardString);
        content.putString(clipboardString.toString());
        Clipboard.getSystemClipboard().setContent(content);
    }
});
MenuItem item2 = new MenuItem("Kopier række");
item2.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        String rowContent = "";
        ObservableList<TablePosition> posList = table.getSelectionModel().getSelectedCells();
        System.out.println(rowContent);
        for (TablePosition p : posList) {
            int c = 1;
            int row = p.getRow();
            System.out.println("c " +c);
            System.out.println("row "+row);
            for (int i = 1; i < table.getColumns().size(); i++) {
                rowContent = rowContent +" "+table.getColumns().get(c).getCellData(row);
                if (c < 13) {
                    c++;            
                }   
            }       
        }
        final ClipboardContent allContent = new ClipboardContent();
        allContent.putString(rowContent.toString());
        Clipboard.getSystemClipboard().setContent(allContent);
        System.out.println(allContent.toString());
        rowContent = "";
    }
});
ContextMenu menu = new ContextMenu();
menu.getItems().addAll(item,item2);
1
4
5/11/2017 9:17:53 PM

Accepted Answer

Add a tab \t between the columns. Excel should recognize this and use a new cell. It works when writing it in Notepad by using tabs and pasting it in excel.

6
3/19/2013 1:04:00 PM

My library solution method signature:

TableViewUtils.copyToClipboard(TableView<T> table, ObservableList<T> rows)

or

// add 'Copy table' and 'Copy row' menu items
TableViewUtils.addCopyToClipboardMenuItem(TableView<T> table)

enter image description here


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon