Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ColumnHelper.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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.helpers;
  16. import java.util.ArrayList;
  17. import java.util.Arrays;
  18. import java.util.Comparator;
  19. import java.util.HashSet;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.ListIterator;
  23. import java.util.Set;
  24. import java.util.TreeSet;
  25. import org.apache.poi.ss.usermodel.CellStyle;
  26. import org.apache.poi.xssf.util.CTColComparator;
  27. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
  28. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
  29. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
  30. /**
  31. * Helper class for dealing with the Column settings on
  32. * a CTWorksheet (the data part of a sheet).
  33. * Note - within POI, we use 0 based column indexes, but
  34. * the column definitions in the XML are 1 based!
  35. */
  36. public class ColumnHelper {
  37. private CTWorksheet worksheet;
  38. private CTCols newCols;
  39. public ColumnHelper(CTWorksheet worksheet) {
  40. super();
  41. this.worksheet = worksheet;
  42. cleanColumns();
  43. }
  44. public void cleanColumns() {
  45. this.newCols = CTCols.Factory.newInstance();
  46. CTCols aggregateCols = CTCols.Factory.newInstance();
  47. List<CTCols> colsList = worksheet.getColsList();
  48. if (colsList != null) {
  49. for (CTCols cols : colsList) {
  50. for (CTCol col : cols.getColList()) {
  51. cloneCol(aggregateCols, col);
  52. }
  53. }
  54. }
  55. sortColumns(aggregateCols);
  56. CTCol[] colArray = new CTCol[aggregateCols.getColList().size()];
  57. aggregateCols.getColList().toArray(colArray);
  58. sweepCleanColumns(newCols, colArray, null);
  59. int i = colsList.size();
  60. for (int y = i - 1; y >= 0; y--) {
  61. worksheet.removeCols(y);
  62. }
  63. worksheet.addNewCols();
  64. worksheet.setColsArray(0, newCols);
  65. }
  66. private static class CTColByMaxComparator implements Comparator<CTCol> {
  67. public int compare(CTCol arg0, CTCol arg1) {
  68. if (arg0.getMax() < arg1.getMax()) {
  69. return -1;
  70. } else {
  71. if (arg0.getMax() > arg1.getMax()) return 1;
  72. else return 0;
  73. }
  74. }
  75. }
  76. /**
  77. * @see http://en.wikipedia.org/wiki/Sweep_line_algorithm
  78. */
  79. private void sweepCleanColumns(CTCols cols, CTCol[] flattenedColsArray, CTCol overrideColumn) {
  80. List<CTCol> flattenedCols = new ArrayList<CTCol>(Arrays.asList(flattenedColsArray));
  81. TreeSet<CTCol> currentElements = new TreeSet<CTCol>(new CTColByMaxComparator());
  82. ListIterator<CTCol> flIter = flattenedCols.listIterator();
  83. CTCol haveOverrideColumn = null;
  84. long lastMaxIndex = 0;
  85. long currentMax = 0;
  86. while (flIter.hasNext()) {
  87. CTCol col = flIter.next();
  88. long currentIndex = col.getMin();
  89. long nextIndex = (col.getMax() > currentMax) ? col.getMax() : currentMax;
  90. if (flIter.hasNext()) {
  91. nextIndex = flIter.next().getMin();
  92. flIter.previous();
  93. }
  94. Iterator<CTCol> iter = currentElements.iterator();
  95. while (iter.hasNext()) {
  96. CTCol elem = iter.next();
  97. if (currentIndex <= elem.getMax()) break; // all passed elements have been purged
  98. iter.remove();
  99. }
  100. if (!currentElements.isEmpty() && lastMaxIndex < currentIndex) {
  101. // we need to process previous elements first
  102. insertCol(cols, lastMaxIndex, currentIndex - 1, currentElements.toArray(new CTCol[]{}), true, haveOverrideColumn);
  103. }
  104. currentElements.add(col);
  105. if (col.getMax() > currentMax) currentMax = col.getMax();
  106. if (col.equals(overrideColumn)) haveOverrideColumn = overrideColumn;
  107. while (currentIndex <= nextIndex && !currentElements.isEmpty()) {
  108. Set<CTCol> currentIndexElements = new HashSet<CTCol>();
  109. CTCol currentElem = currentElements.first();
  110. long currentElemIndex = currentElem.getMax();
  111. currentIndexElements.add(currentElem);
  112. while (currentElements.higher(currentElem) != null && currentElements.higher(currentElem).getMax() == currentElemIndex) {
  113. currentElem = currentElements.higher(currentElem);
  114. currentIndexElements.add(currentElem);
  115. if (col.getMax() > currentMax) currentMax = col.getMax();
  116. if (col.equals(overrideColumn)) haveOverrideColumn = overrideColumn;
  117. }
  118. if (currentElemIndex < nextIndex || !flIter.hasNext()) {
  119. insertCol(cols, currentIndex, currentElemIndex, currentElements.toArray(new CTCol[]{}), true, haveOverrideColumn);
  120. if (flIter.hasNext()) {
  121. if (nextIndex > currentElemIndex) {
  122. currentElements.removeAll(currentIndexElements);
  123. if (currentIndexElements.contains(overrideColumn)) haveOverrideColumn = null;
  124. }
  125. } else {
  126. currentElements.removeAll(currentIndexElements);
  127. if (currentIndexElements.contains(overrideColumn)) haveOverrideColumn = null;
  128. }
  129. lastMaxIndex = currentIndex = currentElemIndex + 1;
  130. } else {
  131. lastMaxIndex = currentIndex;
  132. currentIndex = nextIndex + 1;
  133. }
  134. }
  135. }
  136. sortColumns(cols);
  137. }
  138. public static void sortColumns(CTCols newCols) {
  139. CTCol[] colArray = new CTCol[newCols.getColList().size()];
  140. newCols.getColList().toArray(colArray);
  141. Arrays.sort(colArray, new CTColComparator());
  142. newCols.setColArray(colArray);
  143. }
  144. public CTCol cloneCol(CTCols cols, CTCol col) {
  145. CTCol newCol = cols.addNewCol();
  146. newCol.setMin(col.getMin());
  147. newCol.setMax(col.getMax());
  148. setColumnAttributes(col, newCol);
  149. return newCol;
  150. }
  151. /**
  152. * Returns the Column at the given 0 based index
  153. */
  154. public CTCol getColumn(long index, boolean splitColumns) {
  155. return getColumn1Based(index+1, splitColumns);
  156. }
  157. /**
  158. * Returns the Column at the given 1 based index.
  159. * POI default is 0 based, but the file stores
  160. * as 1 based.
  161. */
  162. public CTCol getColumn1Based(long index1, boolean splitColumns) {
  163. CTCols colsArray = worksheet.getColsArray(0);
  164. for (int i = 0; i < colsArray.sizeOfColArray(); i++) {
  165. CTCol colArray = colsArray.getColArray(i);
  166. if (colArray.getMin() <= index1 && colArray.getMax() >= index1) {
  167. if (splitColumns) {
  168. if (colArray.getMin() < index1) {
  169. insertCol(colsArray, colArray.getMin(), (index1 - 1), new CTCol[]{colArray});
  170. }
  171. if (colArray.getMax() > index1) {
  172. insertCol(colsArray, (index1 + 1), colArray.getMax(), new CTCol[]{colArray});
  173. }
  174. colArray.setMin(index1);
  175. colArray.setMax(index1);
  176. }
  177. return colArray;
  178. }
  179. }
  180. return null;
  181. }
  182. public CTCols addCleanColIntoCols(CTCols cols, CTCol col) {
  183. CTCols newCols = CTCols.Factory.newInstance();
  184. for (CTCol c : cols.getColList()) {
  185. cloneCol(newCols, c);
  186. }
  187. cloneCol(newCols, col);
  188. sortColumns(newCols);
  189. CTCol[] colArray = new CTCol[newCols.getColList().size()];
  190. newCols.getColList().toArray(colArray);
  191. CTCols returnCols = CTCols.Factory.newInstance();
  192. sweepCleanColumns(returnCols, colArray, col);
  193. colArray = new CTCol[returnCols.getColList().size()];
  194. returnCols.getColList().toArray(colArray);
  195. cols.setColArray(colArray);
  196. return returnCols;
  197. }
  198. /*
  199. * Insert a new CTCol at position 0 into cols, setting min=min, max=max and
  200. * copying all the colsWithAttributes array cols attributes into newCol
  201. */
  202. private CTCol insertCol(CTCols cols, long min, long max, CTCol[] colsWithAttributes) {
  203. return insertCol(cols, min, max, colsWithAttributes, false, null);
  204. }
  205. private CTCol insertCol(CTCols cols, long min, long max,
  206. CTCol[] colsWithAttributes, boolean ignoreExistsCheck, CTCol overrideColumn) {
  207. if(ignoreExistsCheck || !columnExists(cols,min,max)){
  208. CTCol newCol = cols.insertNewCol(0);
  209. newCol.setMin(min);
  210. newCol.setMax(max);
  211. for (CTCol col : colsWithAttributes) {
  212. setColumnAttributes(col, newCol);
  213. }
  214. if (overrideColumn != null) setColumnAttributes(overrideColumn, newCol);
  215. return newCol;
  216. }
  217. return null;
  218. }
  219. /**
  220. * Does the column at the given 0 based index exist
  221. * in the supplied list of column definitions?
  222. */
  223. public boolean columnExists(CTCols cols, long index) {
  224. return columnExists1Based(cols, index+1);
  225. }
  226. private boolean columnExists1Based(CTCols cols, long index1) {
  227. for (int i = 0; i < cols.sizeOfColArray(); i++) {
  228. if (cols.getColArray(i).getMin() == index1) {
  229. return true;
  230. }
  231. }
  232. return false;
  233. }
  234. public void setColumnAttributes(CTCol fromCol, CTCol toCol) {
  235. if(fromCol.isSetBestFit()) toCol.setBestFit(fromCol.getBestFit());
  236. if(fromCol.isSetCustomWidth()) toCol.setCustomWidth(fromCol.getCustomWidth());
  237. if(fromCol.isSetHidden()) toCol.setHidden(fromCol.getHidden());
  238. if(fromCol.isSetStyle()) toCol.setStyle(fromCol.getStyle());
  239. if(fromCol.isSetWidth()) toCol.setWidth(fromCol.getWidth());
  240. if(fromCol.isSetCollapsed()) toCol.setCollapsed(fromCol.getCollapsed());
  241. if(fromCol.isSetPhonetic()) toCol.setPhonetic(fromCol.getPhonetic());
  242. if(fromCol.isSetOutlineLevel()) toCol.setOutlineLevel(fromCol.getOutlineLevel());
  243. toCol.setCollapsed(fromCol.isSetCollapsed());
  244. }
  245. public void setColBestFit(long index, boolean bestFit) {
  246. CTCol col = getOrCreateColumn1Based(index+1, false);
  247. col.setBestFit(bestFit);
  248. }
  249. public void setCustomWidth(long index, boolean bestFit) {
  250. CTCol col = getOrCreateColumn1Based(index+1, true);
  251. col.setCustomWidth(bestFit);
  252. }
  253. public void setColWidth(long index, double width) {
  254. CTCol col = getOrCreateColumn1Based(index+1, true);
  255. col.setWidth(width);
  256. }
  257. public void setColHidden(long index, boolean hidden) {
  258. CTCol col = getOrCreateColumn1Based(index+1, true);
  259. col.setHidden(hidden);
  260. }
  261. /**
  262. * Return the CTCol at the given (0 based) column index,
  263. * creating it if required.
  264. */
  265. protected CTCol getOrCreateColumn1Based(long index1, boolean splitColumns) {
  266. CTCol col = getColumn1Based(index1, splitColumns);
  267. if (col == null) {
  268. col = worksheet.getColsArray(0).addNewCol();
  269. col.setMin(index1);
  270. col.setMax(index1);
  271. }
  272. return col;
  273. }
  274. public void setColDefaultStyle(long index, CellStyle style) {
  275. setColDefaultStyle(index, style.getIndex());
  276. }
  277. public void setColDefaultStyle(long index, int styleId) {
  278. CTCol col = getOrCreateColumn1Based(index+1, true);
  279. col.setStyle(styleId);
  280. }
  281. // Returns -1 if no column is found for the given index
  282. public int getColDefaultStyle(long index) {
  283. if (getColumn(index, false) != null) {
  284. return (int) getColumn(index, false).getStyle();
  285. }
  286. return -1;
  287. }
  288. private boolean columnExists(CTCols cols, long min, long max) {
  289. for (int i = 0; i < cols.sizeOfColArray(); i++) {
  290. if (cols.getColArray(i).getMin() == min && cols.getColArray(i).getMax() == max) {
  291. return true;
  292. }
  293. }
  294. return false;
  295. }
  296. public int getIndexOfColumn(CTCols cols, CTCol col) {
  297. for (int i = 0; i < cols.sizeOfColArray(); i++) {
  298. if (cols.getColArray(i).getMin() == col.getMin() && cols.getColArray(i).getMax() == col.getMax()) {
  299. return i;
  300. }
  301. }
  302. return -1;
  303. }
  304. }