简体   繁体   中英

add fill effects for creating excel with apache poi

XSSFCellStyle xssfCellStyle = (XSSFCellStyle) cellStyle;
        xssfCellStyle.setFillForegroundColor(new XSSFColor(color));
        xssfCellStyle.setFillPattern(XSSFCellStyle.DIAMONDS);

Hey,

I am able to set for my exported excel all the pattern styles, i can reach from excel, BUT how can i set the "Fill Effects..." via apache poi. Is there any possibility?

Thank you all for your help

PS: picture shows, what i mean by pattern style and fill effects

picture of excel: format cell editor

Apache POI does currently not have support for the "Fill Effects..." in the high-level API.

But the lowlevel API generated from the specifications should allow to set these things up, but you have to determine the structure yourself, either based on the specifications for the MS documents or by creating a small sample spreadsheet and looking at the file xl\\styles.xml inside the .xlsx (which is a simple zip-file actually!).

Then something like the following should allow you to build the required xml-structure, with the actual calls on ctGradienFill depending on the type of fill you want to make:

        final CellStyle cellStyle = wb.createCellStyle();
        //cellStyle.setFillPattern(FillPatternType.ALT_BARS);

        XSSFCellFill fill = new XSSFCellFill(CTFill.Factory.newInstance());
        final CTGradientFill ctGradientFill = fill.getCTFill().addNewGradientFill();
        ctGradientFill.setDegree(45);
        // ...
        ((XSSFWorkbook)wb).getStylesSource().putFill(fill);

        cell.setCellStyle(cellStyle);

Note: you will need the full schema-jar as described at http://poi.apache.org/faq.html#faq-N10025 to get the additional CTGradientFill class.

gradient-fill-example

this helps a lot, after having understood the xml structure :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM