单元格Excel内部循环中缺less样式

我有下一个类文件生成Excel的pipe理风格:

public class excelUtil { public void setCellBorder(HSSFCellStyle style) { style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM); style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM); style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM); style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM); } public HSSFCellStyle setStyle(HSSFWorkbook workbook, String nameStyle) { HSSFFont font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); HSSFCellStyle style = workbook.createCellStyle(); if (nameStyle.equals("titulo")) { style.setAlignment(org.apache.poi.ss.usermodel.CellStyle.ALIGN_CENTER); style.setFont(font); } if (nameStyle.equals("negrita")) { style.setFont(font); } if (nameStyle.equals("celdaNormal")) { setCellBorder(style); } if (nameStyle.equals("celdaDecimal")) { setCellBorder(style); style.setDataFormat(workbook.createDataFormat().getFormat("#,###,###,##0.00")); } if (nameStyle.equals("celdaPorcentaje")){ setCellBorder(style); style.setDataFormat(workbook.createDataFormat().getFormat("0%")); } if (nameStyle.equals("celdaTitulo")) { setCellBorder(style); style.setAlignment(org.apache.poi.ss.usermodel.CellStyle.ALIGN_CENTER); style.setFont(font); } return style; } 

当我使用这个类如预期的那样工作时:

 HSSFWorkbook workbook= new HSSFWorkbook(); HSSFSheet sheet= workbook.createSheet("Sheet"); HSSFRow row; excelUtil excel = new excelUtil(); row.createCell(0).setCellValue("TITLE"); row.getCell(0).setCellStyle(excel.setStyle(workbook, "titulo")); 

当我在一个循环中使用这个问题的时候,问题就出现了,在某种程度上,样式不再被定义,例如:

  for (mdlCell cell : cells) { actualColumn++; if (cell.getType().equals("number")) { row.createCell(actualColumn).setCellValue(Double.valueOf(cell.getValue()).doubleValue()); row.getCell(actualColumn).setCellStyle(excel.setStyle(workbook, "celdaDecimal")); } else if (cell.getType().equals("text")) { row.createCell(actualColumn).setCellValue(cell.getValue()); row.getCell(actualColumn).setCellStyle(excel.setStyle(workbook, "celdaNormal")); } else if (cell.getType().equals("percent")) { row.createCell(actualColumn).setCellValue(cell.getValue()); row.getCell(actualColumn).setCellStyle(excel.setStyle(workbook, "celdaPorcentaje")); } } 

和输出结果是这样的:

在这里输入图像说明