Java use POI to generate Excel to force a line

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:

 

 
  1. HSSFCellStyle cellStyle=workbook.createCellStyle();

  2. cellStyle.setWrapText(true);

  3. cell.setCellStyle(cellStyle);

 

Forced line break:

 

 
  1. <pre name="code" class="html">HSSFCell cell = row.createCell((short)0);

  2. cell.setCellStyle(cellStyle);

  3. 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

 

 
  1. HSSFCell cell = row.createCell((short)0);

  2. cellStyle.setWrapText(true);//先设置为自动换行

  3. cell.setCellStyle(cellStyle);

  4. 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.

Guess you like

Origin blog.csdn.net/qq_42310995/article/details/88119985