Search in sources :

Example 11 with Beta

use of org.apache.poi.util.Beta in project poi by apache.

the class XSSFWorkbook method addPivotCache.

/**
     * Add pivotCache to the workbook
     */
@Beta
protected CTPivotCache addPivotCache(String rId) {
    CTWorkbook ctWorkbook = getCTWorkbook();
    CTPivotCaches caches;
    if (ctWorkbook.isSetPivotCaches()) {
        caches = ctWorkbook.getPivotCaches();
    } else {
        caches = ctWorkbook.addNewPivotCaches();
    }
    CTPivotCache cache = caches.addNewPivotCache();
    int tableId = getPivotTables().size() + 1;
    cache.setCacheId(tableId);
    cache.setId(rId);
    if (pivotCaches == null) {
        pivotCaches = new ArrayList<CTPivotCache>();
    }
    pivotCaches.add(cache);
    return cache;
}
Also used : CTPivotCaches(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotCaches) CTWorkbook(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook) CTPivotCache(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotCache) Beta(org.apache.poi.util.Beta)

Example 12 with Beta

use of org.apache.poi.util.Beta in project poi by apache.

the class XSSFSheet method createPivotTable.

/**
     * Creates an empty XSSFPivotTable and sets up all its relationships
     * including: pivotCacheDefinition, pivotCacheRecords
     * @return returns a pivotTable
     */
@SuppressWarnings("resource")
@Beta
private XSSFPivotTable createPivotTable() {
    XSSFWorkbook wb = getWorkbook();
    List<XSSFPivotTable> pivotTables = wb.getPivotTables();
    int tableId = getWorkbook().getPivotTables().size() + 1;
    //Create relationship between pivotTable and the worksheet
    XSSFPivotTable pivotTable = (XSSFPivotTable) createRelationship(XSSFRelation.PIVOT_TABLE, XSSFFactory.getInstance(), tableId);
    pivotTable.setParentSheet(this);
    pivotTables.add(pivotTable);
    XSSFWorkbook workbook = getWorkbook();
    //Create relationship between the pivot cache defintion and the workbook
    XSSFPivotCacheDefinition pivotCacheDefinition = (XSSFPivotCacheDefinition) workbook.createRelationship(XSSFRelation.PIVOT_CACHE_DEFINITION, XSSFFactory.getInstance(), tableId);
    String rId = workbook.getRelationId(pivotCacheDefinition);
    //Create relationship between pivotTable and pivotCacheDefinition without creating a new instance
    PackagePart pivotPackagePart = pivotTable.getPackagePart();
    pivotPackagePart.addRelationship(pivotCacheDefinition.getPackagePart().getPartName(), TargetMode.INTERNAL, XSSFRelation.PIVOT_CACHE_DEFINITION.getRelation());
    pivotTable.setPivotCacheDefinition(pivotCacheDefinition);
    //Create pivotCache and sets up it's relationship with the workbook
    pivotTable.setPivotCache(new XSSFPivotCache(workbook.addPivotCache(rId)));
    //Create relationship between pivotcacherecord and pivotcachedefinition
    XSSFPivotCacheRecords pivotCacheRecords = (XSSFPivotCacheRecords) pivotCacheDefinition.createRelationship(XSSFRelation.PIVOT_CACHE_RECORDS, XSSFFactory.getInstance(), tableId);
    //Set relationships id for pivotCacheDefinition to pivotCacheRecords
    pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().setId(pivotCacheDefinition.getRelationId(pivotCacheRecords));
    wb.setPivotTables(pivotTables);
    return pivotTable;
}
Also used : PackagePart(org.apache.poi.openxml4j.opc.PackagePart) Beta(org.apache.poi.util.Beta)

Example 13 with Beta

use of org.apache.poi.util.Beta in project poi by apache.

the class XSSFPivotTable method addColumnLabel.

/**
     * Add a column label using data from the given column and specified function
     * @param columnIndex the index of the source column to be used as column label.
     * {@code columnIndex} is 0-based indexed and relative to the first column in the source.
     * @param function the function to be used on the data
     * The following functions exists:
     * Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp
     * @param valueFieldName the name of pivot table value field
     */
