有时,无涯教程希望创建具有基本样式的电子表格,然后将特殊样式应用于某些单元格,例如在一系列单元格周围绘制边框或设置区域的填充。 Apache POI提供了 CellUtil.setCellProperties ,无需在电子表格中创建一堆不必要的中间样式就可以做到这一点。
将属性创建为Map并将其应用于单元格。
链接:https://www.learnfk.comhttps://www.learnfk.com/apache-poi-word/apache-poi-excel-cell-properties.html
来源:LearnFk无涯教程网
看我们一个示例,在该示例中将特殊样式应用于某些单元格。
package poiexample; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.HashMap; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellUtil; public class CellPropertiesExample { public static void main(String[] args) { try (OutputStream os = new FileOutputStream("Learnfk.xls")) { Workbook workbook = new HSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet"); HashMapproperties = new HashMap (); //在单元格周围设置边框 properties.put(CellUtil.BORDER_TOP, BorderStyle.MEDIUM); properties.put(CellUtil.BORDER_BOTTOM, BorderStyle.MEDIUM); properties.put(CellUtil.BORDER_LEFT, BorderStyle.MEDIUM); properties.put(CellUtil.BORDER_RIGHT, BorderStyle.MEDIUM); //设置颜色红色 properties.put(CellUtil.TOP_BORDER_COLOR, IndexedColors.RED.getIndex()); properties.put(CellUtil.BOTTOM_BORDER_COLOR, IndexedColors.RED.getIndex()); properties.put(CellUtil.LEFT_BORDER_COLOR, IndexedColors.RED.getIndex()); properties.put(CellUtil.RIGHT_BORDER_COLOR, IndexedColors.RED.getIndex()); //将边框应用于单元格 Row row = sheet.createRow(2); Cell cell = row.createCell(2); CellUtil.setCellStyleProperties(cell, properties); //将边框应用于从 D4 开始的 3x3 区域 for (int i=3; i
输出:
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)