POI导出excel如何设置字体,颜色和自适应?

1.设置字体,颜色

//设置style
CellStyle cstyle = workbook.createCellStyle();
HSSFFont font = workbook.createFont();

font .setFontHeightInPoints((short) 10);//字体大小
font .setColor(Font.COLOR_BLUE);//字体颜色
font.setFontName("宋体");//字体
cstyle .setFont(font);

......
cell.setCellStyle(cstyle);//将style设置进单元格cell中
......

2.自适应

  • 设置行高
row.setHeightInPoints(15);//将行高设置成15px
  • 设置列宽
//根据一列数据中的最长的字符串长度设置宽度
sheet.AutoSizeColumn(i)//i为列,只能解决英文、数字列宽自适应。
sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 18 / 10);// 解决自动设置列宽中文失效的问题
发布了52 篇原创文章 · 获赞 84 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_39380155/article/details/104846505
今日推荐