title: Apache POI copy one row from Excel file to another new Excel File
body: <p>I am using JAVA 8 and Apache POI 3.17. I have an Excel file and i want to keep only few lines and delete the others. But my Excel have 40K rows and deleting them one by one is quite long (nearly 30 min :/ )</p>
  
  <p>So i try to change my way of doing it. Now i think it's better to only take rows that i need in the excel source and copy to another new one. But what i have tried so far is not efficient.</p>
  
  <p>I have all my rows and want to keep in a List. But this not working and create me a blank excel :</p>
  
  <pre><code>public void createExcelFileFromLog (Path logRejetFilePath, Path fichierInterdits) throws IOException {
  
  Map&lt;Integer, Integer&gt; mapLigneColonne = getRowAndColumnInError(logRejetFilePath);
  
  Workbook sourceWorkbook = WorkbookFactory.create(new File(fichierInterdits.toAbsolutePath().toString()));
  
  Sheet sourceSheet = sourceWorkbook.getSheetAt(0);
  
  List&lt;Row&gt; listLignes = new ArrayList&lt;Row&gt;();
  
  // get Rows from source Excel
  for (Map.Entry&lt;Integer, Integer&gt; entry : mapLigneColonne.entrySet()) {
      listLignes.add(sourceSheet.getRow(entry.getKey()-1));
  }
  
  // The new Excel
  Workbook workbookToWrite = new XSSFWorkbook();
  Sheet sheetToWrite = workbookToWrite.createSheet("Interdits en erreur");
  
  // Copy Rows
  Integer i = 0;
  for (Row row : listLignes) {
      copyRow(sheetToWrite, row, i);
      i++;
  }
  
  FileOutputStream fos = new FileOutputStream(config.getDossierTemporaire() + "Interdits_en_erreur.xlsx");
  workbookToWrite.write(fos);
  workbookToWrite.close();
  sourceWorkbook.close();
  }
  
  private static void copyRow(Sheet newSheet, Row sourceRow, int newRowNum) {
  
  Row newRow = newSheet.createRow(newRowNum);
  newRow = sourceRow;
  }
  </code></pre>
  
  <p>EDIT : Change the method of copyRow it's better but the date have weird format and blank cells from the original row are gone.</p>
  
  <pre><code>private static void copyRow(Sheet newSheet, Row sourceRow, int newRowNum) {
  
      Row newRow = newSheet.createRow(newRowNum);
      Integer i = 0;
  
      for (Cell cell : sourceRow) {
  
          if(cell.getCellTypeEnum() == CellType.NUMERIC) {
              newRow.createCell(i).setCellValue(cell.getDateCellValue());
          } else {
              newRow.createCell(i).setCellValue(cell.getStringCellValue());
          }
          i++;
      }
  }
  </code></pre>
  
  <p>EDIT 2 : To keep blank cell</p>
  
  <pre><code>private static void copyRow(Sheet newSheet, Row sourceRow, Integer newRowNum, Integer cellToColor) {
  
      Row newRow = newSheet.createRow(newRowNum);
      //Integer i = 0;
  
      int lastColumn = Math.max(sourceRow.getLastCellNum(), 0);
  
      for(int i = 0; i &lt; lastColumn; i++) {
          Cell oldCell = sourceRow.getCell(i, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
  
          if(oldCell == null) {
              newRow.createCell(i).setCellValue("");
          } else if (oldCell.getCellTypeEnum() == CellType.NUMERIC) {
              newRow.createCell(i).setCellValue(oldCell.getDateCellValue());
          } else {
              newRow.createCell(i).setCellValue(oldCell.getStringCellValue());
          }
      }
  }
  </code></pre>