最近在做导出excel时需要在单元格进行强制换行,查找资料,总结如下:
自动换行的设置:
HSSFCellStyle cellStyle=workbook.createCellStyle();
cellStyle.setWrapText(true);
cell.setCellStyle(cellStyle);
强制换行:
<pre name="code" class="html">HSSFCell cell = row.createCell((short)0);
cell.setCellStyle(cellStyle);
cell.setCellValue(new HSSFRichTextString("hello/r/n world!"));
导出的报表好如下图,编辑框显示的是换行,但是单元格显示不换行:
修改代码如码如下
HSSFCell cell = row.createCell((short)0);
cellStyle.setWrapText(true);//先设置为自动换行
cell.setCellStyle(cellStyle);
cell.setCellValue(new HSSFRichTextString("hello/r/n world!"));
导出的报表为: