Java 将单元格样式设置为HSSF工作簿中的单元格或行

Java 将单元格样式设置为HSSF工作簿中的单元格或行,java,apache-poi,stylesheet,poi-hssf,Java,Apache Poi,Stylesheet,Poi Hssf,我以这种方式声明了内部变量: FileOutputStream fos; HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("analyze_result"); Row row = sheet.createRow(rowIndex); Cell cell = row.createCell(cellIndex); HSSFCellStyle cs = workbook.createC

我以这种方式声明了内部变量:

FileOutputStream fos;
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("analyze_result");
Row row = sheet.createRow(rowIndex);
Cell cell = row.createCell(cellIndex);
HSSFCellStyle cs = workbook.createCellStyle();
在课堂中间,我收集了一些重要数据后,将其发送到下一个函数,但结果似乎没有对我的excel执行任何更改:

private void firstWriteToExcel(DefectFileReader curDF , HSSFSheet sheet , HSSFWorkbook workbook , FileOutputStream fos) throws Exception{

    cs.setWrapText(true);
    cs.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);

    if(recipeName.getState() == true){
        row = sheet.createRow(rowIndex - 1);
        row.createCell(cellIndex).setCellValue("Recipe Name: " + curDF.getRecipeName());
        cell.setCellStyle(cs);

    }
    row = sheet.createRow(rowIndex++);
    row.createCell(cellIndex++).setCellValue("inspection index");
    row.setRowStyle(cs);
    if(numberOfDefects.getState() == true){
        System.out.println("in printing to excel");
        row.createCell(cellIndex++).setCellValue("Total Defects");
    }
    if(numberOfDefectsPerDetector.getState() == true){
        ArrayList<String> toOtherFunc = new ArrayList<String>();
        toOtherFunc = curDF.getDetectorNames();
        for(int i=0; i < toOtherFunc.size() ; i++)
            row.createCell(cellIndex++).setCellValue(toOtherFunc.get(i));   
    }
    if(sensetivityName.getState() == true)
        row.createCell(cellIndex++).setCellValue("sensetivity Name");
    if(inspectionDuration.getState()==true)
        row.createCell(cellIndex++).setCellValue("Inspection Duration");

    row.setRowStyle(cs);

    System.out.println("row index is: " + rowIndex + "cell index is: " + cellIndex);
}
private void firstWriteToExcel(缺陷文件读取器curDF、HSSF工作表、HSSF工作簿、FileOutputStream fos)引发异常{
cs.setWrapText(真);
cs.setFillForegroundColor(HSSFColor.灰色25%指数);
if(recipeName.getState()==true){
行=sheet.createRow(行索引-1);
createCell(cellIndex).setCellValue(“配方名称:+curDF.getRecipeName());
cell.setCellStyle(cs);
}
row=sheet.createRow(rowIndex++);
row.createCell(cellIndex++).setCellValue(“检查索引”);
row.setRowStyle(cs);
if(numberOfDefects.getState()==true){
System.out.println(“打印到excel中”);
row.createCell(cellIndex++).setCellValue(“总缺陷”);
}
if(numberOfDefectsPerDetector.getState()==true){
ArrayList toOtherFunc=新的ArrayList();
toOtherFunc=curDF.getDetectorNames();
对于(int i=0;i
私有void writeToExcel(DefectFileReader curDF、XSSFSheet工作表、XSSFWorkbook工作簿、FileOutputStream fos)引发异常{
fontNotForBold.setBold(false);
csForSecondWrite.setFont(fontNotForBold);
setFillForegroundColor(新的XSSFColor(java.awt.Color.white));
csForSecondWrite.setFillPattern(CellStyle.SOLID\u前景);
csForSecondWrite.setboordbottom(XSSFCellStyle.BORDER_-THIN);
csForSecondWrite.setboordtop(XSSFCellStyle.BORDER_-THIN);
csForSecondWrite.setBorderRight(XSSFCellStyle.BORDER_-THIN);
csForSecondWrite.setboordleft(XSSFCellStyle.BORDER_-THIN);
细胞指数=4;
row=sheet.createRow(rowIndex++);
row.createCell(cellIndex++).setCellValue(“检查”+(rowIndex-3));
if(numberOfDefects.getState()==true)
row.createCell(cellIndex++).setCellValue(curDF.getTotalEffects());
if(numberOfDefectsPerDetector.getState()==true){
ArrayList toOtherFunc=新的ArrayList();
int[]toPrintNumber=新int[toOtherFunc.size()];
toOtherFunc=curDF.getDetectorNames();
toPrintNumber=curDF.getDefectPerDetector(TOOTERFUNC);
对于(int i=0;i2){
用于(单元\单元本地:行){
cell_local.setCellStyle(csForSecondWrite);
}
}

我看不到任何代码,您在其中写入了更改后的Excel文件-您是否忘记调用
write
来保存它?不…它在代码的后面。Excel打印很好,只是没有看到样式更改。@Gagravarr您有什么不同的想法吗?为什么在循环中将单元格样式设置为灰色?单元格样式是工作簿范围内的,因此,您应该只设置一次。看起来您正在先创建单元格并填充数据,然后再设置行样式。在该行中创建任何单元格之前,请尝试在该行上设置行样式。此外,在添加任何数据之前,最好为工作簿创建两种不同的单元格样式(例如一个“正常”和一个“灰色”),然后在需要时应用不同的样式,而不是动态更改样式属性,这可能会导致意外的结果。
private void writeToExcel(DefectFileReader curDF , XSSFSheet sheet , XSSFWorkbook workbook , FileOutputStream fos) throws Exception{

        fontNotForBold.setBold(false);
        csForSecondWrite.setFont(fontNotForBold);
        csForSecondWrite.setFillForegroundColor(new XSSFColor(java.awt.Color.white));
        csForSecondWrite.setFillPattern(CellStyle.SOLID_FOREGROUND);
        csForSecondWrite.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        csForSecondWrite.setBorderTop(XSSFCellStyle.BORDER_THIN);
        csForSecondWrite.setBorderRight(XSSFCellStyle.BORDER_THIN);
        csForSecondWrite.setBorderLeft(XSSFCellStyle.BORDER_THIN);
    cellIndex = 4;
    row = sheet.createRow(rowIndex++);
    row.createCell(cellIndex++).setCellValue("inspection_" + (rowIndex - 3));
    if(numberOfDefects.getState() == true)
        row.createCell(cellIndex++).setCellValue(curDF.getTotalDefects());
    if(numberOfDefectsPerDetector.getState() == true){
        ArrayList<String> toOtherFunc = new ArrayList<String>();
        int[] toPrintNumber = new int[toOtherFunc.size()];
        toOtherFunc = curDF.getDetectorNames();
        toPrintNumber = curDF.getDefectPerDetector(toOtherFunc);
        for(int i=0; i < toOtherFunc.size() ; i++)
            row.createCell(cellIndex++).setCellValue(toPrintNumber[i]); 
    }
    if(sensetivityName.getState() == true)
        row.createCell(cellIndex++).setCellValue(curDF.getSensetivityName());
    if(LOGloaded == true){
        LogFileReader logReader = new LogFileReader(LOG , curDF.getInspectionEndTime());
        ArrayList<String> toLog = curDF.getRunsIndex();
        String toPrint = "";
        for(int i=0 ; i < toLog.size() ; i = i+2)
            toPrint += toLog.get(i) + ": " + logReader.diagnozeLog(toLog.get(i+1)) + " ";
        if(toPrint.equals(""))
            row.createCell(cellIndex).setCellValue("The data of this run is not in this log!");
        else
            row.createCell(cellIndex).setCellValue(toPrint);
        sheet.autoSizeColumn(cellIndex);
        cellIndex++;

    }
    if(inspectionDuration.getState()==true)
        row.createCell(cellIndex++).setCellValue(curDF.inspectionDuration());


    for (Row row : sheet){
        if( row.getRowNum() > 2){
        for (Cell cell_local : row){
            cell_local.setCellStyle(csForSecondWrite);
        }
        }