POI Excel - 属性单元格

POI Excel - 属性单元格 首页 / POI入门教程 / POI Excel - 属性单元格

有时,无涯教程希望创建具有基本样式的电子表格,然后将特殊样式应用于某些单元格,例如在一系列单元格周围绘制边框或设置区域的填充。 Apache POI提供了 CellUtil.setCellProperties ,无需在电子表格中创建一堆不必要的中间样式就可以做到这一点。

将属性创建为Map并将其应用于单元格。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/apache-poi-word/apache-poi-excel-cell-properties.html

来源:LearnFk无涯教程网

看我们一个示例,在该示例中将特殊样式应用于某些单元格。

Excel单元格属性示例

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");
			 HashMap properties = 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 

输出:

Apache POI Excel Cell Properties

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

快速上手Kotlin开发 -〔张涛〕

TensorFlow快速入门与实战 -〔彭靖田〕

玩转webpack -〔程柳锋〕

ZooKeeper实战与源码剖析 -〔么敬国〕

MongoDB高手课 -〔唐建法(TJ)〕

说透敏捷 -〔宋宁〕

乔新亮的CTO成长复盘 -〔乔新亮〕

物联网开发实战 -〔郭朝斌〕

程序员的测试课 -〔郑晔〕

好记忆不如烂笔头。留下您的足迹吧 :)