http://jjw198874.blog.163.com/blog/static/1889845522011102401854234/

POI按照源单元格设置目标单元格格式

2011-11-24 00:25:07|  分类: 日记 |  标签:java  eclipse  javaexcel  poi   |字号大中小 订阅

poi按照一个源单元格设置目标单元格格式,如果两个单元格不在同一个workbook,
要用
HSSFCellStyle下的cloneStyleFrom()方法,
而不能用
HSSFCell下的setCellStyle()方法。
public void copyHssfRow(HSSFRow destRow, HSSFRow sourceRow){
int currentColumnNum = 0;
HSSFCell tempCell = null;
HSSFCellStyle tempCellStyle = null;
System.out.println("ExcelHandler.java copy specific excel row starts------");
for(Iterator i = sourceRow.cellIterator(); i.hasNext();){
tempCell = (HSSFCell)i.next();
//确保这个destRow的待赋值cell有值
if(destRow.getCell(currentColumnNum) == null){
destRow.createCell(currentColumnNum);
}
destRow.getCell(currentColumnNum).setCellValue(tempCell.getStringCellValue());
//红色字体的目的是把目的单元格的格式设置为源单元格的格式,这是正确的方式
tempCellStyle = tempCell.getCellStyle();
destRow.getCell(currentColumnNum).getCellStyle().cloneStyleFrom(tempCellStyle);
System.out.println("DestCell name: " + destRow.getCell(currentColumnNum).getStringCellValue()
+ " SourceCell name: " + tempCell.getStringCellValue());
currentColumnNum++;
}
System.out.println("ExcelHandler.java copy specific excel row ends------");
}

最初红色的代码是这样的:
tempCellStyle = tempCell.getCellStyle();
destRow.getCell(currentColumnNum).setCellStyle(tempCellStyle);
然后出现了报错:
java.lang.IllegalArgumentException: This Style does not belong to the supplied Workbook.
再去google搜索这个报错,发现poi手册中HSSFCellStyle有一个方法会抛出这个异常。

verifyBelongsToWorkbook

public void verifyBelongsToWorkbook(HSSFWorkbook wb)
Verifies that this style belongs to the supplied Workbook. Will throw an exception if it belongs to a different one. This is normally called when trying to assign a style to a cell, to ensure the cell and the style are from the same workbook (if they're not, it won't work)
Throws:
java.lang.IllegalArgumentException - if there's a workbook mis-match
再往下一看,就柳暗花明了。poi要求一个单元格的格式和单元格本身都在一个worksheet,不同的worksheet间不能共用HSSFCellStyle。想了下,这样是对的,这样就可以保证同一个style不会同时属于两个不同sheet的不同单元格,如果对一个单元格做了修改,另一个也会受到牵连。单元格之间的格式相当一个格式集,格式集中的参数数值我们可以一样,但是我们的格式集不能是同一个。

cloneStyleFrom

public void cloneStyleFrom(CellStyle source)Clones all the style information from another HSSFCellStyle, onto this one. This HSSFCellStyle will then have all the same properties as the source, but the two may be edited independently. Any stylings on this HSSFCellStyle will be lost! The source HSSFCellStyle could be from another HSSFWorkbook if you like. This allows you to copy styles from one HSSFWorkbook to another.
Specified by:
cloneStyleFrom in interface CellStyle

This Style does not belong to the supplied Workbook. Are you trying to assign a style from one workb相关推荐

  1. vue基础-动态class、动态style、vue过滤器、vue计算属性vue基础-动态class、动态style、vue过滤器、vue计算属性

    vue基础-动态class.动态style.vue过滤器.vue计算属性 文章目录 vue基础-动态class.动态style.vue过滤器.vue计算属性 vue基础-动态class vue基础-动 ...

  2. POI按照源单元格设置目标单元格格式

    原文:http://jjw198874.blog.163.com/blog/static/1889845522011102401854234/ POI按照源单元格设置目标单元格格式 poi按照一个源单 ...

  3. 样式和主题(Style and Theme)详解

    导读: 本篇文章主要根据官方文档修改,介绍了样式和主题的使用以及要注意的问题,同时也提供了Android Studio 快速抽取Style 和 编辑 Theme 的方法. Tips 使用Android ...

  4. Android Style和自定义属性

    文章目录 1. Android Style & Theme 1.1 基本概念 1.1.1 Style(样式) 1.1.2 Theme(主题) 1.2 创建和应用style 1.3 扩展和自定义 ...

  5. HTML样式offset[Direction] 和 style.[direction]的区别

    为什么80%的码农都做不了架构师?>>>    以offsetLeft与style.left为例: offsetLeft使用的值是字符串,如"100px", st ...

  6. 谈谈图像的style transfer(二)

    总说 主要从几个方面来进行说明吧  - 加快transfer的速度  - 让transfer的效果看起来更加visual-pleasing  - 其他的一些方面  - 用GAN来做 加快style s ...

  7. Art Generation with Neural Style Transfer-v2

    这是吴恩达老师深度学习课程第四课(卷积神经网络)第四周第二次的作业题目,不包含答案! Deep Learning & Art: Neural Style Transfer Welcome to ...

  8. WPF QuickStart系列之样式和模板(Style and Template)

    原文:WPF QuickStart系列之样式和模板(Style and Template) 在WPF桌面程序中,当我们想构建一个统一的UI表现时(在不同操作系统下,显示效果一致),此时我们就需要使用到 ...

  9. 样式表(CSS:Cascading Style Sheets)简要教程[上]

    首先提供两个手册的下载 CSS2&JavaScript.rar 一.样式表基础 1. 样式表中每一个条目由选择符(selector)和对应的规则组成,选择符通常是HTML元素名称,也可以是类( ...

最新文章

  1. 一个LabVIEW控件,生成模拟波形效果
  2. ES6中新增的字符串方法
  3. Kubernetes的四种用户部署场景
  4. 华为平板能运行python吗_我是如何使用华为平板的
  5. 日常生活收缩毛孔几个小妙招 - 健康程序员,至尚生活!
  6. PHP str_replace() 和str_ireplace()函数
  7. STOLUCK:经济下行的当下 ,STO或将帮助中小企业度过寒冬
  8. 第六章实验报告(函数和宏定义实验)
  9. 泛型类的定义与实例化 c#
  10. (21)FPGA面试题D触发器表示方法
  11. Linux目录/usr/bin和 /usr/local/bin区别
  12. Linux视频教程—笔记(全)
  13. 【工具向】分析FGUI依赖关系工具
  14. AMD GPU 系列版本信息
  15. 记录一次海康威视录像机重置密码的经历
  16. Counting Bloom Filter
  17. 使用区块链强化安全多方计算(SMPC)执行力、溯源能力
  18. 【心情分享】自己心中的程序员和别人眼里的程序员
  19. java 模拟登录微信_java 微信模拟登录错误,微信改版后不能使用
  20. Mac系统pycharm的setting在哪里?

热门文章

  1. Git之深入解析如何重写提交历史
  2. ☆ 10个小技巧,让你的 Python 代码更加优雅~ ☆
  3. 最长公共子序列Lcs 51Nod - 1006
  4. 13.2.2 路由层——URL控制器
  5. Linux基础 —— 操作系统简介
  6. PAT (Basic Level) Practice (中文)1001 害死人不偿命的(3n+1)猜想 (15 分)
  7. 【机器视觉】 gen_measure_rectangle2算子
  8. 【Linux】一步一步学Linux——nice命令(127)
  9. 【Qt】Qt中信号与槽
  10. 【Linux网络编程】原始套接字实例:MAC 头部报文分析