Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java和Apache POI库覆盖excel文件中的单元格样式?_Java_Excel_Apache Poi - Fatal编程技术网

如何使用java和Apache POI库覆盖excel文件中的单元格样式?

如何使用java和Apache POI库覆盖excel文件中的单元格样式?,java,excel,apache-poi,Java,Excel,Apache Poi,我正在使用java和ApachePOI库对input.xlsx文件进行excel验证 下面我发布了java类中的两个函数。当我尝试设置单元格样式时,它不会反映在excel文件中。我在互联网上搜索过,但他们在创建单元格/行时,到处都给出了代码来赋予样式 public static CellStyle getNewCellStyle(){ CellStyle style = myWorkBook.createCellStyle(); style.setFillBackgroundCo

我正在使用java和ApachePOI库对input.xlsx文件进行excel验证

下面我发布了java类中的两个函数。当我尝试设置单元格样式时,它不会反映在excel文件中。我在互联网上搜索过,但他们在创建单元格/行时,到处都给出了代码来赋予样式

public static CellStyle getNewCellStyle(){
    CellStyle style = myWorkBook.createCellStyle();
    style.setFillBackgroundColor(IndexedColors.GREEN.getIndex());
    style.setFillPattern(CellStyle.ALIGN_FILL); 
    return style;
}


public static void chCaseNumberColumnValidation(Cell cell){
    String cellData = getCellDataValue(cell);
    if(cellData.length() == 10){
        if(cellData.equals("BLANK") || cellData.trim().length() == 0){
            System.out.println("BLANK CELL:   " + cell.getRowIndex() + "," + cell.getColumnIndex());
        }

        if(cellData.charAt(0) != '5'){
            System.out.println("DON't START WITH 5:    " + cell.getRowIndex() + "," + cell.getColumnIndex());
            cell.setCellStyle(getNewCellStyle());
        }
    }
    else{
        System.out.println("****INVALID SIZE   " + cell.getRowIndex() + "," + cell.getColumnIndex());

    }

}

有没有什么方法可以给已经存在的单元格添加背景色。(更改单元格样式)

填充和颜色

Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");

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

// Aqua background
CellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.BIG_SPOTS);
Cell cell = row.createCell((short) 1);
cell.setCellValue("X");
cell.setCellStyle(style);

// Orange "foreground", foreground being the fill foreground not the font color.
style = wb.createCellStyle();
style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 2);
cell.setCellValue("X");
cell.setCellStyle(style);

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

单元格样式是工作簿范围而不是单元格范围,如果只创建一次样式并在所有单元格上重复使用,会发生什么情况?@Gagravarr Creating Cell style是不同的。让我们忽略它。我的问题是,我想将单元格样式从现有样式替换为新样式。我怎么做?若我尝试读取行并更新某些特定行的单元格样式,那个么它不会更新。如果我创建单元并应用单元样式,并将单元添加到图纸中,则它可以工作。但给现有单元赋予样式是行不通的。因为工作簿中已经存在一些数据。Excel在工作簿中只允许有这么多的单元格样式。如果为每个单元格创建一个,则很快就会用完,样式设置将无法工作!这就是为什么需要创建一次单元格样式并重复使用我不想创建任何行/单元格。我正在读输入的excel表格。您的解决方案仅在创建单元格/行时有效。它不适用于现有单元格/行。请更新或删除它。你能告诉我们更多关于最终目标的信息吗?如果您不打算将更改写入文件,那么更新工作簿中单元格格式的目的是什么?