前一段时间在做一个学校排课系统时,有一个地方需要利用把课程表生成excel汇出给客户,由于之前用excel都只是简单的应用,在单元格里都是用自动换行,而这次可能需要用到手动强制换行。
于是我在网上找了一下,网上找到的文章都是说在excel里的文字里加上\n,\n\r,\r\n之类,反正各种各样的都有,更奇怪的是还有人说在单元格里加上<br>
后来我试过用\r后的效里是生成的文件里,你用打开时,并不会换行,如果你用鼠标在单元格里点一下之后就会自动换行。
后来我琢磨了一下,可以通过如下方式进行,
1. 首先在需要强制换行的单元格里使用poi的样式,并且把样式设定为自动换行
Java代码
1. HSSFCellStyle cellStyle=workbook.createCellStyle();
2. cellStyle.setWrapText(true);
3. cell.setCellStyle(cellStyle);
HSSFCellStyle cellStyle=workbook.createCellStyle(); cellStyle.setWrapText(true); cell.setCellStyle(cellStyle);
2. 其次是在需要强制换行的单元格,使用\就可以实再强制换行
Java代码
1. HSSFCell cell = row.createCell((short)0);
2. cell.setCellStyle(cellStyle); cell.setCellValue(new HSSFRichTextString("hello\r\n world!"));
HSSFCell cell = row.createCell((short)0); cell.setCellStyle(cellStyle); cell.setCellValue(new HSSFRichTextString("hello\r\n world!"));
这样就能实现强制换行,
换行后的效里是单元格里强制换行
hello
world!
轉于:http://hi.baidu.com/lovetojava/blog/item/34e71812e757e0daf7039eec.html
于是我在网上找了一下,网上找到的文章都是说在excel里的文字里加上\n,\n\r,\r\n之类,反正各种各样的都有,更奇怪的是还有人说在单元格里加上<br>
后来我试过用\r后的效里是生成的文件里,你用打开时,并不会换行,如果你用鼠标在单元格里点一下之后就会自动换行。
后来我琢磨了一下,可以通过如下方式进行,
1. 首先在需要强制换行的单元格里使用poi的样式,并且把样式设定为自动换行
Java代码
1. HSSFCellStyle cellStyle=workbook.createCellStyle();
2. cellStyle.setWrapText(true);
3. cell.setCellStyle(cellStyle);
HSSFCellStyle cellStyle=workbook.createCellStyle(); cellStyle.setWrapText(true); cell.setCellStyle(cellStyle);
2. 其次是在需要强制换行的单元格,使用\就可以实再强制换行
Java代码
1. HSSFCell cell = row.createCell((short)0);
2. cell.setCellStyle(cellStyle); cell.setCellValue(new HSSFRichTextString("hello\r\n world!"));
HSSFCell cell = row.createCell((short)0); cell.setCellStyle(cellStyle); cell.setCellValue(new HSSFRichTextString("hello\r\n world!"));
这样就能实现强制换行,
换行后的效里是单元格里强制换行
hello
world!
轉于:http://hi.baidu.com/lovetojava/blog/item/34e71812e757e0daf7039eec.html
本文介绍如何在使用Java POI库生成Excel文件时实现单元格内的强制换行效果。通过设置单元格样式允许自动换行,并在字符串中使用
达到换行目的。

5353

被折叠的 条评论
为什么被折叠?



