2014-05-11 14 views
9

Mein System erstellt viele verschiedene Excel-Berichte mit Apache POI aus Java.Eine CellStyle-Bibliothek in Apache POI erstellen

Viele dieser Berichte teilen die gleichen Stile.

Ich habe eine CellStyle-Bibliothek erstellt, die von allen Berichten verwendet werden kann. Ich fragte mich, ob es einen besseren Weg gab.

import org.apache.poi.hssf.util.HSSFColor; 
import org.apache.poi.ss.usermodel.CellStyle; 
import org.apache.poi.ss.usermodel.Workbook; 

public class CellStyles { 
    CellStyle headingCellStyle = null; 
    Workbook wb; 

    public CellStyles(Workbook wb) { 
     this.wb = wb; 
    } 

    public CellStyle getHeadingCellStyle() { 
     if (headingCellStyle == null) { 
      headingCellStyle = wb.createCellStyle(); 
      headingCellStyle.setFillForegroundColor(HSSFColor.YELLOW.index); 
      headingCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); 
     } 
     return headingCellStyle; 
    } 

} 

und dann

Workbook wb = new XSSFWorkbook(inputStream); // XSSF for .xlsm 
CellStyles cs = new CellStyles(wb); 


CellUtil.getCell(myRow, 2).setCellStyle(cs.getHeadingCellStyle()); 
+1

Scheint gut genug –

Antwort

7

Aufruf Ich denke, dass seine Einfachheit dieser Lösung gegeben, es ist gut.

Leider in POI CellStyle muss von Workbook erstellt werden, so dass Sie nicht wirklich vermeiden können, wb als Parameter in irgendeiner Weise zu übergeben.

Neben der Implementierung können Sie versuchen, in CellStyles ein Bündel von static Methoden aufzudecken wb als Parameter zu nehmen und den Stil der Rückkehr, so dass Sie nicht cs Objekt um in Ihrem Code übergeben müssen. Obwohl ich nicht sagen würde, dass es sich lohnt, es richtig zu machen, müssten Sie den statischen Cache von Map[Workbook, CellStyles] Zuordnungen pflegen, die für die Rückgabe des Stils verwendet würden.

Lazy Initialisierung von Stilen funktioniert auch gut und ermöglicht es Ihnen, doppelte Stile zu vermeiden, obwohl es besser wäre, Stile privat ie private CellStyle headingCellStyle = null;, um sicherzustellen, dass nichts die Stilzuweisung außerhalb der Klasse und nullwertige headerCellStyle nicht ändern kann durch Zufall benutzt werden.

4

Ich habe ein Projekt, das HTML/CSS in verschiedene Formate konvertiert, einschließlich Excel und ODF. Wenn es irgendeinen Nutzen bringt, mache ich folgendes, wenn Style eine Klasse ist, die die verschiedenen Eigenschaften enthält, die aus dem CSS extrahiert wurden.

public class ExcelStyleGenerator { 
    private Map<Style, XSSFCellStyle> styles; 

    public ExcelStyleGenerator() { 
     styles = new HashMap<Style, XSSFCellStyle>(); 
    } 

    public CellStyle getStyle(Cell cell, Style style) { 
     XSSFCellStyle cellStyle; 

     if (styles.containsKey(style)) { 
      cellStyle = styles.get(style); 
     } else { 
      cellStyle = (XSSFCellStyle) cell.getSheet().getWorkbook().createCellStyle(); 

      applyBackground(style, cellStyle); 
      applyBorders(style, cellStyle); 
      applyFont(cell, style, cellStyle); 
      applyHorizontalAlignment(style, cellStyle); 
      applyverticalAlignment(style, cellStyle); 
      applyWidth(cell, style); 

      styles.put(style, cellStyle); 
     } 

     return cellStyle; 
    } 

