Java use POI to generate Excel to force a line
13:58:00 May 26, 2014 sanyuesan0000 Reads: 29371
Recently doing export excel needed in the cell when the forced line breaks, to find information, summarized as follows:
Wrap settings:
-
HSSFCellStyle cellStyle=workbook.createCellStyle();
-
cellStyle.setWrapText(true);
-
cell.setCellStyle(cellStyle);
Forced line break:
-
<pre name="code" class="html">HSSFCell cell = row.createCell((short)0);
-
cell.setCellStyle(cellStyle);
-
cell.setCellValue(new HSSFRichTextString("hello/r/n world!"));
FIG exported report as well, the edit box displays the line feed, but the cell does not wrap display:
Modify the code such as code
-
HSSFCell cell = row.createCell((short)0);
-
cellStyle.setWrapText(true);//先设置为自动换行
-
cell.setCellStyle(cellStyle);
-
cell.setCellValue(new HSSFRichTextString("hello/r/n world!"));
Export reports for:
Another note: I originally intended with a "/ r / n" with the sql statement splicing field, assigned to the cell, so that force a line break, but the results show only a string, not line breaks, the last solution is to give when cell splitting string assignment, then str1 + "/ r / n" + str2 splicing, the cell will be correctly displayed wrap.
The reason is not clear.