Example usage for org.apache.poi.xssf.usermodel.extensions XSSFCellBorder XSSFCellBorder

List of usage examples for org.apache.poi.xssf.usermodel.extensions XSSFCellBorder XSSFCellBorder

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel.extensions XSSFCellBorder XSSFCellBorder.

Prototype

public XSSFCellBorder(CTBorder border, IndexedColorMap colorMap) 

Source Link

Usage

From source file:fr.amapj.service.engine.generator.excel.ExcelGeneratorTool.java

License:Open Source License

/**
 * Permet d'ajouter une croix (deux diagnonales) sur la cellule
 * /*from w  w  w  .jav  a2  s .c  om*/
 * Voir la classe DiagonalBorder pour plus d'explications sur HSSFWorkbook
 * 
 * Voir 
 * https://stackoverflow.com/questions/39529042/apache-poi-how-to-add-diagonal-border
 * pour plus d'explications sur XSSFWorkbook
 */
private void addDiagonalBorder(CellStyle style) {
    short lineStyle = CellStyle.BORDER_THIN;

    try {
        if (wb instanceof HSSFWorkbook) {
            Field f = HSSFCellStyle.class.getDeclaredField("_format");
            f.setAccessible(true);

            ExtendedFormatRecord efr = (ExtendedFormatRecord) f.get(style);

            efr.setIndentNotParentBorder(true);
            efr.setDiag((short) 3);

            //   
            efr.setAdtlDiag((short) 64);
            efr.setAdtlDiagLineStyle(lineStyle);
        } else {
            Method m = XSSFCellStyle.class.getDeclaredMethod("getCTBorder");
            m.setAccessible(true);

            Field f1 = XSSFCellStyle.class.getDeclaredField("_stylesSource");
            f1.setAccessible(true);

            Field f2 = XSSFCellStyle.class.getDeclaredField("_theme");
            f2.setAccessible(true);

            CTBorder ct = (CTBorder) m.invoke(style);
            CTXf _cellXf = ((XSSFCellStyle) style).getCoreXf();
            StylesTable _stylesSource = (StylesTable) f1.get(style);
            ThemesTable _theme = (ThemesTable) f2.get(style);

            CTBorderPr pr = ct.isSetDiagonal() ? ct.getDiagonal() : ct.addNewDiagonal();
            if (lineStyle == BorderFormatting.BORDER_NONE) {
                ct.unsetDiagonal();
            } else {
                ct.setDiagonalDown(true);
                ct.setDiagonalUp(true);
                pr.setStyle(STBorderStyle.Enum.forInt(lineStyle + 1));
            }
            int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme));
            _cellXf.setBorderId(idx);
            _cellXf.setApplyBorder(true);
        }
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
            | NoSuchMethodException | InvocationTargetException e) {
        throw new AmapjRuntimeException(e);
    }
}