0

I need to update existing cell value by adding new lines to it during testing through selenium. I am not able to figure out code to update exising data using POI. When I tried to add using New Line Character, it is overwriting existing data. Please provide solution to add value to existing cell data on new line. Below is the code what I have for new Line comment

try (OutputStream fileOut = new FileOutputStream("MyFile.xls")) {  
    Workbook wb = new HSSFWorkbook();  
    Sheet sheet = wb.createSheet("Sheet");  
    Row row     = sheet.createRow(1);  
    Cell cell   = row.createCell(1);  
    cell.setCellValue("This is first line and \n this is second line");  
    CellStyle cs = wb.createCellStyle();  
    cs.setWrapText(true);  
    cell.setCellStyle(cs);  
    row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));  
    sheet.autoSizeColumn(2);  
    wb.write(fileOut); 
    Cell cell2   = row.getCell(1);
    cell2.setCellValue(" \n this is third line");  
    CellStyle cs2 = wb.createCellStyle();  
    cs2.setWrapText(true);  
    cell2.setCellStyle(cs2);  
    row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));  
    sheet.autoSizeColumn(2);  
    wb.write(fileOut);  

}catch(Exception e) {  
    System.out.println(e.getMessage());  
} 
James Z
  • 12,209
  • 10
  • 24
  • 44
Subhrut Taori
  • 27
  • 1
  • 2
  • 11

0 Answers0