title: Javafx copy table to Excel
body: <p>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.</p>
  
  <p>For instance, if I want to copy the following table:</p>
  
  <p><img src="https://i.stack.imgur.com/fSizZ.jpg" alt="My table"></p>
  
  <p>I get the following output in Excel:</p>
  
  <p><img src="https://i.stack.imgur.com/huxEn.jpg" alt="enter image description here"></p>
  
  <p>if you can't tell, the data is displayed in a single column in Excel.</p>
  
  <p>Does anyone know, how I can achieve the right output, so that each data goes into a separate column, when pasted into Excel?</p>
  
  <p>The following is the code I use to copy:</p>
  
  <pre><code>table.getSelectionModel().setCellSelectionEnabled(true);
  table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
  MenuItem item = new MenuItem("Kopiér");
  item.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
      @Override
      public void handle(ActionEvent event) {
          ObservableList&lt;TablePosition&gt; 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&lt;ActionEvent&gt;() {
      @Override
      public void handle(ActionEvent event) {
          String rowContent = "";
          ObservableList&lt;TablePosition&gt; 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 &lt; table.getColumns().size(); i++) {
                  rowContent = rowContent +" "+table.getColumns().get(c).getCellData(row);
                  if (c &lt; 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);
  </code></pre>