Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a linebreak as the data of a cell?

Tags:

I use Apache POI 3.16 to create an Excel file. I want to set the data inside a particular cell to have a linebreak :

rowConsommationEtRealisation.createCell(0).setCellValue("Consommation (crédits)\r\nRéalisation (produits)");

When I open the file then the cell value does not have linebreak ! So how to create linebreak ?

like image 504
pheromix Avatar asked Dec 31 '17 07:12

pheromix


People also ask

How do I insert a carriage return in an Excel cell?

We will press the excel shortcut key ALT + ENTER to insert the carriage return character in excel cell. As we press the “ALT + ENTER” key, it pushes the content in front of the selected data to the new line by inserting a carriage return.

How do you automate ALT Enter in Excel?

Double-click on the cell in which you want to insert the line break (or press F2). This will get you into the edit mode in the cell. Place the cursor where you want the line break. Use the keyboard shortcut – ALT + ENTER (hold the ALT key and then press Enter).

How do you do a line break as a delimiter in Excel?

In the input box to the right of Other press Ctrl + J to insert a line break as your delimiter. You should see dividers appear in the Data preview pane where there are line breaks in your data. Press the Next button.

How do I insert a hard return in Excel?

We have successfully added hard returns within the cells and we can now apply this tip when entering data into our worksheet. Hard returns come in handy when we want to force line breaks within cells with long text strings or sentences, and it is super easy to do with just one shortcut: Alt + Enter.


1 Answers

Try this: here

Row row = sheet.createRow(2);
Cell cell = row.createCell(2);
cell.setCellValue("Use \n with word wrap on to create a new line");

//to enable newlines you need set a cell styles with wrap=true
CellStyle cs = wb.createCellStyle();
cs.setWrapText(true);
cell.setCellStyle(cs);
like image 191
Hadi J Avatar answered Sep 20 '22 03:09

Hadi J