2015-05-13 4 views
0

Я объявил внутренние переменные таким образом:Установить CellStyle в ячейку или строку в HSSFWorkbook

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

В середине класса, после того, как я собираю некоторые ВАЖНЫЕ данные я отправить его к следующей функции, но в в результате, кажется, что никаких изменений не выполняется на моем первенствовать:

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); 
} 
+0

Я не могу видеть код, в котором вы пишете изменили Excel файл из - вы забыли позвонить 'write', чтобы сохранить его? – Gagravarr

+0

нет ... это позже в коде. превосходная печать хорошо не изменилась. – Udi

+0

@Gagravarr у вас есть какая-то другая идея, пожалуйста? – Udi

ответ

0
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); 
     } 
     }