它已经有换行符,但单元格不显示它。您需要设置将自动换行文本属性设置为单元格的单元格样式。
例:
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();
}
}