Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(2);
Cell cell = row.createCell(2);
cell.setCellValue("Use \n with word wrap on to create a new line");
//to enable newlines you need set a cell styles with wrap=true
CellStyle cs = wb.createCellStyle();
cs.setWrapText(true);
cell.setCellStyle(cs);
//increase row height to accomodate two lines of text
row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));
//adjust column width to fit the content
sheet.autoSizeColumn((short)2);
FileOutputStream fileOut = new FileOutputStream("E:\\standarcode\\ooxml-newlines.xlsx");
wb.write(fileOut);
fileOut.close();
效果:
本文介绍了一种在Excel中创建换行的方法,通过设置单元格样式以实现文本换行显示,并调整行高和列宽来优化布局。
2343

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



