List of usage examples for org.apache.poi.hssf.record ExtendedFormatRecord setAdtlDiag
public void setAdtlDiag(short diag)
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 * /* w w w .j ava2 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); } }
From source file:fr.amapj.service.engine.generator.excel.samples.DiagonalBorder.java
License:Open Source License
/** * Permet la generation d'un fichier basique avec 3 lignes et 3 colonnes *///from w ww .j a va 2 s . c o m private void generateFileWithDiagonal() throws Exception { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("Amap"); HSSFCellStyle style = workbook.createCellStyle(); style.setWrapText(true); Field f = HSSFCellStyle.class.getDeclaredField("_format"); f.setAccessible(true); ExtendedFormatRecord efr = (ExtendedFormatRecord) f.get(style); efr.setIndentNotParentBorder(true); efr.setDiag((short) 3); // 8 et 64 semble marcher de facon identique efr.setAdtlDiag((short) 64); efr.setAdtlDiagLineStyle((short) 1); addRow(sheet, style, (short) 0); addRow(sheet, style, (short) 1); addRow(sheet, style, (short) 2); FileOutputStream fos = new FileOutputStream("test2.xls"); workbook.write(fos); fos.flush(); fos.close(); System.out.println("OK !"); }