ExcelWriter writer = ExcelUtil.getWriter(true);
//第二行第一列
CellStyle cellStyle1 = writer.createCellStyle(1, 0);
CellStyle cellStyle1 = writer.createCellStyle(1, 0);
cellStyle1.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellStyle1.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
cellStyle1.setWrapText(true);//自动换行
//第一种字体样式
Font font1 = writer.createFont();
font1.setFontName("黑体");
font1.setFontHeightInPoints((short) 18);
//第二种字体样式
Font font3 = writer.createFont();
font3.setFontName("仿宋_GB2312");
font3.setFontHeightInPoints((short) 16);
//单元格内容
String txt1 = "第一行";
int l = txt1.length();
String txt = txt1 + "\r\n(第二行)";
//RichTextString两种实现类XSSFRichTextString,HSSFRichTextString
//HSSFRichTextString text = new HSSFRichTextString (txt);
XSSFRichTextString text = new XSSFRichTextString(txt);
text.applyFont(0, l, font1);
text.applyFont(l + 1, txt.length(), font3);
writer.writeCellValue(0, 1, text);
writer.setStyle(cellStyle1, 0, 1);
//设置行高
writer.setRowHeight(1, 60);
Hutool导出Excel,在一个单元格设置多个字体样式
于 2022-05-20 11:40:40 首次发布