Search code examples
javaexcelpoi-hssfhssf

writing excel through JAVA using HSSF workBook


I am using HSSF work book to write data into excel using java, Now i have a column name with n-no of words, if i insert it normally its coming in a same line, i want to break the sentences into two different line and insert into same cell.

below is the code iam using to set the column

 rowhead.createCell((short) 0).setCellValue("Hi there it is my lengthy column please help");

Thanks, Tom


Solution

  • You just need to use the Java new line '\n' char to break your sentence into 2 lines then set WrapText to true:

        Cell cell = rowhead.createCell((short) 0));
        cell.setCellValue("Hi there it is my lengthy \n column please help");
        CellStyle cs = wb.createCellStyle();
        cs.setWrapText(true);
        cell.setCellStyle(cs);