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

查看:1003
本文介绍了创建一个新的单元格,在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");

现在,令人惊讶的是,第一个单元格的数据格式正在应用于第二个单元格. 有人有主意吗? 我希望第二个单元格的样式为空.并且第一个单元格的样式应采用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.

相反,使用Cell将具有的新CellStyle. html#createCellStyle()"rel =" nofollow> 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天全站免登陆