将Apache POI样式应用于所有单元格

时间:2022-09-30 20:25:35
    Cell cell = row.createCell(1);
    cell.setCellValue(rdf.getEffectiveDate());
    cell.getCellStyle().setDataFormat(HSSFDataFormat.getBuiltinFormat("d-mmm-yy"));

    cell = row.createCell(2);
    cell.setCellValue(rdf.getExpiryDate());
    cell.getCellStyle().setDataFormat(HSSFDataFormat.getBuiltinFormat("d-mmm-yy"));

    row.createCell(3).setCellValue(rdf.getPremium());
    row.createCell(4).setCellValue(rdf.getAccountNumber());
    row.createCell(5).setCellValue(rdf.getLedgerName());

I wanted to apply Date Format on two of the above columns. But it is getting applied to all the cells. How can I prevent this.

我想把日期格式应用到上面的两列。但是它被应用到所有的细胞中。我怎样才能避免这种情况。

3 个解决方案

#1


12  

As the documentation states, Cell.getCellStyle() will never return null.

作为文档状态,Cell.getCellStyle()永远不会返回null。

https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html#getCellStyle()

https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html getCellStyle()

When no cell style has been explicitly set for a Cell then it will return the default cell style which is initially shared among all cells in the workbook. Changing this then will obviously affect all cells not having an explictly assigned style.

当没有为单元格显式设置单元格样式时,它将返回默认的单元格样式,该样式最初在工作簿中的所有单元格*享。这样做的改变显然会影响到没有明确指定样式的所有单元格。

You need to create a new CellStyle and then assign this to the relevant cells.

您需要创建一个新的CellStyle,然后将其分配给相关的单元格。

From the POI developer guide:

来自POI开发者指南:

https://poi.apache.org/spreadsheet/quick-guide.html#CreateDateCells

https://poi.apache.org/spreadsheet/quick-guide.html CreateDateCells

Workbook wb = new HSSFWorkbook();
    //Workbook wb = new XSSFWorkbook();
    CreationHelper createHelper = wb.getCreationHelper();
    Sheet sheet = wb.createSheet("new sheet");

    // Create a row and put some cells in it. Rows are 0 based.
    Row row = sheet.createRow(0);

    // Create a cell and put a date value in it.  The first cell is not styled
    // as a date.
    Cell cell = row.createCell(0);
    cell.setCellValue(new Date());

    // we style the second cell as a date (and time).  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.
    CellStyle cellStyle = wb.createCellStyle();
    cellStyle.setDataFormat(
        createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
    cell = row.createCell(1);
    cell.setCellValue(new Date());
    cell.setCellStyle(cellStyle);

    //you can also set date as java.util.Calendar
    cell = row.createCell(2);
    cell.setCellValue(Calendar.getInstance());
    cell.setCellStyle(cellStyle);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();

#2


3  

Try creating a new cell style. I think you may be changing the default style. So something like this...

尝试创建一个新的单元格样式。我认为你可能正在改变默认的风格。所以这样的…

CellStyle dateTimeCS = wb.createCellStyle();
dateTimeCS.setDataFormat(HSSFDataFormat.getBuiltinFormat("d-mmm-yy"));
cell.setCellStyle(dateTimeCS);

#3


0  

use RegionUtil to apply borders to a range of cells

使用RegionUtil对一系列单元格应用边框

https://poi.apache.org/apidocs/org/apache/poi/ss/util/RegionUtil.html

https://poi.apache.org/apidocs/org/apache/poi/ss/util/RegionUtil.html

looks like it was added in version 3.15

看起来它是在3.15版本中添加的

#1


12  

As the documentation states, Cell.getCellStyle() will never return null.

作为文档状态,Cell.getCellStyle()永远不会返回null。

https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html#getCellStyle()

https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html getCellStyle()

When no cell style has been explicitly set for a Cell then it will return the default cell style which is initially shared among all cells in the workbook. Changing this then will obviously affect all cells not having an explictly assigned style.

当没有为单元格显式设置单元格样式时,它将返回默认的单元格样式,该样式最初在工作簿中的所有单元格*享。这样做的改变显然会影响到没有明确指定样式的所有单元格。

You need to create a new CellStyle and then assign this to the relevant cells.

您需要创建一个新的CellStyle,然后将其分配给相关的单元格。

From the POI developer guide:

来自POI开发者指南:

https://poi.apache.org/spreadsheet/quick-guide.html#CreateDateCells

https://poi.apache.org/spreadsheet/quick-guide.html CreateDateCells

Workbook wb = new HSSFWorkbook();
    //Workbook wb = new XSSFWorkbook();
    CreationHelper createHelper = wb.getCreationHelper();
    Sheet sheet = wb.createSheet("new sheet");

    // Create a row and put some cells in it. Rows are 0 based.
    Row row = sheet.createRow(0);

    // Create a cell and put a date value in it.  The first cell is not styled
    // as a date.
    Cell cell = row.createCell(0);
    cell.setCellValue(new Date());

    // we style the second cell as a date (and time).  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.
    CellStyle cellStyle = wb.createCellStyle();
    cellStyle.setDataFormat(
        createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
    cell = row.createCell(1);
    cell.setCellValue(new Date());
    cell.setCellStyle(cellStyle);

    //you can also set date as java.util.Calendar
    cell = row.createCell(2);
    cell.setCellValue(Calendar.getInstance());
    cell.setCellStyle(cellStyle);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();

#2


3  

Try creating a new cell style. I think you may be changing the default style. So something like this...

尝试创建一个新的单元格样式。我认为你可能正在改变默认的风格。所以这样的…

CellStyle dateTimeCS = wb.createCellStyle();
dateTimeCS.setDataFormat(HSSFDataFormat.getBuiltinFormat("d-mmm-yy"));
cell.setCellStyle(dateTimeCS);

#3


0  

use RegionUtil to apply borders to a range of cells

使用RegionUtil对一系列单元格应用边框

https://poi.apache.org/apidocs/org/apache/poi/ss/util/RegionUtil.html

https://poi.apache.org/apidocs/org/apache/poi/ss/util/RegionUtil.html

looks like it was added in version 3.15

看起来它是在3.15版本中添加的