Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javafx copy table to Excel

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);
like image 674
Marc Rasmussen Avatar asked Mar 19 '13 12:03

Marc Rasmussen


1 Answers

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.

like image 108
Kai Avatar answered Oct 02 '22 20:10

Kai