@Beta
public void addColumnLabel(DataConsolidateFunction function, int columnIndex, String valueFieldName) {
    checkColumnIndex(columnIndex);
    addDataColumn(columnIndex, true);
    addDataField(function, columnIndex, valueFieldName);
    // colfield should be added for the second one.
    if (pivotTableDefinition.getDataFields().getCount() == 2) {
        CTColFields colFields;
        if (pivotTableDefinition.getColFields() != null) {
            colFields = pivotTableDefinition.getColFields();
        } else {
            colFields = pivotTableDefinition.addNewColFields();
        }
        colFields.addNewField().setX(-2);
        colFields.setCount(colFields.sizeOfFieldArray());
    }
}
Also used : CTColFields(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColFields) Beta(org.apache.poi.util.Beta)

Example 14 with Beta

use of org.apache.poi.util.Beta in project poi by apache.

the class XSSFSheet method copyRows.

/**
     * copyRows rows from srcRows to this sheet starting at destStartRow
     *
     * Additionally copies merged regions that are completely defined in these
     * rows (ie. merged 2 cells on a row to be shifted).
     * @param srcRows the rows to copy. Formulas will be offset by the difference
     * in the row number of the first row in srcRows and destStartRow (even if srcRows
     * are from a different sheet).
     * @param destStartRow the row in this sheet to paste the first row of srcRows
     * the remainder of srcRows will be pasted below destStartRow per the cell copy policy
     * @param policy is the cell copy policy, which can be used to merge the source and destination
     * when the source is blank, copy styles only, paste as value, etc
     */
@Beta
public void copyRows(List<? extends Row> srcRows, int destStartRow, CellCopyPolicy policy) {
    if (srcRows == null || srcRows.size() == 0) {
        throw new IllegalArgumentException("No rows to copy");
    }
    final Row srcStartRow = srcRows.get(0);
    final Row srcEndRow = srcRows.get(srcRows.size() - 1);
    if (srcStartRow == null) {
        throw new IllegalArgumentException("copyRows: First row cannot be null");
    }
    final int srcStartRowNum = srcStartRow.getRowNum();
    final int srcEndRowNum = srcEndRow.getRowNum();
    // check row numbers to make sure they are continuous and increasing (monotonic)
    // and srcRows does not contain null rows
    final int size = srcRows.size();
    for (int index = 1; index < size; index++) {
        final Row curRow = srcRows.get(index);
        if (curRow == null) {
            throw new IllegalArgumentException("srcRows may not contain null rows. Found null row at index " + index + ".");
        //} else if (curRow.getRowNum() != prevRow.getRowNum() + 1) {
        //    throw new IllegalArgumentException("srcRows must contain continuously increasing row numbers. " +
        //            "Got srcRows[" + (index-1) + "]=Row " + prevRow.getRowNum() + ", srcRows[" + index + "]=Row " + curRow.getRowNum() + ".");
        // FIXME: assumes row objects belong to non-null sheets and sheets belong to non-null workbooks.
        } else if (srcStartRow.getSheet().getWorkbook() != curRow.getSheet().getWorkbook()) {
            throw new IllegalArgumentException("All rows in srcRows must belong to the same sheet in the same workbook." + "Expected all rows from same workbook (" + srcStartRow.getSheet().getWorkbook() + "). " + "Got srcRows[" + index + "] from different workbook (" + curRow.getSheet().getWorkbook() + ").");
        } else if (srcStartRow.getSheet() != curRow.getSheet()) {
            throw new IllegalArgumentException("All rows in srcRows must belong to the same sheet. " + "Expected all rows from " + srcStartRow.getSheet().getSheetName() + ". " + "Got srcRows[" + index + "] from " + curRow.getSheet().getSheetName());
        }
    }
    // FIXME: is special behavior needed if srcRows and destRows belong to the same sheets and the regions overlap?
    final CellCopyPolicy options = new CellCopyPolicy(policy);
    // avoid O(N^2) performance scanning through all regions for each row
    // merged regions will be copied after all the rows have been copied
    options.setCopyMergedRegions(false);
    // FIXME: if srcRows contains gaps or null values, clear out those rows that will be overwritten
    // how will this work with merging (copy just values, leave cell styles in place?)
    int r = destStartRow;
    for (Row srcRow : srcRows) {
        int destRowNum;
        if (policy.isCondenseRows()) {
            destRowNum = r++;
        } else {
            final int shift = (srcRow.getRowNum() - srcStartRowNum);
            destRowNum = destStartRow + shift;
        }
        //removeRow(destRowNum); //this probably clears all external formula references to destRow, causing unwanted #REF! errors
        final XSSFRow destRow = createRow(destRowNum);
        destRow.copyRowFrom(srcRow, options);
    }
    // Copy merged regions that are contained within the copy region
    if (policy.isCopyMergedRegions()) {
        // FIXME: is this something that rowShifter could be doing?
        final int shift = destStartRow - srcStartRowNum;
        for (CellRangeAddress srcRegion : srcStartRow.getSheet().getMergedRegions()) {
            if (srcStartRowNum <= srcRegion.getFirstRow() && srcRegion.getLastRow() <= srcEndRowNum) {
                // srcRegion is fully inside the copied rows
                final CellRangeAddress destRegion = srcRegion.copy();
                destRegion.setFirstRow(destRegion.getFirstRow() + shift);
                destRegion.setLastRow(destRegion.getLastRow() + shift);
                addMergedRegion(destRegion);
            }
        }
    }
}
Also used : Row(org.apache.poi.ss.usermodel.Row) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) CellCopyPolicy(org.apache.poi.ss.usermodel.CellCopyPolicy) Beta(org.apache.poi.util.Beta)