    protected void applyBackground(Style style, XSSFCellStyle cellStyle) { 
     if (style.isBackgroundSet()) { 
      cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); 
      cellStyle.setFillForegroundColor(new XSSFColor(style.getProperty(CssColorProperty.BACKGROUND))); 
     } 
    } 

    protected void applyBorders(Style style, XSSFCellStyle cellStyle) { 
     if (style.isBorderWidthSet()) { 
      short width = (short) style.getProperty(CssIntegerProperty.BORDER_WIDTH); 

      Color color = style.getProperty(CssColorProperty.BORDER_COLOR) != null ? style 
        .getProperty(CssColorProperty.BORDER_COLOR) : Color.BLACK; 

      cellStyle.setBorderBottom(BorderStyle.THIN); 
      cellStyle.setBorderBottom(width); 
      cellStyle.setBottomBorderColor(new XSSFColor(color)); 

      cellStyle.setBorderTop(BorderStyle.THIN); 
      cellStyle.setBorderTop(width); 
      cellStyle.setTopBorderColor(new XSSFColor(color)); 

      cellStyle.setBorderLeft(BorderStyle.THIN); 
      cellStyle.setBorderLeft(width); 
      cellStyle.setLeftBorderColor(new XSSFColor(color)); 

      cellStyle.setBorderRight(BorderStyle.THIN); 
      cellStyle.setBorderRight(width); 
      cellStyle.setRightBorderColor(new XSSFColor(color)); 
     } 
    } 

    protected void applyFont(Cell cell, Style style, XSSFCellStyle cellStyle) { 
     Font font = createFont(cell.getSheet().getWorkbook(), style); 
     cellStyle.setFont(font); 
    } 

    protected void applyHorizontalAlignment(Style style, XSSFCellStyle cellStyle) { 
     if (style.isHorizontallyAlignedLeft()) { 
      cellStyle.setAlignment(HorizontalAlignment.LEFT); 
     } else if (style.isHorizontallyAlignedRight()) { 
      cellStyle.setAlignment(HorizontalAlignment.RIGHT); 
     } else if (style.isHorizontallyAlignedCenter()) { 
      cellStyle.setAlignment(HorizontalAlignment.CENTER); 
     } 
    } 

    protected void applyverticalAlignment(Style style, XSSFCellStyle cellStyle) { 
     if (style.isVerticallyAlignedTop()) { 
      cellStyle.setVerticalAlignment(VerticalAlignment.TOP); 
     } else if (style.isVerticallyAlignedBottom()) { 
      cellStyle.setVerticalAlignment(VerticalAlignment.BOTTOM); 
     } else if (style.isVerticallyAlignedMiddle()) { 
      cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); 
     } 
    } 

    protected void applyWidth(Cell cell, Style style) { 
     if (style.getProperty(CssIntegerProperty.WIDTH) > 0) { 
      cell.getSheet().setColumnWidth(cell.getColumnIndex(), style.getProperty(CssIntegerProperty.WIDTH) * 50); 
     } 
    } 

    public Font createFont(Workbook workbook, Style style) { 
     Font font = workbook.createFont(); 

     if (style.isFontNameSet()) { 
      font.setFontName(style.getProperty(CssStringProperty.FONT_FAMILY)); 
     } 

     if (style.isFontSizeSet()) { 
      font.setFontHeightInPoints((short) style.getProperty(CssIntegerProperty.FONT_SIZE)); 
     } 

     if (style.isColorSet()) { 
      Color color = style.getProperty(CssColorProperty.COLOR); 

      // if(! color.equals(Color.WHITE)) // POI Bug 
      // { 
      ((XSSFFont) font).setColor(new XSSFColor(color)); 
      // } 
     } 

     if (style.isFontBold()) { 
      font.setBoldweight(Font.BOLDWEIGHT_BOLD); 
     } 

     font.setItalic(style.isFontItalic()); 

     if (style.isTextUnderlined()) { 
      font.setUnderline(Font.U_SINGLE); 
     } 

     return font; 
    } 
} 
+0

Vielen Dank. Dieser Code würde definitiv dazu beitragen, wiederholten Code beim Erstellen der Style-Bibliothek zu vermeiden. – gordon613