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:
cloneStyleFromin interfaceCellStyle
 

POI按照源单元格设置目标单元格格式的更多相关文章

  1. POI中设置Excel单元格格式

    引用:http://apps.hi.baidu.com/share/detail/17249059 POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWork ...

  2. POI HSSFCellStyle 设置 Excel 单元格样式

    POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb ...

  3. poi excel设置合并单元格边框格式

    版本3.17 //设置合并单元格的边框 public static void setBorderForMergeCell(BorderStyle style,int color, CellRangeA ...

  4. POI教程之第二讲:创建一个时间格式的单元格,处理不同内容格式的单元格,遍历工作簿的行和列并获取单元格内容,文本提取

    第二讲 1.创建一个时间格式的单元格 Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿 Sheet sheet=wb.createSheet("第一个 ...

  5. 如何用Apache POI操作Excel文件-----如何对一个单元格加注解?

    有的时候,我们需要通过操作Apache POI,在生成Cell数据的同时,能对其生成的Cell,加上注解(comments),类似于下面的. 那么对于这种情况,我们的代码应该如何写呢? 借花献佛,我就 ...

  6. java POI excel 导出复合样式(一个单元格两个字体)

    前言:java poi 导出 excel 时,需要设置一个单元格有多个字体样式,有点类似于富文本. 想要达到的效果(一个单元格里): 我使用的 poi 版本是 <dependency> & ...

  7. NPOI2.2.0.0实例详解(十)—设置EXCEL单元格【文本格式】 NPOI 单元格 格式设为文本 HSSFDataFormat

    NPOI2.2.0.0实例详解(十)—设置EXCEL单元格[文本格式] 2015年12月10日 09:55:17 阅读数:3150 using System; using System.Collect ...

  8. C#中的Excel操作【1】——设置Excel单元格的内容,打开Excel文件的一种方式

    前言 作为项目管理大队中的一员,在公司里面接触最多的就是Excel文件了,所以一开始就想从Excel入手,学习简单的二次开发,开始自己的编程之路! 程序界面 功能说明 打开文件按钮,可以由使用者指定要 ...

  9. GridControl 设置焦点单元格

    gdvNew.Focus(); //GridControl 控件获取焦点 gdvNew.FocusedRowHandle = _smtReport.Count - 1; //设置焦点行 gdvNew. ...

随机推荐

  1. mysql中模糊查询的四种用法介绍

    下面介绍mysql中模糊查询的四种用法: 1,%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FROM [user] ...

  2. TensorFlow简单介绍和在centos上的安装

    ##tensorflow简单介绍: TensorFlow™ is an open source software library for numerical computation using dat ...

  3. jq中 offset()方法, scrollTop()方法以及scrollLeft()方法

    offset()方法是用来获取元素在当前视窗的相对偏移,其中返回的对象包含两个属性,即top和left,它只对可见元素有效. scrollTop()方法是用来获取元素的滚动条距离顶端的距离. scro ...

  4. Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  5. codeforces 192b

    link: http://codeforces.com/contest/330/problem/B I think the problem is hard at first. However, whe ...

  6. c 函数及指针学习 8

    联合体 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h>   union sa     {     double a;     int b; ...

  7. 简单三步-实现dede站内搜索功能

    第一步:找到对应的搜索模板的代码 我们都知道,dede有自带的搜索功能,我们只要找到对应的模板,然后把我们想要的代码拿出来就行了.具体如下: 首先进入templets-->default--&g ...

  8. socket模块

    1 1.1 server: #!/use/local/env python# -*- coding:utf-8 -*- import socket ip_port = ('127.0.0.1', 99 ...

  9. PCA人脸识别

    人脸数据来自http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html 实现代码和效果如下.由于图片数量有限(40*10),将原 ...

  10. 用audacity制作循环背景音乐

    用audacity制作循环音乐: 1,将音乐前面一段剪切,粘贴到结尾. 2,选择包含接缝的一个区间,然后 菜单->效果->crossfade clips.