POI----CellStyle单元格样式的设定

1.创建单元格样式。

CellStyle style = workbook.createCellStyle();
cell.setCellStyle(style);

2. setFillForegroundColor(short paramShort) :设置单元格背景填充色。

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

 short paramShort:参数对应的颜色如上图所示

IndexedColors.AQUA.getIndex()
IndexedColors.AUTOMATIC.getIndex()
IndexedColors.BLACK.getIndex()
IndexedColors.BLUE.getIndex()
IndexedColors.BLUE_GREY.getIndex() 
IndexedColors.BRIGHT_GREEN.getIndex() 
IndexedColors.BROWN.getIndex() 
IndexedColors.CORAL.getIndex()
IndexedColors.CORNFLOWER_BLUE.getIndex()
IndexedColors.DARK_BLUE.getIndex()
IndexedColors.DARK_GREEN.getIndex()
IndexedColors.DARK_RED.getIndex()
IndexedColors.DARK_TEAL.getIndex()
IndexedColors.DARK_YELLOW.getIndex()
IndexedColors.GOLD.getIndex()
IndexedColors.GREEN.getIndex()
IndexedColors.GREY_25_PERCENT.getIndex()
IndexedColors.GREY_40_PERCENT.getIndex()
IndexedColors.GREY_50_PERCENT.getIndex()
IndexedColors.GREY_80_PERCENT.getIndex()
IndexedColors.INDIGO.getIndex()
IndexedColors.LAVENDER.getIndex()
IndexedColors.LEMON_CHIFFON.getIndex()
IndexedColors.LIGHT_BLUE.getIndex()
IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex()
IndexedColors.LIGHT_GREEN.getIndex()
IndexedColors.LIGHT_ORANGE.getIndex()
IndexedColors.LIGHT_TURQUOISE.getIndex()
IndexedColors.LIGHT_YELLOW.getIndex()
IndexedColors.LIME.getIndex()
IndexedColors.MAROON.getIndex()
IndexedColors.OLIVE_GREEN.getIndex()
IndexedColors.ORANGE.getIndex()
IndexedColors.ORCHID.getIndex()
IndexedColors.PALE_BLUE.getIndex()
IndexedColors.PINK.getIndex()
IndexedColors.PLUM.getIndex()
IndexedColors.RED.getIndex()
IndexedColors.ROSE.getIndex()
IndexedColors.ROYAL_BLUE.getIndex()
IndexedColors.SEA_GREEN.getIndex()
IndexedColors.SKY_BLUE.getIndex()
IndexedColors.TAN.getIndex()
IndexedColors.TEAL.getIndex()
IndexedColors.TURQUOISE.getIndex()
IndexedColors.VIOLET.getIndex() 
IndexedColors.WHITE.getIndex()
IndexedColors.YELLOW.getIndex()

3.设置单元格字体颜色  ps:font还可以设置字体的名,高度,下划线等。

  ExcelWorkBook xlBook = xlSheet.getXlBook();
  Font font = xlBook.createFont();
  font.setColor(IndexedColors.WHITE.getIndex());
  style.setFont(font);

4. 设置填充方式。style.setFillPattern(CellStyle.SOLID_FOREGROUND)

5.  setAlignment(CellStyle.ALIGN_CENTER)  设置水平方向字体居中

6.setVerticalAlignment(CellStyle.VERTICAL_CENTER)设置垂直方向字体居中

 

 

 

 

 

 

  • 8
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
poi-tl是一个基于Apache POI的Java模板引擎,用于生成Word、Excel和PowerPoint文档。它提供了一种简单而强大的方式来操作这些文档。 在poi-tl中,要合并Word文档中的列单元格,可以按照以下步骤进行操作: 1. 创建一个Word模板文件,其中包含需要合并列单元格的表格。 2. 使用poi-tl的API加载模板文件,并获取到需要操作的表格对象。 3. 使用表格对象的合并单元格方法,指定需要合并的起始行、起始列、结束行和结束列。 4. 根据需要重复步骤3,合并多个列单元格。 5. 保存修改后的Word文档。 下面是一个示例代码,演示了如何使用poi-tl合并Word文档中的列单元格: ```java import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.VerticalAlign; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class MergeTableCellsExample { public static void main(String[] args) { try { // 加载Word模板文件 FileInputStream fileInputStream = new FileInputStream("template.docx"); XWPFDocument document = new XWPFDocument(fileInputStream); // 获取第一个表格对象 XWPFTable table = document.getTables().get(0); // 合并第一行的前两个单元格 mergeTableCells(table, 0, 0, 0, 1); // 保存修改后的Word文档 FileOutputStream fileOutputStream = new FileOutputStream("output.docx"); document.write(fileOutputStream); fileOutputStream.close(); System.out.println("合并列单元格成功!"); } catch (IOException e) { e.printStackTrace(); } } private static void mergeTableCells(XWPFTable table, int startRow, int startCol, int endRow, int endCol) { for (int rowIndex = startRow; rowIndex <= endRow; rowIndex++) { XWPFTableRow row = table.getRow(rowIndex); for (int colIndex = startCol; colIndex <= endCol; colIndex++) { XWPFTableCell cell = row.getCell(colIndex); if (colIndex == startCol) { // 设置合并单元格的垂直对齐方式为居中 cell.setVerticalAlignment(VerticalAlign.CENTER); } else { // 移除非起始列的单元格 row.removeCell(colIndex); } } } } } ``` 请注意,上述示例代码中的"template.docx"是模板文件的路径,"output.docx"是保存合并后的Word文档的路径。你需要根据实际情况修改这些路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值