Apache POI样式应用于所有单元格 [英] Apache POI style getting applied to all cells

查看:173
本文介绍了Apache POI样式应用于所有单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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());

我想在上述两个列上应用日期格式。但它正在应用于所有的单元格。

解决方案

正如文档所述,Cell.getCellStyle()将永远不会返回null。



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



当没有为单元格显式设置单元格样式,那么它将返回最初在工作簿中的所有单元格之间共享的默认单元格样式。您将需要创建一个新的CellStyle,然后将其分配给相关的单元格。



从POI开发人员指南:



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

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

//创建一行并在其中放入一些单元格。行为0。
Row row = sheet.createRow(0);

//创建一个单元格并在其中放入一个日期值。第一个单元格的样式不是
//作为日期。
单元格= row.createCell(0);
cell.setCellValue(new Date());

//我们将第二个单元格设为日期(和时间)。重要的是要
//从工作簿中创建一个新的单元格样式,否则你最终可以结束
//修改内置样式,并且不仅影响这个单元格,而且还会影响其他单元格。
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);

//您还可以将日期设置为java.util.Calendar
cell = row.createCell(2);
cell.setCellValue(Calendar.getInstance());
cell.setCellStyle(cellStyle);

//将输出写入文件
FileOutputStream fileOut = new FileOutputStream(workbook.xls);
wb.write(fileOut);
fileOut.close();


    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.

解决方案

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

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.

From the POI developer guide:

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();

这篇关于Apache POI样式应用于所有单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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