上代码
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
public static void main(String[] args) {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("测试");
// 合并单元格后居中
CellStyle cellStyle = wb.createCellStyle();
// 垂直居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
// 设置字体
Font font = wb.createFont();
font.setBold(true);
font.setFontName("宋体");
font.setFontHeightInPoints((short) 25);
font.setItalic(false);
font.setStrikeout(false);
cellStyle.setFont(font);
cellStyle.setWrapText(true);
Row row = sheet.createRow(1);
row.setHeightInPoints(100);
Cell specialCell = row.createCell(0);
specialCell.setCellValue("测试列");
specialCell.setCellStyle(cellStyle);
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 1));
String path = "D:\\test\\" + System.currentTimeMillis() + ".xlsx";
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(path);
wb.write(fileOutputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}