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;
}
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;
}
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());
}
}
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);
}
}
}
}
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();
}
}
Aggregations