Search in sources :

Example 1 with Styles

use of org.docx4j.openpackaging.parts.SpreadsheetML.Styles in project jmix by jmix-framework.

the class XlsxGenerator method generatePackage.

@Override
protected OpcPackage generatePackage(ReportData reportData) throws Docx4JException, JAXBException {
    SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
    // String sheetInternalName = ((Messages) AppBeans.get(Messages.NAME)).getMessage(getClass(), SHEET);
    WorksheetPart sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/" + SHEET + ".xml"), SHEET, 1);
    SheetData sheetData = sheet.getJaxbElement().getSheetData();
    ObjectFactory factory = org.xlsx4j.jaxb.Context.getsmlObjectFactory();
    CTCalcPr ctCalcPr = factory.createCTCalcPr();
    ctCalcPr.setCalcMode(STCalcMode.AUTO);
    pkg.getWorkbookPart().getJaxbElement().setCalcPr(ctCalcPr);
    Styles styles = new Styles(new PartName("/xl/styles.xml"));
    styles.setJaxbElement(generateStyleSheet(factory));
    DefinedNames definedNames = factory.createDefinedNames();
    // first row of sheet is '1'
    long rowNum = 1;
    long startedRowForRegion;
    long endedRowForRegion;
    int maxColCount = 0;
    for (ReportRegion reportRegion : reportData.getReportRegions()) {
        if (reportRegion.isTabulatedRegion()) {
            // insert empty row before table
            rowNum++;
            // first column of sheet is '1'
            int colNum = 1;
            for (RegionProperty regionProperty : reportRegion.getRegionProperties()) {
                sheetData.getRow().add(createRow(factory, regionProperty.getHierarchicalLocalizedNameExceptRoot(), colNum++, rowNum));
            }
            rowNum++;
            startedRowForRegion = rowNum;
            colNum = 1;
            for (RegionProperty regionProperty : reportRegion.getRegionProperties()) {
                sheetData.getRow().add(createRow(factory, reportTemplatePlaceholder.getPlaceholderValue(regionProperty.getHierarchicalNameExceptRoot(), reportRegion), colNum++, rowNum));
            }
            endedRowForRegion = rowNum;
            maxColCount = maxColCount > colNum ? maxColCount : colNum;
            rowNum++;
            // insert empty row after table
            rowNum++;
        } else {
            startedRowForRegion = rowNum;
            for (RegionProperty regionProperty : reportRegion.getRegionProperties()) {
                Row row = factory.createRow();
                row.setR(rowNum);
                row.getC().add(createCell(factory, regionProperty.getHierarchicalLocalizedNameExceptRoot() + ":", 1, rowNum));
                row.getC().add(createCell(factory, reportTemplatePlaceholder.getPlaceholderValue(regionProperty.getHierarchicalNameExceptRoot(), reportRegion), 2, rowNum));
                sheetData.getRow().add(row);
                rowNum++;
            }
            endedRowForRegion = rowNum - 1;
            maxColCount = maxColCount > 2 ? maxColCount : 2;
        }
        addDefinedNames(SHEET, factory, definedNames, startedRowForRegion, endedRowForRegion, reportRegion);
    }
    List<Cols> lstCols = sheet.getJaxbElement().getCols();
    Cols cols = factory.createCols();
    for (int i = 0; i < maxColCount; i++) {
        Col col = factory.createCol();
        col.setMin(i + 1);
        col.setMax(i + 1);
        col.setBestFit(Boolean.TRUE);
        col.setCustomWidth(true);
        col.setWidth(30.);
        cols.getCol().add(col);
    }
    lstCols.add(cols);
    pkg.getWorkbookPart().getJaxbElement().setDefinedNames(definedNames);
    sheet.addTargetPart(styles);
    Parts parts = pkg.getParts();
    Part workBook = parts.get(new PartName("/xl/workbook.xml"));
    workBook.addTargetPart(styles);
    return pkg;
}
Also used : SpreadsheetMLPackage(org.docx4j.openpackaging.packages.SpreadsheetMLPackage) RegionProperty(io.jmix.reports.entity.wizard.RegionProperty) WorksheetPart(org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart) PartName(org.docx4j.openpackaging.parts.PartName) Parts(org.docx4j.openpackaging.parts.Parts) Part(org.docx4j.openpackaging.parts.Part) WorksheetPart(org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart) ReportRegion(io.jmix.reports.entity.wizard.ReportRegion) Styles(org.docx4j.openpackaging.parts.SpreadsheetML.Styles)

Example 2 with Styles

use of org.docx4j.openpackaging.parts.SpreadsheetML.Styles in project docx4j by plutext.

the class CellUtils method getNumberFormatIndex.

// /**
// * Get the index of the number format (numFmt) record used by this cell format.
// *
// * @return the index of the number format
// */
// public long getDataFormatIndexZZZ(CTCellStyle cellStyle) {
// CTXf xf = this.stylesPart.getXfByIndex(cellStyle.getXfId());
// if (xf == null) {
// throw new RuntimeException("xf unexpectedly null");
// }
// 
// System.out.println("Using NumFmtId " + xf.getNumFmtId());
// 
// return xf.getNumFmtId();
// //(short)_cellXf.getNumFmtId();
// }
/**
 * Get the index of the number format (numFmt) record used by this cell format.
 *
 * @return the index of the number format
 */
public static long getNumberFormatIndex(Cell _cell) {
    Styles stylesPart = WorksheetPart.getWorksheetPart(_cell).getWorkbookPart().getStylesPart();
    CTXf xf = stylesPart.getXfByIndex(_cell.getS());
    if (xf == null) {
        throw new RuntimeException("xf unexpectedly null");
    }
    log.debug("Using NumFmtId " + xf.getNumFmtId());
    return xf.getNumFmtId();
// (short)_cellXf.getNumFmtId();
}
Also used : CTXf(org.xlsx4j.sml.CTXf) Styles(org.docx4j.openpackaging.parts.SpreadsheetML.Styles)

Example 3 with Styles

use of org.docx4j.openpackaging.parts.SpreadsheetML.Styles in project docx4j by plutext.

the class CellUtils method getCellStyle.

/**
 * Return the cell's style.
 *
 * @return the cell's style.</code>
 */
public static CTCellStyle getCellStyle(Cell _cell) {
    Styles stylesPart = WorksheetPart.getWorksheetPart(_cell).getWorkbookPart().getStylesPart();
    CTXf xf = stylesPart.getXfByIndex(_cell.getS());
    if (xf == null) {
        throw new RuntimeException("xf unexpectedly null");
    }
    return stylesPart.getStyleByIndex(xf.getXfId());
}
Also used : CTXf(org.xlsx4j.sml.CTXf) Styles(org.docx4j.openpackaging.parts.SpreadsheetML.Styles)

Aggregations

Styles (org.docx4j.openpackaging.parts.SpreadsheetML.Styles)3 CTXf (org.xlsx4j.sml.CTXf)2 RegionProperty (io.jmix.reports.entity.wizard.RegionProperty)1 ReportRegion (io.jmix.reports.entity.wizard.ReportRegion)1 SpreadsheetMLPackage (org.docx4j.openpackaging.packages.SpreadsheetMLPackage)1 Part (org.docx4j.openpackaging.parts.Part)1 PartName (org.docx4j.openpackaging.parts.PartName)1 Parts (org.docx4j.openpackaging.parts.Parts)1 WorksheetPart (org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart)1