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.

SXSSFSheet.java 40KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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.streaming;
  16. import java.io.*;
  17. import java.util.Iterator;
  18. import java.util.TreeMap;
  19. import java.util.Map;
  20. import org.apache.poi.ss.SpreadsheetVersion;
  21. import org.apache.poi.ss.usermodel.*;
  22. import org.apache.poi.ss.util.AreaReference;
  23. import org.apache.poi.ss.util.SheetUtil;
  24. import org.apache.poi.xssf.usermodel.XSSFSheet;
  25. import org.apache.poi.hssf.util.PaneInformation;
  26. import org.apache.poi.ss.util.CellRangeAddress;
  27. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetFormatPr;
  28. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
  29. /**
  30. * Streaming version of XSSFSheet implementing the "BigGridDemo" strategy.
  31. *
  32. * @author Alex Geller, Four J's Development Tools
  33. */
  34. public class SXSSFSheet implements Sheet, Cloneable
  35. {
  36. SXSSFWorkbook _workbook;
  37. XSSFSheet _sh;
  38. TreeMap<Integer,SXSSFRow> _rows=new TreeMap<Integer,SXSSFRow>();
  39. SheetDataWriter _writer;
  40. int _randomAccessWindowSize = SXSSFWorkbook.DEFAULT_WINDOW_SIZE;
  41. int outlineLevelRow = 0;
  42. public SXSSFSheet(SXSSFWorkbook workbook, XSSFSheet xSheet) throws IOException
  43. {
  44. _workbook=workbook;
  45. _sh=xSheet;
  46. _writer = workbook.createSheetDataWriter();
  47. setRandomAccessWindowSize(_workbook.getRandomAccessWindowSize());
  48. }
  49. /**
  50. * for testing purposes only
  51. */
  52. SheetDataWriter getSheetDataWriter(){
  53. return _writer;
  54. }
  55. /* Gets "<sheetData>" document fragment*/
  56. public InputStream getWorksheetXMLInputStream() throws IOException
  57. {
  58. // flush all remaining data and close the temp file writer
  59. flushRows(0);
  60. _writer.close();
  61. return _writer.getWorksheetXMLInputStream();
  62. }
  63. //start of interface implementation
  64. public Iterator<Row> iterator()
  65. {
  66. return rowIterator();
  67. }
  68. /**
  69. * Create a new row within the sheet and return the high level representation
  70. *
  71. * @param rownum row number
  72. * @return high level Row object representing a row in the sheet
  73. * @see #removeRow(Row)
  74. */
  75. public Row createRow(int rownum)
  76. {
  77. int maxrow = SpreadsheetVersion.EXCEL2007.getLastRowIndex();
  78. if (rownum < 0 || rownum > maxrow) {
  79. throw new IllegalArgumentException("Invalid row number (" + rownum
  80. + ") outside allowable range (0.." + maxrow + ")");
  81. }
  82. //Make the initial allocation as big as the row above.
  83. Row previousRow=rownum>0?getRow(rownum-1):null;
  84. int initialAllocationSize=0;
  85. //have previous row in memory -> take that value.
  86. if(previousRow!=null)
  87. initialAllocationSize=previousRow.getLastCellNum();
  88. //are we called after a flush(0)? If yes, ask the writer for the value.
  89. if(initialAllocationSize<=0&&_writer.getNumberOfFlushedRows()>0)
  90. initialAllocationSize=_writer.getNumberOfCellsOfLastFlushedRow();
  91. //default to 10 on the first row.
  92. if(initialAllocationSize<=0)
  93. initialAllocationSize=10;
  94. SXSSFRow newRow=new SXSSFRow(this,initialAllocationSize);
  95. _rows.put(new Integer(rownum),newRow);
  96. if(_randomAccessWindowSize>=0&&_rows.size()>_randomAccessWindowSize)
  97. {
  98. try
  99. {
  100. flushRows(_randomAccessWindowSize);
  101. }
  102. catch (IOException ioe)
  103. {
  104. throw new RuntimeException(ioe);
  105. }
  106. }
  107. return newRow;
  108. }
  109. /**
  110. * Remove a row from this sheet. All cells contained in the row are removed as well
  111. *
  112. * @param row representing a row to remove.
  113. */
  114. public void removeRow(Row row)
  115. {
  116. if (row.getSheet() != this) {
  117. throw new IllegalArgumentException("Specified row does not belong to this sheet");
  118. }
  119. for(Iterator<Map.Entry<Integer,SXSSFRow>> iter=_rows.entrySet().iterator();iter.hasNext();)
  120. {
  121. Map.Entry<Integer,SXSSFRow> entry=iter.next();
  122. if(entry.getValue()==row)
  123. {
  124. iter.remove();
  125. return;
  126. }
  127. }
  128. }
  129. /**
  130. * Returns the logical row (not physical) 0-based. If you ask for a row that is not
  131. * defined you get a null. This is to say row 4 represents the fifth row on a sheet.
  132. *
  133. * @param rownum row to get (0-based)
  134. * @return Row representing the rownumber or null if its not defined on the sheet
  135. */
  136. public Row getRow(int rownum)
  137. {
  138. return _rows.get(new Integer(rownum));
  139. }
  140. /**
  141. * Returns the number of physically defined rows (NOT the number of rows in the sheet)
  142. *
  143. * @return the number of physically defined rows in this sheet
  144. */
  145. public int getPhysicalNumberOfRows()
  146. {
  147. return _rows.size()+_writer.getNumberOfFlushedRows();
  148. }
  149. /**
  150. * Gets the first row on the sheet
  151. *
  152. * @return the number of the first logical row on the sheet (0-based)
  153. */
  154. public int getFirstRowNum()
  155. {
  156. if(_writer.getNumberOfFlushedRows() > 0)
  157. return _writer.getLowestIndexOfFlushedRows();
  158. return _rows.size() == 0 ? 0 : _rows.firstKey();
  159. }
  160. /**
  161. * Gets the last row on the sheet
  162. *
  163. * @return last row contained n this sheet (0-based)
  164. */
  165. public int getLastRowNum()
  166. {
  167. return _rows.size() == 0 ? 0 : _rows.lastKey();
  168. }
  169. /**
  170. * Get the visibility state for a given column
  171. *
  172. * @param columnIndex - the column to get (0-based)
  173. * @param hidden - the visiblity state of the column
  174. */
  175. public void setColumnHidden(int columnIndex, boolean hidden)
  176. {
  177. _sh.setColumnHidden(columnIndex,hidden);
  178. }
  179. /**
  180. * Get the hidden state for a given column
  181. *
  182. * @param columnIndex - the column to set (0-based)
  183. * @return hidden - <code>false</code> if the column is visible
  184. */
  185. public boolean isColumnHidden(int columnIndex)
  186. {
  187. return _sh.isColumnHidden(columnIndex);
  188. }
  189. /**
  190. * Set the width (in units of 1/256th of a character width)
  191. * <p>
  192. * The maximum column width for an individual cell is 255 characters.
  193. * This value represents the number of characters that can be displayed
  194. * in a cell that is formatted with the standard font.
  195. * </p>
  196. *
  197. * @param columnIndex - the column to set (0-based)
  198. * @param width - the width in units of 1/256th of a character width
  199. */
  200. public void setColumnWidth(int columnIndex, int width)
  201. {
  202. _sh.setColumnWidth(columnIndex,width);
  203. }
  204. /**
  205. * get the width (in units of 1/256th of a character width )
  206. * @param columnIndex - the column to set (0-based)
  207. * @return width - the width in units of 1/256th of a character width
  208. */
  209. public int getColumnWidth(int columnIndex)
  210. {
  211. return _sh.getColumnWidth(columnIndex);
  212. }
  213. /**
  214. * Set the default column width for the sheet (if the columns do not define their own width)
  215. * in characters
  216. *
  217. * @param width default column width measured in characters
  218. */
  219. public void setDefaultColumnWidth(int width)
  220. {
  221. _sh.setDefaultColumnWidth(width);
  222. }
  223. /**
  224. * Get the default column width for the sheet (if the columns do not define their own width)
  225. * in characters
  226. *
  227. * @return default column width measured in characters
  228. */
  229. public int getDefaultColumnWidth()
  230. {
  231. return _sh.getDefaultColumnWidth();
  232. }
  233. /**
  234. * Get the default row height for the sheet (if the rows do not define their own height) in
  235. * twips (1/20 of a point)
  236. *
  237. * @return default row height measured in twips (1/20 of a point)
  238. */
  239. public short getDefaultRowHeight()
  240. {
  241. return _sh.getDefaultRowHeight();
  242. }
  243. /**
  244. * Get the default row height for the sheet (if the rows do not define their own height) in
  245. * points.
  246. *
  247. * @return default row height in points
  248. */
  249. public float getDefaultRowHeightInPoints()
  250. {
  251. return _sh.getDefaultRowHeightInPoints();
  252. }
  253. /**
  254. * Set the default row height for the sheet (if the rows do not define their own height) in
  255. * twips (1/20 of a point)
  256. *
  257. * @param height default row height measured in twips (1/20 of a point)
  258. */
  259. public void setDefaultRowHeight(short height)
  260. {
  261. _sh.setDefaultRowHeight(height);
  262. }
  263. /**
  264. * Set the default row height for the sheet (if the rows do not define their own height) in
  265. * points
  266. * @param height default row height
  267. */
  268. public void setDefaultRowHeightInPoints(float height)
  269. {
  270. _sh.setDefaultRowHeightInPoints(height);
  271. }
  272. /**
  273. * Returns the CellStyle that applies to the given
  274. * (0 based) column, or null if no style has been
  275. * set for that column
  276. */
  277. public CellStyle getColumnStyle(int column)
  278. {
  279. return _sh.getColumnStyle(column);
  280. }
  281. /**
  282. * Sets the CellStyle that applies to the given
  283. * (0 based) column.
  284. */
  285. // public CellStyle setColumnStyle(int column, CellStyle style);
  286. /**
  287. * Adds a merged region of cells (hence those cells form one)
  288. *
  289. * @param region (rowfrom/colfrom-rowto/colto) to merge
  290. * @return index of this region
  291. */
  292. public int addMergedRegion(CellRangeAddress region)
  293. {
  294. return _sh.addMergedRegion(region);
  295. }
  296. /**
  297. * Determines whether the output is vertically centered on the page.
  298. *
  299. * @param value true to vertically center, false otherwise.
  300. */
  301. public void setVerticallyCenter(boolean value)
  302. {
  303. _sh.setVerticallyCenter(value);
  304. }
  305. /**
  306. * Determines whether the output is horizontally centered on the page.
  307. *
  308. * @param value true to horizontally center, false otherwise.
  309. */
  310. public void setHorizontallyCenter(boolean value)
  311. {
  312. _sh.setHorizontallyCenter(value);
  313. }
  314. /**
  315. * Determine whether printed output for this sheet will be horizontally centered.
  316. */
  317. public boolean getHorizontallyCenter()
  318. {
  319. return _sh.getHorizontallyCenter();
  320. }
  321. /**
  322. * Determine whether printed output for this sheet will be vertically centered.
  323. */
  324. public boolean getVerticallyCenter()
  325. {
  326. return _sh.getVerticallyCenter();
  327. }
  328. /**
  329. * Removes a merged region of cells (hence letting them free)
  330. *
  331. * @param index of the region to unmerge
  332. */
  333. public void removeMergedRegion(int index)
  334. {
  335. _sh.removeMergedRegion(index);
  336. }
  337. /**
  338. * Returns the number of merged regions
  339. *
  340. * @return number of merged regions
  341. */
  342. public int getNumMergedRegions()
  343. {
  344. return _sh.getNumMergedRegions();
  345. }
  346. /**
  347. * Returns the merged region at the specified index
  348. *
  349. * @return the merged region at the specified index
  350. */
  351. public CellRangeAddress getMergedRegion(int index)
  352. {
  353. return _sh.getMergedRegion(index);
  354. }
  355. /**
  356. * Returns an iterator of the physical rows
  357. *
  358. * @return an iterator of the PHYSICAL rows. Meaning the 3rd element may not
  359. * be the third row if say for instance the second row is undefined.
  360. */
  361. public Iterator<Row> rowIterator()
  362. {
  363. @SuppressWarnings("unchecked")
  364. Iterator<Row> result = (Iterator<Row>)(Iterator<? extends Row>)_rows.values().iterator();
  365. return result;
  366. }
  367. /**
  368. * Flag indicating whether the sheet displays Automatic Page Breaks.
  369. *
  370. * @param value <code>true</code> if the sheet displays Automatic Page Breaks.
  371. */
  372. public void setAutobreaks(boolean value)
  373. {
  374. _sh.setAutobreaks(value);
  375. }
  376. /**
  377. * Set whether to display the guts or not
  378. *
  379. * @param value - guts or no guts
  380. */
  381. public void setDisplayGuts(boolean value)
  382. {
  383. _sh.setDisplayGuts(value);
  384. }
  385. /**
  386. * Set whether the window should show 0 (zero) in cells containing zero value.
  387. * When false, cells with zero value appear blank instead of showing the number zero.
  388. *
  389. * @param value whether to display or hide all zero values on the worksheet
  390. */
  391. public void setDisplayZeros(boolean value)
  392. {
  393. _sh.setDisplayZeros(value);
  394. }
  395. /**
  396. * Gets the flag indicating whether the window should show 0 (zero) in cells containing zero value.
  397. * When false, cells with zero value appear blank instead of showing the number zero.
  398. *
  399. * @return whether all zero values on the worksheet are displayed
  400. */
  401. public boolean isDisplayZeros()
  402. {
  403. return _sh.isDisplayZeros();
  404. }
  405. /**
  406. * Sets whether the worksheet is displayed from right to left instead of from left to right.
  407. *
  408. * @param value true for right to left, false otherwise.
  409. */
  410. public void setRightToLeft(boolean value)
  411. {
  412. _sh.setRightToLeft(value);
  413. }
  414. /**
  415. * Whether the text is displayed in right-to-left mode in the window
  416. *
  417. * @return whether the text is displayed in right-to-left mode in the window
  418. */
  419. public boolean isRightToLeft()
  420. {
  421. return _sh.isRightToLeft();
  422. }
  423. /**
  424. * Flag indicating whether the Fit to Page print option is enabled.
  425. *
  426. * @param value <code>true</code> if the Fit to Page print option is enabled.
  427. */
  428. public void setFitToPage(boolean value)
  429. {
  430. _sh.setFitToPage(value);
  431. }
  432. /**
  433. * Flag indicating whether summary rows appear below detail in an outline, when applying an outline.
  434. *
  435. * <p>
  436. * When true a summary row is inserted below the detailed data being summarized and a
  437. * new outline level is established on that row.
  438. * </p>
  439. * <p>
  440. * When false a summary row is inserted above the detailed data being summarized and a new outline level
  441. * is established on that row.
  442. * </p>
  443. * @param value <code>true</code> if row summaries appear below detail in the outline
  444. */
  445. public void setRowSumsBelow(boolean value)
  446. {
  447. _sh.setRowSumsBelow(value);
  448. }
  449. /**
  450. * Flag indicating whether summary columns appear to the right of detail in an outline, when applying an outline.
  451. *
  452. * <p>
  453. * When true a summary column is inserted to the right of the detailed data being summarized
  454. * and a new outline level is established on that column.
  455. * </p>
  456. * <p>
  457. * When false a summary column is inserted to the left of the detailed data being
  458. * summarized and a new outline level is established on that column.
  459. * </p>
  460. * @param value <code>true</code> if col summaries appear right of the detail in the outline
  461. */
  462. public void setRowSumsRight(boolean value)
  463. {
  464. _sh.setRowSumsRight(value);
  465. }
  466. /**
  467. * Flag indicating whether the sheet displays Automatic Page Breaks.
  468. *
  469. * @return <code>true</code> if the sheet displays Automatic Page Breaks.
  470. */
  471. public boolean getAutobreaks()
  472. {
  473. return _sh.getAutobreaks();
  474. }
  475. /**
  476. * Get whether to display the guts or not,
  477. * default value is true
  478. *
  479. * @return boolean - guts or no guts
  480. */
  481. public boolean getDisplayGuts()
  482. {
  483. return _sh.getDisplayGuts();
  484. }
  485. /**
  486. * Flag indicating whether the Fit to Page print option is enabled.
  487. *
  488. * @return <code>true</code> if the Fit to Page print option is enabled.
  489. */
  490. public boolean getFitToPage()
  491. {
  492. return _sh.getFitToPage();
  493. }
  494. /**
  495. * Flag indicating whether summary rows appear below detail in an outline, when applying an outline.
  496. *
  497. * <p>
  498. * When true a summary row is inserted below the detailed data being summarized and a
  499. * new outline level is established on that row.
  500. * </p>
  501. * <p>
  502. * When false a summary row is inserted above the detailed data being summarized and a new outline level
  503. * is established on that row.
  504. * </p>
  505. * @return <code>true</code> if row summaries appear below detail in the outline
  506. */
  507. public boolean getRowSumsBelow()
  508. {
  509. return _sh.getRowSumsBelow();
  510. }
  511. /**
  512. * Flag indicating whether summary columns appear to the right of detail in an outline, when applying an outline.
  513. *
  514. * <p>
  515. * When true a summary column is inserted to the right of the detailed data being summarized
  516. * and a new outline level is established on that column.
  517. * </p>
  518. * <p>
  519. * When false a summary column is inserted to the left of the detailed data being
  520. * summarized and a new outline level is established on that column.
  521. * </p>
  522. * @return <code>true</code> if col summaries appear right of the detail in the outline
  523. */
  524. public boolean getRowSumsRight()
  525. {
  526. return _sh.getRowSumsRight();
  527. }
  528. /**
  529. * Gets the flag indicating whether this sheet displays the lines
  530. * between rows and columns to make editing and reading easier.
  531. *
  532. * @return <code>true</code> if this sheet displays gridlines.
  533. * @see #isPrintGridlines() to check if printing of gridlines is turned on or off
  534. */
  535. public boolean isPrintGridlines()
  536. {
  537. return _sh.isPrintGridlines();
  538. }
  539. /**
  540. * Sets the flag indicating whether this sheet should display the lines
  541. * between rows and columns to make editing and reading easier.
  542. * To turn printing of gridlines use {@link #setPrintGridlines(boolean)}
  543. *
  544. *
  545. * @param show <code>true</code> if this sheet should display gridlines.
  546. * @see #setPrintGridlines(boolean)
  547. */
  548. public void setPrintGridlines(boolean show)
  549. {
  550. _sh.setPrintGridlines(show);
  551. }
  552. /**
  553. * Gets the print setup object.
  554. *
  555. * @return The user model for the print setup object.
  556. */
  557. public PrintSetup getPrintSetup()
  558. {
  559. return _sh.getPrintSetup();
  560. }
  561. /**
  562. * Gets the user model for the default document header.
  563. * <p/>
  564. * Note that XSSF offers more kinds of document headers than HSSF does
  565. * </p>
  566. * @return the document header. Never <code>null</code>
  567. */
  568. public Header getHeader()
  569. {
  570. return _sh.getHeader();
  571. }
  572. /**
  573. * Gets the user model for the default document footer.
  574. * <p/>
  575. * Note that XSSF offers more kinds of document footers than HSSF does.
  576. *
  577. * @return the document footer. Never <code>null</code>
  578. */
  579. public Footer getFooter()
  580. {
  581. return _sh.getFooter();
  582. }
  583. /**
  584. * Sets a flag indicating whether this sheet is selected.
  585. *<p>
  586. * Note: multiple sheets can be selected, but only one sheet can be active at one time.
  587. *</p>
  588. * @param value <code>true</code> if this sheet is selected
  589. * @see Workbook#setActiveSheet(int)
  590. */
  591. public void setSelected(boolean value)
  592. {
  593. _sh.setSelected(value);
  594. }
  595. /**
  596. * Gets the size of the margin in inches.
  597. *
  598. * @param margin which margin to get
  599. * @return the size of the margin
  600. */
  601. public double getMargin(short margin)
  602. {
  603. return _sh.getMargin(margin);
  604. }
  605. /**
  606. * Sets the size of the margin in inches.
  607. *
  608. * @param margin which margin to get
  609. * @param size the size of the margin
  610. */
  611. public void setMargin(short margin, double size)
  612. {
  613. _sh.setMargin(margin,size);
  614. }
  615. /**
  616. * Answer whether protection is enabled or disabled
  617. *
  618. * @return true => protection enabled; false => protection disabled
  619. */
  620. public boolean getProtect()
  621. {
  622. return _sh.getProtect();
  623. }
  624. /**
  625. * Sets the protection enabled as well as the password
  626. * @param password to set for protection. Pass <code>null</code> to remove protection
  627. */
  628. public void protectSheet(String password)
  629. {
  630. _sh.protectSheet(password);
  631. }
  632. /**
  633. * Answer whether scenario protection is enabled or disabled
  634. *
  635. * @return true => protection enabled; false => protection disabled
  636. */
  637. public boolean getScenarioProtect()
  638. {
  639. return _sh.getScenarioProtect();
  640. }
  641. /**
  642. * Sets the zoom magnication for the sheet. The zoom is expressed as a
  643. * fraction. For example to express a zoom of 75% use 3 for the numerator
  644. * and 4 for the denominator.
  645. *
  646. * @param numerator The numerator for the zoom magnification.
  647. * @param denominator The denominator for the zoom magnification.
  648. */
  649. public void setZoom(int numerator, int denominator)
  650. {
  651. _sh.setZoom(numerator,denominator);
  652. }
  653. /**
  654. * The top row in the visible view when the sheet is
  655. * first viewed after opening it in a viewer
  656. *
  657. * @return short indicating the rownum (0 based) of the top row
  658. */
  659. public short getTopRow()
  660. {
  661. return _sh.getTopRow();
  662. }
  663. /**
  664. * The left col in the visible view when the sheet is
  665. * first viewed after opening it in a viewer
  666. *
  667. * @return short indicating the rownum (0 based) of the top row
  668. */
  669. public short getLeftCol()
  670. {
  671. return _sh.getLeftCol();
  672. }
  673. /**
  674. * Sets desktop window pane display area, when the
  675. * file is first opened in a viewer.
  676. *
  677. * @param toprow the top row to show in desktop window pane
  678. * @param leftcol the left column to show in desktop window pane
  679. */
  680. public void showInPane(short toprow, short leftcol)
  681. {
  682. _sh.showInPane(toprow, leftcol);
  683. }
  684. /**
  685. * Control if Excel should be asked to recalculate all formulas when the
  686. * workbook is opened, via the "sheetCalcPr fullCalcOnLoad" option.
  687. * Calculating the formula values with {@link org.apache.poi.ss.usermodel.FormulaEvaluator} is the
  688. * recommended solution, but this may be used for certain cases where
  689. * evaluation in POI is not possible.
  690. */
  691. public void setForceFormulaRecalculation(boolean value) {
  692. _sh.setForceFormulaRecalculation(value);
  693. }
  694. /**
  695. * Whether Excel will be asked to recalculate all formulas when the
  696. * workbook is opened.
  697. */
  698. public boolean getForceFormulaRecalculation() {
  699. return _sh.getForceFormulaRecalculation();
  700. }
  701. /**
  702. * Shifts rows between startRow and endRow n number of rows.
  703. * If you use a negative number, it will shift rows up.
  704. * Code ensures that rows don't wrap around.
  705. *
  706. * Calls shiftRows(startRow, endRow, n, false, false);
  707. *
  708. * <p>
  709. * Additionally shifts merged regions that are completely defined in these
  710. * rows (ie. merged 2 cells on a row to be shifted).
  711. * @param startRow the row to start shifting
  712. * @param endRow the row to end shifting
  713. * @param n the number of rows to shift
  714. */
  715. public void shiftRows(int startRow, int endRow, int n)
  716. {
  717. throw new RuntimeException("NotImplemented");
  718. }
  719. /**
  720. * Shifts rows between startRow and endRow n number of rows.
  721. * If you use a negative number, it will shift rows up.
  722. * Code ensures that rows don't wrap around
  723. *
  724. * <p>
  725. * Additionally shifts merged regions that are completely defined in these
  726. * rows (ie. merged 2 cells on a row to be shifted).
  727. * <p>
  728. * @param startRow the row to start shifting
  729. * @param endRow the row to end shifting
  730. * @param n the number of rows to shift
  731. * @param copyRowHeight whether to copy the row height during the shift
  732. * @param resetOriginalRowHeight whether to set the original row's height to the default
  733. */
  734. public void shiftRows(int startRow, int endRow, int n, boolean copyRowHeight, boolean resetOriginalRowHeight)
  735. {
  736. throw new RuntimeException("NotImplemented");
  737. }
  738. /**
  739. * Creates a split (freezepane). Any existing freezepane or split pane is overwritten.
  740. * @param colSplit Horizonatal position of split.
  741. * @param rowSplit Vertical position of split.
  742. * @param leftmostColumn Left column visible in right pane.
  743. * @param topRow Top row visible in bottom pane
  744. */
  745. public void createFreezePane(int colSplit, int rowSplit, int leftmostColumn, int topRow)
  746. {
  747. _sh.createFreezePane(colSplit, rowSplit, leftmostColumn, topRow);
  748. }
  749. /**
  750. * Creates a split (freezepane). Any existing freezepane or split pane is overwritten.
  751. * @param colSplit Horizonatal position of split.
  752. * @param rowSplit Vertical position of split.
  753. */
  754. public void createFreezePane(int colSplit, int rowSplit)
  755. {
  756. _sh.createFreezePane(colSplit,rowSplit);
  757. }
  758. /**
  759. * Creates a split pane. Any existing freezepane or split pane is overwritten.
  760. * @param xSplitPos Horizonatal position of split (in 1/20th of a point).
  761. * @param ySplitPos Vertical position of split (in 1/20th of a point).
  762. * @param topRow Top row visible in bottom pane
  763. * @param leftmostColumn Left column visible in right pane.
  764. * @param activePane Active pane. One of: PANE_LOWER_RIGHT,
  765. * PANE_UPPER_RIGHT, PANE_LOWER_LEFT, PANE_UPPER_LEFT
  766. * @see #PANE_LOWER_LEFT
  767. * @see #PANE_LOWER_RIGHT
  768. * @see #PANE_UPPER_LEFT
  769. * @see #PANE_UPPER_RIGHT
  770. */
  771. public void createSplitPane(int xSplitPos, int ySplitPos, int leftmostColumn, int topRow, int activePane)
  772. {
  773. _sh.createSplitPane(xSplitPos, ySplitPos, leftmostColumn, topRow, activePane);
  774. }
  775. /**
  776. * Returns the information regarding the currently configured pane (split or freeze)
  777. *
  778. * @return null if no pane configured, or the pane information.
  779. */
  780. public PaneInformation getPaneInformation()
  781. {
  782. return _sh.getPaneInformation();
  783. }
  784. /**
  785. * Sets whether the gridlines are shown in a viewer
  786. *
  787. * @param show whether to show gridlines or not
  788. */
  789. public void setDisplayGridlines(boolean show)
  790. {
  791. _sh.setDisplayGridlines(show);
  792. }
  793. /**
  794. * Returns if gridlines are displayed
  795. *
  796. * @return whether gridlines are displayed
  797. */
  798. public boolean isDisplayGridlines()
  799. {
  800. return _sh.isDisplayGridlines();
  801. }
  802. /**
  803. * Sets whether the formulas are shown in a viewer
  804. *
  805. * @param show whether to show formulas or not
  806. */
  807. public void setDisplayFormulas(boolean show)
  808. {
  809. _sh.setDisplayFormulas(show);
  810. }
  811. /**
  812. * Returns if formulas are displayed
  813. *
  814. * @return whether formulas are displayed
  815. */
  816. public boolean isDisplayFormulas()
  817. {
  818. return _sh.isDisplayFormulas();
  819. }
  820. /**
  821. * Sets whether the RowColHeadings are shown in a viewer
  822. *
  823. * @param show whether to show RowColHeadings or not
  824. */
  825. public void setDisplayRowColHeadings(boolean show)
  826. {
  827. _sh.setDisplayRowColHeadings(show);
  828. }
  829. /**
  830. * Returns if RowColHeadings are displayed.
  831. * @return whether RowColHeadings are displayed
  832. */
  833. public boolean isDisplayRowColHeadings()
  834. {
  835. return _sh.isDisplayRowColHeadings();
  836. }
  837. /**
  838. * Sets a page break at the indicated row
  839. * @param row FIXME: Document this!
  840. */
  841. public void setRowBreak(int row)
  842. {
  843. _sh.setRowBreak(row);
  844. }
  845. /**
  846. * Determines if there is a page break at the indicated row
  847. * @param row FIXME: Document this!
  848. * @return FIXME: Document this!
  849. */
  850. public boolean isRowBroken(int row)
  851. {
  852. return _sh.isRowBroken(row);
  853. }
  854. /**
  855. * Removes the page break at the indicated row
  856. * @param row
  857. */
  858. public void removeRowBreak(int row)
  859. {
  860. _sh.removeRowBreak(row);
  861. }
  862. /**
  863. * Retrieves all the horizontal page breaks
  864. * @return all the horizontal page breaks, or null if there are no row page breaks
  865. */
  866. public int[] getRowBreaks()
  867. {
  868. return _sh.getRowBreaks();
  869. }
  870. /**
  871. * Retrieves all the vertical page breaks
  872. * @return all the vertical page breaks, or null if there are no column page breaks
  873. */
  874. public int[] getColumnBreaks()
  875. {
  876. return _sh.getColumnBreaks();
  877. }
  878. /**
  879. * Sets a page break at the indicated column
  880. * @param column
  881. */
  882. public void setColumnBreak(int column)
  883. {
  884. _sh.setColumnBreak(column);
  885. }
  886. /**
  887. * Determines if there is a page break at the indicated column
  888. * @param column FIXME: Document this!
  889. * @return FIXME: Document this!
  890. */
  891. public boolean isColumnBroken(int column)
  892. {
  893. return _sh.isColumnBroken(column);
  894. }
  895. /**
  896. * Removes a page break at the indicated column
  897. * @param column
  898. */
  899. public void removeColumnBreak(int column)
  900. {
  901. _sh.removeColumnBreak(column);
  902. }
  903. /**
  904. * Expands or collapses a column group.
  905. *
  906. * @param columnNumber One of the columns in the group.
  907. * @param collapsed true = collapse group, false = expand group.
  908. */
  909. public void setColumnGroupCollapsed(int columnNumber, boolean collapsed)
  910. {
  911. _sh.setColumnGroupCollapsed(columnNumber, collapsed);
  912. }
  913. /**
  914. * Create an outline for the provided column range.
  915. *
  916. * @param fromColumn beginning of the column range.
  917. * @param toColumn end of the column range.
  918. */
  919. public void groupColumn(int fromColumn, int toColumn)
  920. {
  921. _sh.groupColumn(fromColumn,toColumn);
  922. }
  923. /**
  924. * Ungroup a range of columns that were previously groupped
  925. *
  926. * @param fromColumn start column (0-based)
  927. * @param toColumn end column (0-based)
  928. */
  929. public void ungroupColumn(int fromColumn, int toColumn)
  930. {
  931. _sh.ungroupColumn(fromColumn, toColumn);
  932. }
  933. /**
  934. * Tie a range of rows together so that they can be collapsed or expanded
  935. *
  936. * <p>
  937. * Please note the rows being grouped <em>must</em> be in the current window,
  938. * if the rows are already flushed then groupRow has no effect.
  939. * </p>
  940. * <p>
  941. * Correct code:
  942. * <pre><code>
  943. * Workbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory
  944. * Sheet sh = wb.createSheet();
  945. * for (int rownum = 0; rownum &lt; 1000; rownum++) {
  946. * Row row = sh.createRow(rownum);
  947. * if(rownum == 200) {
  948. * sh.groupRow(100, 200);
  949. * }
  950. * }
  951. *
  952. * </code></pre>
  953. * </p>
  954. * <p>
  955. * Incorrect code:
  956. * <pre><code>
  957. * Workbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory
  958. * Sheet sh = wb.createSheet();
  959. * for (int rownum = 0; rownum &lt; 1000; rownum++) {
  960. * Row row = sh.createRow(rownum);
  961. * }
  962. * sh.groupRow(100, 200); // the rows in the range [100, 200] are already flushed and groupRows has no effect
  963. *
  964. * </code></pre>
  965. * </p>
  966. *
  967. * @param fromRow start row (0-based)
  968. * @param toRow end row (0-based)
  969. */
  970. public void groupRow(int fromRow, int toRow)
  971. {
  972. for(SXSSFRow row : _rows.subMap(fromRow, toRow + 1).values()){
  973. int level = row.getOutlineLevel() + 1;
  974. row.setOutlineLevel(level);
  975. if(level > outlineLevelRow) outlineLevelRow = level;
  976. }
  977. CTWorksheet ct = _sh.getCTWorksheet();
  978. CTSheetFormatPr pr = ct.isSetSheetFormatPr() ?
  979. ct.getSheetFormatPr() :
  980. ct.addNewSheetFormatPr();
  981. if(outlineLevelRow > 0) pr.setOutlineLevelRow((short)outlineLevelRow);
  982. }
  983. /**
  984. * Ungroup a range of rows that were previously groupped
  985. *
  986. * @param fromRow start row (0-based)
  987. * @param toRow end row (0-based)
  988. */
  989. public void ungroupRow(int fromRow, int toRow)
  990. {
  991. _sh.ungroupRow(fromRow, toRow);
  992. }
  993. /**
  994. * Set view state of a groupped range of rows
  995. *
  996. * @param row start row of a groupped range of rows (0-based)
  997. * @param collapse whether to expand/collapse the detail rows
  998. */
  999. public void setRowGroupCollapsed(int row, boolean collapse)
  1000. {
  1001. //_sh.setRowGroupCollapsed(row, collapse);
  1002. throw new RuntimeException("Not Implemented");
  1003. }
  1004. /**
  1005. * Sets the default column style for a given column. POI will only apply this style to new cells added to the sheet.
  1006. *
  1007. * @param column the column index
  1008. * @param style the style to set
  1009. */
  1010. public void setDefaultColumnStyle(int column, CellStyle style)
  1011. {
  1012. _sh.setDefaultColumnStyle(column, style);
  1013. }
  1014. /**
  1015. * Adjusts the column width to fit the contents.
  1016. *
  1017. * <p>
  1018. * This process can be relatively slow on large sheets, so this should
  1019. * normally only be called once per column, at the end of your
  1020. * processing.
  1021. * </p>
  1022. * You can specify whether the content of merged cells should be considered or ignored.
  1023. * Default is to ignore merged cells.
  1024. *
  1025. * @param column the column index
  1026. */
  1027. public void autoSizeColumn(int column)
  1028. {
  1029. autoSizeColumn(column, false);
  1030. }
  1031. /**
  1032. * Adjusts the column width to fit the contents.
  1033. * <p>
  1034. * This process can be relatively slow on large sheets, so this should
  1035. * normally only be called once per column, at the end of your
  1036. * processing.
  1037. * </p>
  1038. * You can specify whether the content of merged cells should be considered or ignored.
  1039. * Default is to ignore merged cells.
  1040. *
  1041. * @param column the column index
  1042. * @param useMergedCells whether to use the contents of merged cells when calculating the width of the column
  1043. */
  1044. public void autoSizeColumn(int column, boolean useMergedCells)
  1045. {
  1046. double width = SheetUtil.getColumnWidth(this, column, useMergedCells);
  1047. if (width != -1) {
  1048. width *= 256;
  1049. int maxColumnWidth = 255*256; // The maximum column width for an individual cell is 255 characters
  1050. if (width > maxColumnWidth) {
  1051. width = maxColumnWidth;
  1052. }
  1053. setColumnWidth(column, (int)(width));
  1054. }
  1055. }
  1056. /**
  1057. * Returns cell comment for the specified row and column
  1058. *
  1059. * @return cell comment or <code>null</code> if not found
  1060. */
  1061. public Comment getCellComment(int row, int column)
  1062. {
  1063. return _sh.getCellComment(row, column);
  1064. }
  1065. /**
  1066. * Creates the top-level drawing patriarch.
  1067. *
  1068. * @return The new drawing patriarch.
  1069. */
  1070. public Drawing createDrawingPatriarch()
  1071. {
  1072. return _sh.createDrawingPatriarch();
  1073. }
  1074. /**
  1075. * Return the parent workbook
  1076. *
  1077. * @return the parent workbook
  1078. */
  1079. public Workbook getWorkbook()
  1080. {
  1081. return _workbook;
  1082. }
  1083. /**
  1084. * Returns the name of this sheet
  1085. *
  1086. * @return the name of this sheet
  1087. */
  1088. public String getSheetName()
  1089. {
  1090. return _sh.getSheetName();
  1091. }
  1092. /**
  1093. * Note - this is not the same as whether the sheet is focused (isActive)
  1094. * @return <code>true</code> if this sheet is currently selected
  1095. */
  1096. public boolean isSelected()
  1097. {
  1098. return _sh.isSelected();
  1099. }
  1100. /**
  1101. * Sets array formula to specified region for result.
  1102. *
  1103. * @param formula text representation of the formula
  1104. * @param range Region of array formula for result.
  1105. * @return the {@link CellRange} of cells affected by this change
  1106. */
  1107. public CellRange<? extends Cell> setArrayFormula(String formula, CellRangeAddress range)
  1108. {
  1109. return _sh.setArrayFormula(formula, range);
  1110. }
  1111. /**
  1112. * Remove a Array Formula from this sheet. All cells contained in the Array Formula range are removed as well
  1113. *
  1114. * @param cell any cell within Array Formula range
  1115. * @return the {@link CellRange} of cells affected by this change
  1116. */
  1117. public CellRange<? extends Cell> removeArrayFormula(Cell cell)
  1118. {
  1119. return _sh.removeArrayFormula(cell);
  1120. }
  1121. public DataValidationHelper getDataValidationHelper()
  1122. {
  1123. return _sh.getDataValidationHelper();
  1124. }
  1125. /**
  1126. * Creates a data validation object
  1127. * @param dataValidation The Data validation object settings
  1128. */
  1129. public void addValidationData(DataValidation dataValidation)
  1130. {
  1131. _sh.addValidationData(dataValidation);
  1132. }
  1133. /**
  1134. * Enable filtering for a range of cells
  1135. *
  1136. * @param range the range of cells to filter
  1137. */
  1138. public AutoFilter setAutoFilter(CellRangeAddress range)
  1139. {
  1140. return _sh.setAutoFilter(range);
  1141. }
  1142. public SheetConditionalFormatting getSheetConditionalFormatting(){
  1143. return _sh.getSheetConditionalFormatting();
  1144. }
  1145. @Override
  1146. public CellRangeAddress getRepeatingRows() {
  1147. return _sh.getRepeatingRows();
  1148. }
  1149. @Override
  1150. public CellRangeAddress getRepeatingColumns() {
  1151. return _sh.getRepeatingColumns();
  1152. }
  1153. //end of interface implementation
  1154. /**
  1155. * Specifies how many rows can be accessed at most via getRow().
  1156. * When a new node is created via createRow() and the total number
  1157. * of unflushed records would exeed the specified value, then the
  1158. * row with the lowest index value is flushed and cannot be accessed
  1159. * via getRow() anymore.
  1160. * A value of -1 indicates unlimited access. In this case all
  1161. * records that have not been flushed by a call to flush() are available
  1162. * for random access.
  1163. * A value of 0 is not allowed because it would flush any newly created row
  1164. * without having a chance to specify any cells.
  1165. */
  1166. public void setRandomAccessWindowSize(int value)
  1167. {
  1168. if(value == 0 || value < -1) {
  1169. throw new IllegalArgumentException("RandomAccessWindowSize must be either -1 or a positive integer");
  1170. }
  1171. _randomAccessWindowSize=value;
  1172. }
  1173. /**
  1174. * Specifies how many rows can be accessed at most via getRow().
  1175. * The exeeding rows (if any) are flushed to the disk while rows
  1176. * with lower index values are flushed first.
  1177. */
  1178. public void flushRows(int remaining) throws IOException
  1179. {
  1180. while(_rows.size() > remaining) flushOneRow();
  1181. }
  1182. /**
  1183. * Flush all rows to disk. After this call no rows can be accessed via getRow()
  1184. *
  1185. * @throws IOException
  1186. */
  1187. public void flushRows() throws IOException
  1188. {
  1189. this.flushRows(0);
  1190. }
  1191. private void flushOneRow() throws IOException
  1192. {
  1193. Integer firstRowNum = _rows.firstKey();
  1194. if (firstRowNum!=null) {
  1195. int rowIndex = firstRowNum.intValue();
  1196. SXSSFRow row = _rows.get(firstRowNum);
  1197. _writer.writeRow(rowIndex, row);
  1198. _rows.remove(firstRowNum);
  1199. }
  1200. }
  1201. public void changeRowNum(SXSSFRow row, int newRowNum)
  1202. {
  1203. removeRow(row);
  1204. _rows.put(new Integer(newRowNum),row);
  1205. }
  1206. public int getRowNum(SXSSFRow row)
  1207. {
  1208. for(Iterator<Map.Entry<Integer,SXSSFRow>> iter=_rows.entrySet().iterator();iter.hasNext();)
  1209. {
  1210. Map.Entry<Integer,SXSSFRow> entry=iter.next();
  1211. if(entry.getValue()==row)
  1212. return entry.getKey().intValue();
  1213. }
  1214. return -1;
  1215. }
  1216. }