这篇文章主要讲解了“java POI怎么实现Excel单元格内容换行”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“java POI怎么实现Excel单元格内容换行”吧!
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
@RestController
public class MyController {
@RequestMapping("/ip/v5")
public void getExcel(HttpServletResponse response) throws IOException {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("this is 单元格第1行");
arrayList.add("this is 单元格第2行");
arrayList.add("this is 单元格第3行");
arrayList.add("this is 单元格第4行");
XSSFWorkbook workBook = new XSSFWorkbook();
XSSFSheet sheet = workBook.createSheet();
workBook.setSheetName(0, "ip-v4表");
XSSFCellStyle cs = workBook.createCellStyle(); // 换行的关键,自定义单元格内容换行规则
cs.setWrapText(true);
String fileName = "china-ip-v4" + ".xls";// 设置要导出的文件的名字
String[] headers = { "掩码" };
XSSFRow titleRow = sheet.createRow(0);
// 在excel表中添加表头
for (int i = 0; i < headers.length; i++) {
titleRow.createCell(i).setCellValue(headers[i]);
}
String content = String.join("\n", arrayList);
int rowNum = 1;
XSSFRow row1 = sheet.createRow(rowNum); // 创建一行
XSSFCell cell = row1.createCell(0); // 创建一个单元格
// 如下也是可以的
//cell.setCellValue("this is 单元格第1行 \n this is单元格第2行 \n this is 单元格第3行 \n this is 单元格第4行");
cell.setCellValue(content);
cell.setCellStyle(cs);
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
response.flushBuffer();
workBook.write(response.getOutputStream());
}
}
结果:
String str="强制\r\n换行"
在字符串中间加上\r\n就行了~
感谢各位的阅读,以上就是“java POI怎么实现Excel单元格内容换行”的内容了,经过本文的学习后,相信大家对java POI怎么实现Excel单元格内容换行这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。