创建一个新单元格,在 Apache POI 中复制上一个单元格的样式..? [英] Creating a new cell, copies previous cell's style in Apache POI..?

查看:41
本文介绍了创建一个新单元格,在 Apache POI 中复制上一个单元格的样式..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的java类中,我声明单元格如下:

In my java class I am declaring cell like:

HSSFCell cell = null;

我在很多地方使用这个单元格来创建一个单元格并设置值、样式.喜欢:

And I am using this cell in many places to create a cell and set values, styles. Like:

cell = row.createCell(1);               
cell.setCellValue("1234.00");
setCellStyle(currency, cell, workbook);

cell = row.createCell(2);               
setCellValue("2445.00");

现在,令人惊讶的是,第一个单元格的数据格式正在应用于第二个单元格.任何人有任何想法?我希望第二个单元格的样式为 none.第一个单元格的样式应该是 setCellStyle() 方法应用的数据格式.但是,实际上我正在通过 setCellStyle() 方法应用数据格式获取两个单元格值.

Now, surprisingly, the first cell's data format is being applied to 2nd cell. Anyone has any idea? I expect the 2nd cell's style to be none. And the 1st cell's style should be with the data format applied by setCellStyle() method. But, actually I am getting both the cell values with data format applied by setCellStyle() method.

setCellStyle() 方法:

setCellStyle() method:

public void setCellStyle(String currency, HSSFCell cell, HSSFWorkbook workbook){

        HSSFCellStyle cellStyle = cell.getCellStyle();//I am using cell.getStyle() because the default cell style is not null for a HSSFCell

        HSSFDataFormat dataFormat = workbook.createDataFormat();

        if("GBP".equalsIgnoreCase(currency)){
            cellStyle.setDataFormat(dataFormat.getFormat("[$£-809]#,##0_);[Red]([$£-809]#,##0)"));                 
        }else (){
            cellStyle.setDataFormat(dataFormat.getFormat("$#,##0_);[Red]($#,##0)")); 
        }
}

推荐答案

现在您已经更新了您的帖子以显示您的 setCellStyle 方法,我可以看到问题所在.每个 Cell 都以默认的 CellStyle 开始,并且它们都共享相同的默认 CellStyle.当您为第一个 Cell 调用 setCellStyle 时,您正在更改所有单元格共享的默认 CellStyle.这意味着您创建的任何其他单元格(您未设置 CellStyle 的位置)都有您的更改.此外,任何其他时间您调用自己的 setCellStyle,它都会再次更改默认单元格样式.

Now that you've updated your post to show your setCellStyle method, I can see the problem. Every Cell starts out with a default CellStyle, and they all share the same default CellStyle. When you call setCellStyle for the first Cell, you are changing that default CellStyle that is shared for all cells. This means that any other cells you create, where you don't set the CellStyle have your changes. Additionally, any other time you call your own setCellStyle, it will change the default cell style again.

相反,使用 WorkbookcreateCellStyle 方法.

Instead, create a new CellStyle that only that Cell will have, using Workbook's createCellStyle method.

HSSFCellStyle cellStyle = workbook.createCellStyle();

如果您打算将多个单元格设置为相同的单元格样式,那么您应该重用 CellStyle 对象.

If you intend to set multiple cells to the same cell style, then you should reuse the CellStyle objects.

从工作簿创建新的单元格样式很重要,否则您最终可能会修改内置样式并不仅影响此单元格,还会影响其他单元格.

It is important to create a new cell style from the workbook otherwise you can end up modifying the built in style and effecting not only this cell but other cells.

这篇关于创建一个新单元格,在 Apache POI 中复制上一个单元格的样式..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