title: Copy sheet with JXL in Java
body: <p>I would like to copy a sheet from an existing XLS document to a new one to a new location.<br>
  How could I do this with JXL?  </p>
  
  <pre><code>Workbook w1 = Workbook.getWorkbook(new File("ExistingDocument.xls"), settings);
  
  WritableWorkbook w2 = Workbook.createWorkbook(new File("NewDocument.xls"));
  
  /* So here, I would like copy the first sheet from w1 to the second sheet of w2 ... */
  
  w2.write();
  w2.close();
  
  w1.close();
  </code></pre>
  
  <p><strong>edit:</strong><br>
  <code>w1.getSheet(0).getCell(0, 0)</code> is not a <code>WritableCell</code>, so I couldn't use the <code>copyTo</code> method.<br>
  Is there any way to add a cell/sheet from <code>w1</code> to <code>w2</code> workbook?<br>
  <strong>edit2:</strong><br>
  So do I have to create a writable copy of the workbook to an other file?<br>
  (<strong>edit3:</strong> Or is there any other free lib which can do this?)</p>
  
  <hr>
  
  <p><em><strong>Update:</em></strong></p>
  
  <p>When I run this code, I get <code>jxl.common.AssertionFailed</code> exceptions on line </p>
  
  <pre><code>WritableCellFormat newFormat = new WritableCellFormat(readFormat);
  </code></pre>
  
  <p>If I remove this line and change the code to</p>
  
  <p><code>newCell.setCellFormat(readFormat);</code></p>
  
  <p>then the cell styles aren't copied (the fonts, the cell borders, etc.).</p>
  
  <pre><code>try {
      Workbook sourceDocument = Workbook.getWorkbook(new File("C:\\source.xls"));
      WritableWorkbook writableTempSource = Workbook.createWorkbook(new File("C:\\temp.xls"), sourceDocument);
      WritableWorkbook copyDocument = Workbook.createWorkbook(new File("C:\\copy.xls"));
      WritableSheet sourceSheet = writableTempSource.getSheet(0);
      WritableSheet targetSheet = copyDocument.createSheet("sheet 1", 0);
  
      for (int row = 0; row &lt; sourceSheet.getRows(); row++) {
          for (int col = 0; col &lt; sourceSheet.getColumns(); col++) {
              WritableCell readCell = sourceSheet.getWritableCell(col, row);
              WritableCell newCell = readCell.copyTo(col, row);
              CellFormat readFormat = readCell.getCellFormat();
                      /* exception on the following line */
              WritableCellFormat newFormat = new WritableCellFormat(readFormat);
              newCell.setCellFormat(newFormat);
              targetSheet.addCell(newCell);
          }
      }
      copyDocument.write();
      copyDocument.close();
      writableTempSource.close();
      sourceDocument.close();
  } catch (Exception e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>How could I copy the cell styles too to the new cell?</p>