How to insert a linebreak as the data of a cell?
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);
It has the line break already but the cell does not show it. You need setting a cell style having wrap text property set to the cell.
Example:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
class ExcelLineBreakWrapText {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
CellStyle wrapStyle = workbook.createCellStyle();
wrapStyle.setWrapText(true);
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellStyle(wrapStyle);
cell.setCellValue("Consommation (crédits)\r\nRéalisation (produits)");
sheet.autoSizeColumn(0);
workbook.write(new FileOutputStream("ExcelLineBreakWrapText.xlsx"));
workbook.close();
}
}