You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XSSFPivotTable.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel;
  16. import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import javax.xml.namespace.QName;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import org.apache.poi.ooxml.POIXMLDocumentPart;
  25. import org.apache.poi.openxml4j.opc.PackagePart;
  26. import org.apache.poi.ss.SpreadsheetVersion;
  27. import org.apache.poi.ss.usermodel.Cell;
  28. import org.apache.poi.ss.usermodel.CellType;
  29. import org.apache.poi.ss.usermodel.DataConsolidateFunction;
  30. import org.apache.poi.ss.usermodel.DataFormat;
  31. import org.apache.poi.ss.usermodel.Sheet;
  32. import org.apache.poi.ss.usermodel.Workbook;
  33. import org.apache.poi.ss.util.AreaReference;
  34. import org.apache.poi.ss.util.CellReference;
  35. import org.apache.poi.util.Beta;
  36. import org.apache.poi.util.Internal;
  37. import org.apache.xmlbeans.XmlException;
  38. import org.apache.xmlbeans.XmlOptions;
  39. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCacheSource;
  40. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColFields;
  41. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataField;
  42. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataFields;
  43. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTField;
  44. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTItems;
  45. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTLocation;
  46. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageField;
  47. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageFields;
  48. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotCacheDefinition;
  49. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotField;
  50. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotFields;
  51. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotTableDefinition;
  52. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotTableStyle;
  53. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRowFields;
  54. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheetSource;
  55. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STAxis;
  56. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataConsolidateFunction;
  57. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STItemType;
  58. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STSourceType;
  59. public class XSSFPivotTable extends POIXMLDocumentPart {
  60. protected static final short CREATED_VERSION = 3;
  61. protected static final short MIN_REFRESHABLE_VERSION = 3;
  62. protected static final short UPDATED_VERSION = 3;
  63. private CTPivotTableDefinition pivotTableDefinition;
  64. private XSSFPivotCacheDefinition pivotCacheDefinition;
  65. private XSSFPivotCache pivotCache;
  66. private XSSFPivotCacheRecords pivotCacheRecords;
  67. private Sheet parentSheet;
  68. private Sheet dataSheet;
  69. @Beta
  70. protected XSSFPivotTable() {
  71. super();
  72. pivotTableDefinition = CTPivotTableDefinition.Factory.newInstance();
  73. pivotCache = new XSSFPivotCache();
  74. pivotCacheDefinition = new XSSFPivotCacheDefinition();
  75. pivotCacheRecords = new XSSFPivotCacheRecords();
  76. }
  77. /**
  78. * Creates an XSSFPivotTable representing the given package part and relationship.
  79. * Should only be called when reading in an existing file.
  80. *
  81. * @param part - The package part that holds xml data representing this pivot table.
  82. *
  83. * @since POI 3.14-Beta1
  84. */
  85. @Beta
  86. protected XSSFPivotTable(PackagePart part) throws IOException {
  87. super(part);
  88. readFrom(part.getInputStream());
  89. }
  90. @Beta
  91. public void readFrom(InputStream is) throws IOException {
  92. try {
  93. XmlOptions options = new XmlOptions(DEFAULT_XML_OPTIONS);
  94. //Removing root element
  95. options.setLoadReplaceDocumentElement(null);
  96. pivotTableDefinition = CTPivotTableDefinition.Factory.parse(is, options);
  97. } catch (XmlException e) {
  98. throw new IOException(e.getLocalizedMessage());
  99. }
  100. }
  101. @Beta
  102. public void setPivotCache(XSSFPivotCache pivotCache) {
  103. this.pivotCache = pivotCache;
  104. }
  105. @Beta
  106. public XSSFPivotCache getPivotCache() {
  107. return pivotCache;
  108. }
  109. @Beta
  110. public Sheet getParentSheet() {
  111. return parentSheet;
  112. }
  113. @Beta
  114. public void setParentSheet(XSSFSheet parentSheet) {
  115. this.parentSheet = parentSheet;
  116. }
  117. @Beta
  118. @Internal
  119. public CTPivotTableDefinition getCTPivotTableDefinition() {
  120. return pivotTableDefinition;
  121. }
  122. @Beta
  123. @Internal
  124. public void setCTPivotTableDefinition(CTPivotTableDefinition pivotTableDefinition) {
  125. this.pivotTableDefinition = pivotTableDefinition;
  126. }
  127. @Beta
  128. public XSSFPivotCacheDefinition getPivotCacheDefinition() {
  129. return pivotCacheDefinition;
  130. }
  131. @Beta
  132. public void setPivotCacheDefinition(XSSFPivotCacheDefinition pivotCacheDefinition) {
  133. this.pivotCacheDefinition = pivotCacheDefinition;
  134. }
  135. @Beta
  136. public XSSFPivotCacheRecords getPivotCacheRecords() {
  137. return pivotCacheRecords;
  138. }
  139. @Beta
  140. public void setPivotCacheRecords(XSSFPivotCacheRecords pivotCacheRecords) {
  141. this.pivotCacheRecords = pivotCacheRecords;
  142. }
  143. @Beta
  144. public Sheet getDataSheet() {
  145. return dataSheet;
  146. }
  147. @Beta
  148. private void setDataSheet(Sheet dataSheet) {
  149. this.dataSheet = dataSheet;
  150. }
  151. @Beta
  152. @Override
  153. protected void commit() throws IOException {
  154. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  155. //Sets the pivotTableDefinition tag
  156. xmlOptions.setSaveSyntheticDocumentElement(new QName(CTPivotTableDefinition.type.getName().
  157. getNamespaceURI(), "pivotTableDefinition"));
  158. PackagePart part = getPackagePart();
  159. try (OutputStream out = part.getOutputStream()) {
  160. pivotTableDefinition.save(out, xmlOptions);
  161. }
  162. }
  163. /**
  164. * Set default values for the table definition.
  165. */
  166. @Beta
  167. protected void setDefaultPivotTableDefinition() {
  168. //Not more than one until more created
  169. pivotTableDefinition.setMultipleFieldFilters(false);
  170. //Indentation increment for compact rows
  171. pivotTableDefinition.setIndent(0);
  172. //The pivot version which created the pivot cache set to default value
  173. pivotTableDefinition.setCreatedVersion(CREATED_VERSION);
  174. //Minimun version required to update the pivot cache
  175. pivotTableDefinition.setMinRefreshableVersion(MIN_REFRESHABLE_VERSION);
  176. //Version of the application which "updated the spreadsheet last"
  177. pivotTableDefinition.setUpdatedVersion(UPDATED_VERSION);
  178. //Titles shown at the top of each page when printed
  179. pivotTableDefinition.setItemPrintTitles(true);
  180. //Set autoformat properties
  181. pivotTableDefinition.setUseAutoFormatting(true);
  182. pivotTableDefinition.setApplyNumberFormats(false);
  183. pivotTableDefinition.setApplyWidthHeightFormats(true);
  184. pivotTableDefinition.setApplyAlignmentFormats(false);
  185. pivotTableDefinition.setApplyPatternFormats(false);
  186. pivotTableDefinition.setApplyFontFormats(false);
  187. pivotTableDefinition.setApplyBorderFormats(false);
  188. pivotTableDefinition.setCacheId(pivotCache.getCTPivotCache().getCacheId());
  189. pivotTableDefinition.setName("PivotTable"+pivotTableDefinition.getCacheId());
  190. pivotTableDefinition.setDataCaption("Values");
  191. //Set the default style for the pivot table
  192. CTPivotTableStyle style = pivotTableDefinition.addNewPivotTableStyleInfo();
  193. style.setName("PivotStyleLight16");
  194. style.setShowLastColumn(true);
  195. style.setShowColStripes(false);
  196. style.setShowRowStripes(false);
  197. style.setShowColHeaders(true);
  198. style.setShowRowHeaders(true);
  199. }
  200. protected AreaReference getPivotArea() {
  201. final Workbook wb = getDataSheet().getWorkbook();
  202. return getPivotCacheDefinition().getPivotArea(wb);
  203. }
  204. /**
  205. * Verify column index (relative to first column in pivot area) is within the
  206. * pivot area
  207. *
  208. * @param columnIndex
  209. * @throws IndexOutOfBoundsException
  210. */
  211. private void checkColumnIndex(int columnIndex) throws IndexOutOfBoundsException {
  212. AreaReference pivotArea = getPivotArea();
  213. int size = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol() + 1;
  214. if (columnIndex < 0 || columnIndex >= size) {
  215. throw new IndexOutOfBoundsException("Column Index: " + columnIndex + ", Size: " + size);
  216. }
  217. }
  218. /**
  219. * Add a row label using data from the given column.
  220. * @param columnIndex the index of the source column to be used as row label.
  221. * {@code columnIndex} is 0-based indexed and relative to the first column in the source.
  222. */
  223. @Beta
  224. public void addRowLabel(int columnIndex) {
  225. checkColumnIndex(columnIndex);
  226. AreaReference pivotArea = getPivotArea();
  227. final int lastRowIndex = pivotArea.getLastCell().getRow() - pivotArea.getFirstCell().getRow();
  228. CTPivotFields pivotFields = pivotTableDefinition.getPivotFields();
  229. CTPivotField pivotField = CTPivotField.Factory.newInstance();
  230. CTItems items = pivotField.addNewItems();
  231. pivotField.setAxis(STAxis.AXIS_ROW);
  232. pivotField.setShowAll(false);
  233. for (int i = 0; i <= lastRowIndex; i++) {
  234. items.addNewItem().setT(STItemType.DEFAULT);
  235. }
  236. items.setCount(items.sizeOfItemArray());
  237. pivotFields.setPivotFieldArray(columnIndex, pivotField);
  238. CTRowFields rowFields;
  239. if(pivotTableDefinition.getRowFields() != null) {
  240. rowFields = pivotTableDefinition.getRowFields();
  241. } else {
  242. rowFields = pivotTableDefinition.addNewRowFields();
  243. }
  244. rowFields.addNewField().setX(columnIndex);
  245. rowFields.setCount(rowFields.sizeOfFieldArray());
  246. }
  247. @Beta
  248. public List<Integer> getRowLabelColumns() {
  249. if (pivotTableDefinition.getRowFields() != null) {
  250. List<Integer> columnIndexes = new ArrayList<>();
  251. for (CTField f : pivotTableDefinition.getRowFields().getFieldArray()) {
  252. columnIndexes.add(f.getX());
  253. }
  254. return columnIndexes;
  255. } else {
  256. return Collections.emptyList();
  257. }
  258. }
  259. /**
  260. * Add a col label using data from the given column.
  261. * @param columnIndex the index of the source column to be used as row label.
  262. * {@code columnIndex} is 0-based indexed and relative to the first column in the source.
  263. * @param valueFormat format of column value (e.g. for date: "DD.MM.YYYY")
  264. */
  265. @Beta
  266. public void addColLabel(int columnIndex, String valueFormat) {
  267. checkColumnIndex(columnIndex);
  268. AreaReference pivotArea = getPivotArea();
  269. final int lastRowIndex = pivotArea.getLastCell().getRow() - pivotArea.getFirstCell().getRow();
  270. CTPivotFields pivotFields = pivotTableDefinition.getPivotFields();
  271. CTPivotField pivotField = CTPivotField.Factory.newInstance();
  272. CTItems items = pivotField.addNewItems();
  273. pivotField.setAxis(STAxis.AXIS_COL);
  274. pivotField.setShowAll(false);
  275. if (valueFormat != null && !valueFormat.trim().isEmpty()) {
  276. DataFormat df = parentSheet.getWorkbook().createDataFormat();
  277. pivotField.setNumFmtId(df.getFormat(valueFormat));
  278. }
  279. for (int i = 0; i <= lastRowIndex; i++) {
  280. items.addNewItem().setT(STItemType.DEFAULT);
  281. }
  282. items.setCount(items.sizeOfItemArray());
  283. pivotFields.setPivotFieldArray(columnIndex, pivotField);
  284. CTColFields colFields;
  285. if(pivotTableDefinition.getColFields() != null) {
  286. colFields = pivotTableDefinition.getColFields();
  287. } else {
  288. colFields = pivotTableDefinition.addNewColFields();
  289. }
  290. colFields.addNewField().setX(columnIndex);
  291. colFields.setCount(colFields.sizeOfFieldArray());
  292. }
  293. /**
  294. * Add a col label using data from the given column.
  295. * @param columnIndex the index of the source column to be used as row label.
  296. * {@code columnIndex} is 0-based indexed and relative to the first column in the source.
  297. */
  298. @Beta
  299. public void addColLabel(int columnIndex) {
  300. addColLabel(columnIndex, null);
  301. }
  302. @Beta
  303. public List<Integer> getColLabelColumns() {
  304. if (pivotTableDefinition.getColFields() != null) {
  305. List<Integer> columnIndexes = new ArrayList<>();
  306. for (CTField f : pivotTableDefinition.getColFields().getFieldArray()) {
  307. columnIndexes.add(f.getX());
  308. }
  309. return columnIndexes;
  310. } else {
  311. return Collections.emptyList();
  312. }
  313. }
  314. /**
  315. * Add a column label using data from the given column and specified function
  316. * @param columnIndex the index of the source column to be used as column label.
  317. * {@code columnIndex} is 0-based indexed and relative to the first column in the source.
  318. * @param function the function to be used on the data
  319. * The following functions exists:
  320. * Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp
  321. * @param valueFieldName the name of pivot table value field
  322. * @param valueFormat format of value field (e.g. "#,##0.00")
  323. */
  324. @Beta
  325. public void addColumnLabel(DataConsolidateFunction function, int columnIndex, String valueFieldName, String valueFormat) {
  326. checkColumnIndex(columnIndex);
  327. addDataColumn(columnIndex, true);
  328. addDataField(function, columnIndex, valueFieldName, valueFormat);
  329. // colfield should be added for the second one.
  330. if (pivotTableDefinition.getDataFields().getCount() == 2) {
  331. CTColFields colFields;
  332. if(pivotTableDefinition.getColFields() != null) {
  333. colFields = pivotTableDefinition.getColFields();
  334. } else {
  335. colFields = pivotTableDefinition.addNewColFields();
  336. }
  337. colFields.addNewField().setX(-2);
  338. colFields.setCount(colFields.sizeOfFieldArray());
  339. }
  340. }
  341. /**
  342. * Add a column label using data from the given column and specified function
  343. * @param columnIndex the index of the source column to be used as column label.
  344. * {@code columnIndex} is 0-based indexed and relative to the first column in the source.
  345. * @param function the function to be used on the data
  346. * The following functions exists:
  347. * Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp
  348. * @param valueFieldName the name of pivot table value field
  349. */
  350. @Beta
  351. public void addColumnLabel(DataConsolidateFunction function, int columnIndex, String valueFieldName) {
  352. addColumnLabel(function, columnIndex, valueFieldName, null);
  353. }
  354. /**
  355. * Add a column label using data from the given column and specified function
  356. * @param columnIndex the index of the source column to be used as column label
  357. * {@code columnIndex} is 0-based indexed and relative to the first column in the source..
  358. * @param function the function to be used on the data
  359. * The following functions exists:
  360. * Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp
  361. */
  362. @Beta
  363. public void addColumnLabel(DataConsolidateFunction function, int columnIndex) {
  364. addColumnLabel(function, columnIndex, function.getName(), null);
  365. }
  366. /**
  367. * Add data field with data from the given column and specified function.
  368. * @param function the function to be used on the data
  369. * The following functions exists:
  370. * Sum, Count, Average, Max, Min, Product, Count numbers, StdDev, StdDevp, Var, Varp
  371. * @param columnIndex the index of the column to be used as column label.
  372. * @param valueFieldName the name of pivot table value field
  373. */
  374. @Beta
  375. private void addDataField(DataConsolidateFunction function, int columnIndex, String valueFieldName, String valueFormat) {
  376. checkColumnIndex(columnIndex);
  377. CTDataFields dataFields;
  378. if(pivotTableDefinition.getDataFields() != null) {
  379. dataFields = pivotTableDefinition.getDataFields();
  380. } else {
  381. dataFields = pivotTableDefinition.addNewDataFields();
  382. }
  383. CTDataField dataField = dataFields.addNewDataField();
  384. dataField.setSubtotal(STDataConsolidateFunction.Enum.forInt(function.getValue()));
  385. dataField.setName(valueFieldName);
  386. dataField.setFld(columnIndex);
  387. if (valueFormat != null && !valueFormat.trim().isEmpty()) {
  388. DataFormat df = parentSheet.getWorkbook().createDataFormat();
  389. dataField.setNumFmtId(df.getFormat(valueFormat));
  390. }
  391. dataFields.setCount(dataFields.sizeOfDataFieldArray());
  392. }
  393. /**
  394. * Add column containing data from the referenced area.
  395. * @param columnIndex the index of the column containing the data
  396. * @param isDataField true if the data should be displayed in the pivot table.
  397. */
  398. @Beta
  399. public void addDataColumn(int columnIndex, boolean isDataField) {
  400. checkColumnIndex(columnIndex);
  401. CTPivotFields pivotFields = pivotTableDefinition.getPivotFields();
  402. CTPivotField pivotField = CTPivotField.Factory.newInstance();
  403. pivotField.setDataField(isDataField);
  404. pivotField.setShowAll(false);
  405. pivotFields.setPivotFieldArray(columnIndex, pivotField);
  406. }
  407. /**
  408. * Add filter for the column with the corresponding index and cell value
  409. * @param columnIndex index of column to filter on
  410. */
  411. @Beta
  412. public void addReportFilter(int columnIndex) {
  413. checkColumnIndex(columnIndex);
  414. AreaReference pivotArea = getPivotArea();
  415. int lastRowIndex = pivotArea.getLastCell().getRow() - pivotArea.getFirstCell().getRow();
  416. // check and change row of location
  417. CTLocation location = pivotTableDefinition.getLocation();
  418. AreaReference destination = new AreaReference(location.getRef(), SpreadsheetVersion.EXCEL2007);
  419. if (destination.getFirstCell().getRow() < 2) {
  420. AreaReference newDestination = new AreaReference(new CellReference(2, destination.getFirstCell().getCol()), new CellReference(
  421. 3, destination.getFirstCell().getCol()+1), SpreadsheetVersion.EXCEL2007);
  422. location.setRef(newDestination.formatAsString());
  423. }
  424. CTPivotFields pivotFields = pivotTableDefinition.getPivotFields();
  425. CTPivotField pivotField = CTPivotField.Factory.newInstance();
  426. CTItems items = pivotField.addNewItems();
  427. pivotField.setAxis(STAxis.AXIS_PAGE);
  428. pivotField.setShowAll(false);
  429. for(int i = 0; i <= lastRowIndex; i++) {
  430. items.addNewItem().setT(STItemType.DEFAULT);
  431. }
  432. items.setCount(items.sizeOfItemArray());
  433. pivotFields.setPivotFieldArray(columnIndex, pivotField);
  434. CTPageFields pageFields;
  435. if (pivotTableDefinition.getPageFields()!= null) {
  436. pageFields = pivotTableDefinition.getPageFields();
  437. //Another filter has already been created
  438. pivotTableDefinition.setMultipleFieldFilters(true);
  439. } else {
  440. pageFields = pivotTableDefinition.addNewPageFields();
  441. }
  442. CTPageField pageField = pageFields.addNewPageField();
  443. pageField.setHier(-1);
  444. pageField.setFld(columnIndex);
  445. pageFields.setCount(pageFields.sizeOfPageFieldArray());
  446. pivotTableDefinition.getLocation().setColPageCount(pageFields.getCount());
  447. }
  448. /**
  449. * Creates cacheSource and workSheetSource for pivot table and sets the source reference as well assets the location of the pivot table
  450. * @param position Position for pivot table in sheet
  451. * @param sourceSheet Sheet where the source will be collected from
  452. * @param refConfig an configurator that knows how to configure pivot table references
  453. */
  454. @Beta
  455. protected void createSourceReferences(CellReference position, Sheet sourceSheet, PivotTableReferenceConfigurator refConfig){
  456. //Get cell one to the right and one down from position, add both to AreaReference and set pivot table location.
  457. AreaReference destination = new AreaReference(position, new CellReference(
  458. position.getRow()+1, position.getCol()+1), SpreadsheetVersion.EXCEL2007);
  459. CTLocation location;
  460. if(pivotTableDefinition.getLocation() == null) {
  461. location = pivotTableDefinition.addNewLocation();
  462. location.setFirstDataCol(1);
  463. location.setFirstDataRow(1);
  464. location.setFirstHeaderRow(1);
  465. } else {
  466. location = pivotTableDefinition.getLocation();
  467. }
  468. location.setRef(destination.formatAsString());
  469. pivotTableDefinition.setLocation(location);
  470. //Set source for the pivot table
  471. CTPivotCacheDefinition cacheDef = getPivotCacheDefinition().getCTPivotCacheDefinition();
  472. CTCacheSource cacheSource = cacheDef.addNewCacheSource();
  473. cacheSource.setType(STSourceType.WORKSHEET);
  474. CTWorksheetSource worksheetSource = cacheSource.addNewWorksheetSource();
  475. worksheetSource.setSheet(sourceSheet.getSheetName());
  476. setDataSheet(sourceSheet);
  477. refConfig.configureReference(worksheetSource);
  478. if (worksheetSource.getName() == null && worksheetSource.getRef() == null) throw new IllegalArgumentException("Pivot table source area reference or name must be specified.");
  479. }
  480. @Beta
  481. protected void createDefaultDataColumns() {
  482. CTPivotFields pivotFields;
  483. if (pivotTableDefinition.getPivotFields() != null) {
  484. pivotFields = pivotTableDefinition.getPivotFields();
  485. } else {
  486. pivotFields = pivotTableDefinition.addNewPivotFields();
  487. }
  488. AreaReference sourceArea = getPivotArea();
  489. int firstColumn = sourceArea.getFirstCell().getCol();
  490. int lastColumn = sourceArea.getLastCell().getCol();
  491. CTPivotField pivotField;
  492. for(int i = firstColumn; i<=lastColumn; i++) {
  493. pivotField = pivotFields.addNewPivotField();
  494. pivotField.setDataField(false);
  495. pivotField.setShowAll(false);
  496. }
  497. pivotFields.setCount(pivotFields.sizeOfPivotFieldArray());
  498. }
  499. protected static interface PivotTableReferenceConfigurator {
  500. /**
  501. * Configure the name or area reference for the pivot table
  502. * @param wsSource CTWorksheetSource that needs the pivot source reference assignment
  503. */
  504. public void configureReference(CTWorksheetSource wsSource);
  505. }
  506. }