Excel Open Xml中CellStyleXfs,cellStyle,cellXfs之间关系的总结

最近这几个东东打交道了几天,总算是弄明白了,综合多个帖子,现在总结如下:

在创建stylesheet时,必须创建fonts,Fills,Borders 和cellXfs(CellFormats)四个节点。

而cellXfs节点是综合节点,它需要引用numFormatId列表、FontId列表、fillId列表和borderId列表,这些都包含在Styles.xml文件中。

1.cellXfs,是给单元格自行设置的样式。 
2.CellStyleXfs,就是在单元格样式中建立的自定义样式,用于cellXfs中的xfid属性。 
3.cellStyle, 在"name"属性可以找到CellStyleXfs中的样式名,对应的属性"xfId",是"CellStyleXfs"节点的子节点"xf"的索引,从"0"开始。

他们之间的具体关系,下面这篇文章写的很精辟,how to understand the process of cell formatting ?http://social.msdn.microsoft.com/Forums/zh-CN/e6fe3ff0-152e-4398-9d17-fee8476ae466/how-to-understand-the-process-of-cell-formatting-

总结一下:即单元格没有指定特别的样式时,也就是打开excel,在单元格中随意输入一个字符,然后什么都不做就保存,这就是没有指定特别的样式,用excel再次打开该文档时,它就去cellStyle里找默认的Normal样式,该样式在CellStyleXfs里定义,可以找到对应的fontid, fillid, borderid, numfmtid等等。 如果给单元格指定了某种样式,就去cellXfs里找这种格式,它里面也可以找到对应的fontid, fillid, borderid, numfmtid等等。

实际的Demo如下:

1.打开excel,在单元格中随意输入一个字符,然后什么都不做就保存,这就是没有指定特别的样式, 此时style.xml中的cellXfs, cellStyleXfs, cellStyles如下:

2.用excel打开这个文件,新建一种单元格样式:样式1,但不给任何一个单元格应用,操作步骤如下:

保存该文件,现在styles.xml里的代码如下:

可以看到,新增的样式保存在cellStyleXfs和cellStyles节点中了,因为没有单元格应用它,所以在cellXfs中没有它的踪迹。

在sheet1.xml中,如下图,该单元格没有s="*"的标记,excel就去cellStyles里去找默认的常规样式,然后按照xfId="0"再去cellStyleXfs里找第一项,用里面标记的borderid, fillid, fontid, numFmtId去给它应用了。

3.给一个单元格应用:样式1

此时styles.xml中代码如下:

这时,cellXfs里就有添加进样式1的内容了, 同时在sheet1.xml中,应用了此样式的单元格上有了s="1"的标记,如下图,索引从0开始,s="1"其实指的第二项,这时excel就直接用cellXfs里第二项,用里面标记的borderid, fillid, fontid, numFmtId去给它应用了。

附录:

1.CellStyle Class Cell Style.When the object is serialized out as xml, its qualified name is x:cellStyle.http://msdn.microsoft.com/zh-cn/library/ie/documentformat.openxml.spreadsheet.cellstyle.aspx?cs-save-lang=1&cs-lang=vb

2.CellStyleFormats Class Formatting Records.When the object is serialized out as xml, its qualified name is x:cellStyleXfs. http://msdn.microsoft.com/zh-cn/library/ie/documentformat.openxml.spreadsheet.cellstyleformats.aspx

3.CellFormats Class Cell Formats.When the object is serialized out as xml, its qualified name is x:cellXfs.http://msdn.microsoft.com/zh-cn/library/ie/documentformat.openxml.spreadsheet.cellformats.aspx

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,关于Java POI使用缓存复制Excel所有CellStyle的问题,可以按照以下步骤进行: 1. 首先创建一个HashMap对象,用于缓存CellStyle。HashMap的key为CellStyle的hashcode,value为CellStyle对象。 ``` HashMap<Integer, CellStyle> styleCache = new HashMap<Integer, CellStyle>(); ``` 2. 遍历源Workbook的所有Sheet,复制每个Sheet到目标Workbook。在复制单元格时,需要判断单元格所在的行和列是否已经存在于目标Sheet。如果存在,直接获取目标单元格的CellStyle并应用到当前单元格;如果不存在,需要创建新的单元格和CellStyle。 ``` for (int i = 0; i < sourceWorkbook.getNumberOfSheets(); i++) { Sheet sourceSheet = sourceWorkbook.getSheetAt(i); Sheet targetSheet = targetWorkbook.createSheet(sourceSheet.getSheetName()); for (int rowNum = sourceSheet.getFirstRowNum(); rowNum <= sourceSheet.getLastRowNum(); rowNum++) { Row sourceRow = sourceSheet.getRow(rowNum); Row targetRow = targetSheet.getRow(rowNum); if (targetRow == null) { targetRow = targetSheet.createRow(rowNum); } for (int colNum = sourceRow.getFirstCellNum(); colNum < sourceRow.getLastCellNum(); colNum++) { Cell sourceCell = sourceRow.getCell(colNum); Cell targetCell = targetRow.getCell(colNum); if (targetCell == null) { targetCell = targetRow.createCell(colNum); } CellStyle sourceStyle = sourceCell.getCellStyle(); int styleHashCode = sourceStyle.hashCode(); CellStyle targetStyle = styleCache.get(styleHashCode); if (targetStyle == null) { targetStyle = targetWorkbook.createCellStyle(); targetStyle.cloneStyleFrom(sourceStyle); styleCache.put(styleHashCode, targetStyle); } targetCell.setCellStyle(targetStyle); targetCell.setCellValue(sourceCell.getStringCellValue()); } } } ``` 在上述代码,缓存CellStyle的HashMap对象被命名为styleCache。在复制每个单元格时,首先获取单元格的CellStyle对象,并计算其hashcode。如果HashMap已经存在相同hashcode的CellStyle,直接获取并应用到目标单元格;否则,创建新的CellStyle对象并应用到目标单元格,并将其存储到HashMap。 希望以上内容能够帮到您。如果您有任何问题或需要进一步的帮助,请随时告诉我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值