Example 15 with Beta

use of org.apache.poi.util.Beta in project poi by apache.

the class XSSFPivotCacheDefinition method createCacheFields.

/**
     * Generates a cache field for each column in the reference area for the pivot table.
     * @param sheet The sheet where the data i collected from
     */
@Beta
protected void createCacheFields(Sheet sheet) {
    //Get values for start row, start and end column
    AreaReference ar = getPivotArea(sheet.getWorkbook());
    CellReference firstCell = ar.getFirstCell();
    CellReference lastCell = ar.getLastCell();
    int columnStart = firstCell.getCol();
    int columnEnd = lastCell.getCol();
    Row row = sheet.getRow(firstCell.getRow());
    CTCacheFields cFields;
    if (ctPivotCacheDefinition.getCacheFields() != null) {
        cFields = ctPivotCacheDefinition.getCacheFields();
    } else {
        cFields = ctPivotCacheDefinition.addNewCacheFields();
    }
    //For each column, create a cache field and give it en empty sharedItems
    for (int i = columnStart; i <= columnEnd; i++) {
        CTCacheField cf = cFields.addNewCacheField();
        if (i == columnEnd) {
            cFields.setCount(cFields.sizeOfCacheFieldArray());
        }
        //General number format
        cf.setNumFmtId(0);
        Cell cell = row.getCell(i);
        cell.setCellType(CellType.STRING);
        cf.setName(row.getCell(i).getStringCellValue());
        cf.addNewSharedItems();
    }
}
Also used : AreaReference(org.apache.poi.ss.util.AreaReference) CTCacheField(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCacheField) Row(org.apache.poi.ss.usermodel.Row) CellReference(org.apache.poi.ss.util.CellReference) Cell(org.apache.poi.ss.usermodel.Cell) CTCacheFields(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCacheFields) Beta(org.apache.poi.util.Beta)

Aggregations

Beta (org.apache.poi.util.Beta)21 AreaReference (org.apache.poi.ss.util.AreaReference)7 XmlOptions (org.apache.xmlbeans.XmlOptions)7 IOException (java.io.IOException)4 QName (javax.xml.namespace.QName)4 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)4 XmlException (org.apache.xmlbeans.XmlException)4 CTPivotField (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotField)4 CTPivotFields (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotFields)4 OutputStream (java.io.OutputStream)3 Cell (org.apache.poi.ss.usermodel.Cell)3 Row (org.apache.poi.ss.usermodel.Row)2 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)2 CellReference (org.apache.poi.ss.util.CellReference)2 CTItems (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTItems)2 CTWorksheetSource (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheetSource)2 HashSet (java.util.HashSet)1 FormulaShifter (org.apache.poi.ss.formula.FormulaShifter)1 CellCopyPolicy (org.apache.poi.ss.usermodel.CellCopyPolicy)1 Name (org.apache.poi.ss.usermodel.Name)1