When creating a new cell, copy the previous cell style into Apache POI ..?

In my java class, I declare the cell as:

HSSFCell cell = null;

And I use this cell in many places to create a cell and set values, styles. How:

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

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

Now, surprisingly, the first cell data format is applied to the 2nd cell. Does anyone have an idea? I expect that the style of the second cell will not be. And the style of the 1st cell should be with the data format used by the setCellStyle () method. But in fact, I get both cell values โ€‹โ€‹with the data format used by the setCellStyle () method.

setCellStyle ():

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)")); 
        }
}
+4
source share
1 answer

, , setCellStyle, . Cell CellStyle, CellStyle. setCellStyle Cell, CellStyle, . , , CellStyle, . , , setCellStyle, .

CellStyle, Cell, Workbook createCellStyle.

HSSFCellStyle cellStyle = workbook.createCellStyle();

, CellStyle.

, , .

+4

Source: https://habr.com/ru/post/1616776/


All Articles