java font.createfont_Java Workbook.createFont方法代码示例

这段代码展示了如何在Java中使用Apache POI库创建Excel工作簿,并定义不同样式的单元格,包括居中对齐、边框样式、字体设置等。
摘要由CSDN通过智能技术生成

import org.apache.poi.ss.usermodel.Workbook; //导入方法依赖的package包/类

/**

* 创建表格样式

* @param wb 工作薄对象

* @return 样式列表

*/

private Map createStyles(Workbook wb) {

Map styles = new HashMap();

CellStyle style = wb.createCellStyle();

style.setAlignment(CellStyle.ALIGN_CENTER);

style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

Font titleFont = wb.createFont();

titleFont.setFontName("Arial");

titleFont.setFontHeightInPoints((short) 16);

titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);

style.setFont(titleFont);

styles.put("title", style);

style = wb.createCellStyle();

style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

style.setBorderRight(CellStyle.BORDER_THIN);

style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());

style.setBorderLeft(CellStyle.BORDER_THIN);

style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());

style.setBorderTop(CellStyle.BORDER_THIN);

style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());

style.setBorderBottom(CellStyle.BORDER_THIN);

style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());

Font dataFont = wb.createFont();

dataFont.setFontName("Arial");

dataFont.setFontHeightInPoints((short) 10);

style.setFont(dataFont);

styles.put("data", style);

style = wb.createCellStyle();

style.cloneStyleFrom(styles.get("data"));

style.setAlignment(CellStyle.ALIGN_LEFT);

styles.put("data1", style);

style = wb.createCellStyle();

style.cloneStyleFrom(styles.get("data"));

style.setAlignment(CellStyle.ALIGN_CENTER);

styles.put("data2", style);

style = wb.createCellStyle();

style.cloneStyleFrom(styles.get("data"));

style.setAlignment(CellStyle.ALIGN_RIGHT);

styles.put("data3", style);

style = wb.createCellStyle();

style.cloneStyleFrom(styles.get("data"));

//style.setWrapText(true);

style.setAlignment(CellStyle.ALIGN_CENTER);

style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());

style.setFillPattern(CellStyle.SOLID_FOREGROUND);

Font headerFont = wb.createFont();

headerFont.setFontName("Arial");

headerFont.setFontHeightInPoints((short) 10);

headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);

headerFont.setColor(IndexedColors.WHITE.getIndex());

style.setFont(headerFont);

styles.put("header", style);

return styles;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值