I am creating an application where I display some data in a table and table. Now I want the user to be able to copy data from the table and paste it into Excel. My copy function works fine, but the result is somewhat dubious.
For example, if I want to copy the following table:

I get the following output in Excel:

if you cannot say, the data is displayed in a single column in Excel.
Does anyone know how I can get the correct output so that each data falls into a separate column when pasted into Excel?
Below is the code I use for copying:
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);
source share