ExcelWriter writer = ExcelUtil.getWriter();
writer.getStyleSet().setAlign(HorizontalAlignment.CENTER, VerticalAlignment.CENTER); //设置内容水平居中,上下居中
ArrayList<String> header = CollUtil.newArrayList("字段1","字段2","字段3");
ArrayList<String> content = CollUtil.newArrayList("内容1","内容2","内容3");
ArrayList<ArrayList<String>> list = CollUtil.newArrayList(header, content);
writer.write(list,true);
writer.setRowHeight(0,40); //设置首行高度
try {
writer.flush(response.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}finally {
writer.close();
}
解决方案:
writer.setRowHeight(0,40); 放在writer.write(list,true);后即可。
以下是 setRowHeight 代码
/**
* 设置行高,值为一个点的高度
*
* @param rownum 行号(从0开始计数,-1表示所有行的默认高度)
* @param height 高度
* @return this
* @since 4.0.8
*/
public ExcelWriter setRowHeight(int rownum, int height) {
if (rownum < 0) {
this.sheet.setDefaultRowHeightInPoints(height);
} else {
final Row row = this.sheet.getRow(rownum);
if (null != row) {
row.setHeightInPoints(height);
}
}
return this;
}
代码中表示需要先获取要设置高度的当前Row,无内容的情况下会跳过高度设置。