Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

XSLFTable.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xslf.usermodel;
  20. import static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  21. import java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import javax.xml.namespace.QName;
  26. import org.apache.poi.POIXMLException;
  27. import org.apache.poi.sl.usermodel.TableShape;
  28. import org.apache.poi.util.Internal;
  29. import org.apache.poi.util.Units;
  30. import org.apache.xmlbeans.XmlCursor;
  31. import org.apache.xmlbeans.XmlException;
  32. import org.apache.xmlbeans.XmlObject;
  33. import org.apache.xmlbeans.impl.values.XmlAnyTypeImpl;
  34. import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData;
  35. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  36. import org.openxmlformats.schemas.drawingml.x2006.main.CTTable;
  37. import org.openxmlformats.schemas.drawingml.x2006.main.CTTableRow;
  38. import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
  39. import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrameNonVisual;
  40. /**
  41. * Represents a table in a .pptx presentation
  42. *
  43. * @author Yegor Kozlov
  44. */
  45. public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow>,
  46. TableShape<XSLFShape,XSLFTextParagraph> {
  47. static String TABLE_URI = "http://schemas.openxmlformats.org/drawingml/2006/table";
  48. private CTTable _table;
  49. private List<XSLFTableRow> _rows;
  50. /*package*/ XSLFTable(CTGraphicalObjectFrame shape, XSLFSheet sheet){
  51. super(shape, sheet);
  52. XmlObject[] rs = shape.getGraphic().getGraphicData()
  53. .selectPath("declare namespace a='http://schemas.openxmlformats.org/drawingml/2006/main' ./a:tbl");
  54. if (rs.length == 0) {
  55. throw new IllegalStateException("a:tbl element was not found in\n " + shape.getGraphic().getGraphicData());
  56. }
  57. // Pesky XmlBeans bug - see Bugzilla #49934
  58. // it never happens when using the full ooxml-schemas jar but may happen with the abridged poi-ooxml-schemas
  59. if(rs[0] instanceof XmlAnyTypeImpl){
  60. try {
  61. rs[0] = CTTable.Factory.parse(rs[0].toString(), DEFAULT_XML_OPTIONS);
  62. }catch (XmlException e){
  63. throw new POIXMLException(e);
  64. }
  65. }
  66. _table = (CTTable) rs[0];
  67. CTTableRow[] trArray = _table.getTrArray();
  68. _rows = new ArrayList<XSLFTableRow>(trArray.length);
  69. for(CTTableRow row : trArray) _rows.add(new XSLFTableRow(row, this));
  70. }
  71. @Override
  72. public XSLFTableCell getCell(int row, int col) {
  73. List<XSLFTableRow> rows = getRows();
  74. if (row < 0 || rows.size() <= row) {
  75. return null;
  76. }
  77. XSLFTableRow r = rows.get(row);
  78. if (r == null) {
  79. // empty row
  80. return null;
  81. }
  82. List<XSLFTableCell> cells = r.getCells();
  83. if (col < 0 || cells.size() <= col) {
  84. return null;
  85. }
  86. // cell can be potentially empty ...
  87. return cells.get(col);
  88. }
  89. @Internal
  90. public CTTable getCTTable(){
  91. return _table;
  92. }
  93. @Override
  94. public int getNumberOfColumns() {
  95. return _table.getTblGrid().sizeOfGridColArray();
  96. }
  97. @Override
  98. public int getNumberOfRows() {
  99. return _table.sizeOfTrArray();
  100. }
  101. @Override
  102. public double getColumnWidth(int idx){
  103. return Units.toPoints(
  104. _table.getTblGrid().getGridColArray(idx).getW());
  105. }
  106. @Override
  107. public void setColumnWidth(int idx, double width) {
  108. _table.getTblGrid().getGridColArray(idx).setW(Units.toEMU(width));
  109. }
  110. @Override
  111. public double getRowHeight(int row) {
  112. return Units.toPoints(_table.getTrArray(row).getH());
  113. }
  114. @Override
  115. public void setRowHeight(int row, double height) {
  116. _table.getTrArray(row).setH(Units.toEMU(height));
  117. }
  118. public Iterator<XSLFTableRow> iterator(){
  119. return _rows.iterator();
  120. }
  121. public List<XSLFTableRow> getRows(){
  122. return Collections.unmodifiableList(_rows);
  123. }
  124. public XSLFTableRow addRow(){
  125. CTTableRow tr = _table.addNewTr();
  126. XSLFTableRow row = new XSLFTableRow(tr, this);
  127. row.setHeight(20.0); // default height is 20 points
  128. _rows.add(row);
  129. return row;
  130. }
  131. static CTGraphicalObjectFrame prototype(int shapeId){
  132. CTGraphicalObjectFrame frame = CTGraphicalObjectFrame.Factory.newInstance();
  133. CTGraphicalObjectFrameNonVisual nvGr = frame.addNewNvGraphicFramePr();
  134. CTNonVisualDrawingProps cnv = nvGr.addNewCNvPr();
  135. cnv.setName("Table " + shapeId);
  136. cnv.setId(shapeId + 1);
  137. nvGr.addNewCNvGraphicFramePr().addNewGraphicFrameLocks().setNoGrp(true);
  138. nvGr.addNewNvPr();
  139. frame.addNewXfrm();
  140. CTGraphicalObjectData gr = frame.addNewGraphic().addNewGraphicData();
  141. XmlCursor cursor = gr.newCursor();
  142. cursor.toNextToken();
  143. cursor.beginElement(new QName("http://schemas.openxmlformats.org/drawingml/2006/main", "tbl"));
  144. cursor.beginElement(new QName("http://schemas.openxmlformats.org/drawingml/2006/main", "tblPr"));
  145. cursor.toNextToken();
  146. cursor.beginElement(new QName("http://schemas.openxmlformats.org/drawingml/2006/main", "tblGrid"));
  147. cursor.dispose();
  148. gr.setUri(TABLE_URI);
  149. return frame;
  150. }
  151. /**
  152. * Merge cells of a table
  153. */
  154. public void mergeCells(int firstRow, int lastRow, int firstCol, int lastCol) {
  155. if(firstRow > lastRow) {
  156. throw new IllegalArgumentException(
  157. "Cannot merge, first row > last row : "
  158. + firstRow + " > " + lastRow
  159. );
  160. }
  161. if(firstCol > lastCol) {
  162. throw new IllegalArgumentException(
  163. "Cannot merge, first column > last column : "
  164. + firstCol + " > " + lastCol
  165. );
  166. }
  167. int rowSpan = (lastRow - firstRow) + 1;
  168. boolean mergeRowRequired = rowSpan > 1;
  169. int colSpan = (lastCol - firstCol) + 1;
  170. boolean mergeColumnRequired = colSpan > 1;
  171. for(int i = firstRow; i <= lastRow; i++) {
  172. XSLFTableRow row = _rows.get(i);
  173. for(int colPos = firstCol; colPos <= lastCol; colPos++) {
  174. XSLFTableCell cell = row.getCells().get(colPos);
  175. if(mergeRowRequired) {
  176. if(i == firstRow) {
  177. cell.setRowSpan(rowSpan);
  178. } else {
  179. cell.setVMerge(true);
  180. }
  181. }
  182. if(mergeColumnRequired) {
  183. if(colPos == firstCol) {
  184. cell.setGridSpan(colSpan);
  185. } else {
  186. cell.setHMerge(true);
  187. }
  188. }
  189. }
  190. }
  191. }
  192. }