PageRenderTime 49ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/usermodel/HSSFSheet.java

http://github.com/openmicroscopy/bioformats
Java | 1606 lines | 788 code | 231 blank | 587 comment | 115 complexity | 5084cc24e46752bd3911d5d0a983e0a6 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. /*
  38. * HSSFSheet.java
  39. *
  40. * Created on September 30, 2001, 3:40 PM
  41. */
  42. package loci.poi.hssf.usermodel;
  43. import loci.poi.ddf.EscherRecord;
  44. import loci.poi.hssf.model.Sheet;
  45. import loci.poi.hssf.model.Workbook;
  46. import loci.poi.hssf.record.*;
  47. import loci.poi.hssf.util.Region;
  48. import loci.poi.hssf.util.PaneInformation;
  49. import loci.poi.util.POILogFactory;
  50. import loci.poi.util.POILogger;
  51. import java.io.PrintWriter;
  52. import java.util.ArrayList;
  53. import java.util.Iterator;
  54. import java.util.List;
  55. import java.util.TreeMap;
  56. import java.text.AttributedString;
  57. import java.text.NumberFormat;
  58. import java.text.DecimalFormat;
  59. import java.awt.font.TextLayout;
  60. import java.awt.font.FontRenderContext;
  61. import java.awt.font.TextAttribute;
  62. import java.awt.geom.AffineTransform;
  63. /**
  64. * High level representation of a worksheet.
  65. * @author Andrew C. Oliver (acoliver at apache dot org)
  66. * @author Glen Stampoultzis (glens at apache.org)
  67. * @author Libin Roman (romal at vistaportal.com)
  68. * @author Shawn Laubach (slaubach at apache dot org) (Just a little)
  69. * @author Jean-Pierre Paris (jean-pierre.paris at m4x dot org) (Just a little, too)
  70. * @author Yegor Kozlov (yegor at apache.org) (Autosizing columns)
  71. */
  72. public class HSSFSheet
  73. {
  74. private static final int DEBUG = POILogger.DEBUG;
  75. /* Constants for margins */
  76. public static final short LeftMargin = Sheet.LeftMargin;
  77. public static final short RightMargin = Sheet.RightMargin;
  78. public static final short TopMargin = Sheet.TopMargin;
  79. public static final short BottomMargin = Sheet.BottomMargin;
  80. public static final byte PANE_LOWER_RIGHT = (byte)0;
  81. public static final byte PANE_UPPER_RIGHT = (byte)1;
  82. public static final byte PANE_LOWER_LEFT = (byte)2;
  83. public static final byte PANE_UPPER_LEFT = (byte)3;
  84. /**
  85. * Used for compile-time optimization. This is the initial size for the collection of
  86. * rows. It is currently set to 20. If you generate larger sheets you may benefit
  87. * by setting this to a higher number and recompiling a custom edition of HSSFSheet.
  88. */
  89. public final static int INITIAL_CAPACITY = 20;
  90. /**
  91. * reference to the low level Sheet object
  92. */
  93. private Sheet sheet;
  94. private TreeMap rows;
  95. protected Workbook book;
  96. protected HSSFWorkbook workbook;
  97. private int firstrow;
  98. private int lastrow;
  99. private static POILogger log = POILogFactory.getLogger(HSSFSheet.class);
  100. /**
  101. * Creates new HSSFSheet - called by HSSFWorkbook to create a sheet from
  102. * scratch. You should not be calling this from application code (its protected anyhow).
  103. *
  104. * @param workbook - The HSSF Workbook object associated with the sheet.
  105. * @see loci.poi.hssf.usermodel.HSSFWorkbook#createSheet()
  106. */
  107. protected HSSFSheet(HSSFWorkbook workbook)
  108. {
  109. sheet = Sheet.createSheet();
  110. rows = new TreeMap(); // new ArrayList(INITIAL_CAPACITY);
  111. this.workbook = workbook;
  112. this.book = workbook.getWorkbook();
  113. }
  114. /**
  115. * Creates an HSSFSheet representing the given Sheet object. Should only be
  116. * called by HSSFWorkbook when reading in an exisiting file.
  117. *
  118. * @param workbook - The HSSF Workbook object associated with the sheet.
  119. * @param sheet - lowlevel Sheet object this sheet will represent
  120. * @see loci.poi.hssf.usermodel.HSSFWorkbook#createSheet()
  121. */
  122. protected HSSFSheet(HSSFWorkbook workbook, Sheet sheet)
  123. {
  124. this.sheet = sheet;
  125. rows = new TreeMap();
  126. this.workbook = workbook;
  127. this.book = workbook.getWorkbook();
  128. setPropertiesFromSheet(sheet);
  129. }
  130. HSSFSheet cloneSheet(HSSFWorkbook workbook) {
  131. return new HSSFSheet(workbook, sheet.cloneSheet());
  132. }
  133. /**
  134. * used internally to set the properties given a Sheet object
  135. */
  136. private void setPropertiesFromSheet(Sheet sheet)
  137. {
  138. int sloc = sheet.getLoc();
  139. RowRecord row = sheet.getNextRow();
  140. while (row != null)
  141. {
  142. createRowFromRecord(row);
  143. row = sheet.getNextRow();
  144. }
  145. sheet.setLoc(sloc);
  146. CellValueRecordInterface cval = sheet.getNextValueRecord();
  147. long timestart = System.currentTimeMillis();
  148. if (log.check( POILogger.DEBUG ))
  149. log.log(DEBUG, "Time at start of cell creating in HSSF sheet = ",
  150. new Long(timestart));
  151. HSSFRow lastrow = null;
  152. while (cval != null)
  153. {
  154. long cellstart = System.currentTimeMillis();
  155. HSSFRow hrow = lastrow;
  156. if ( ( lastrow == null ) || ( lastrow.getRowNum() != cval.getRow() ) )
  157. {
  158. hrow = getRow( cval.getRow() );
  159. }
  160. if ( hrow != null )
  161. {
  162. lastrow = hrow;
  163. if (log.check( POILogger.DEBUG ))
  164. log.log( DEBUG, "record id = " + Integer.toHexString( ( (Record) cval ).getSid() ) );
  165. hrow.createCellFromRecord( cval );
  166. cval = sheet.getNextValueRecord();
  167. if (log.check( POILogger.DEBUG ))
  168. log.log( DEBUG, "record took ",
  169. new Long( System.currentTimeMillis() - cellstart ) );
  170. }
  171. else
  172. {
  173. cval = null;
  174. }
  175. }
  176. if (log.check( POILogger.DEBUG ))
  177. log.log(DEBUG, "total sheet cell creation took ",
  178. new Long(System.currentTimeMillis() - timestart));
  179. }
  180. /**
  181. * Create a new row within the sheet and return the high level representation
  182. *
  183. * @param rownum row number
  184. * @return High level HSSFRow object representing a row in the sheet
  185. * @see loci.poi.hssf.usermodel.HSSFRow
  186. * @see #removeRow(HSSFRow)
  187. */
  188. public HSSFRow createRow(int rownum)
  189. {
  190. HSSFRow row = new HSSFRow(book, sheet, rownum);
  191. addRow(row, true);
  192. return row;
  193. }
  194. /**
  195. * Used internally to create a high level Row object from a low level row object.
  196. * USed when reading an existing file
  197. * @param row low level record to represent as a high level Row and add to sheet
  198. * @return HSSFRow high level representation
  199. */
  200. private HSSFRow createRowFromRecord(RowRecord row)
  201. {
  202. HSSFRow hrow = new HSSFRow(book, sheet, row);
  203. addRow(hrow, false);
  204. return hrow;
  205. }
  206. /**
  207. * Remove a row from this sheet. All cells contained in the row are removed as well
  208. *
  209. * @param row representing a row to remove.
  210. */
  211. public void removeRow(HSSFRow row)
  212. {
  213. sheet.setLoc(sheet.getDimsLoc());
  214. if (rows.size() > 0)
  215. {
  216. rows.remove(row);
  217. if (row.getRowNum() == getLastRowNum())
  218. {
  219. lastrow = findLastRow(lastrow);
  220. }
  221. if (row.getRowNum() == getFirstRowNum())
  222. {
  223. firstrow = findFirstRow(firstrow);
  224. }
  225. Iterator iter = row.cellIterator();
  226. while (iter.hasNext())
  227. {
  228. HSSFCell cell = (HSSFCell) iter.next();
  229. sheet.removeValueRecord(row.getRowNum(),
  230. cell.getCellValueRecord());
  231. }
  232. sheet.removeRow(row.getRowRecord());
  233. }
  234. }
  235. /**
  236. * used internally to refresh the "last row" when the last row is removed.
  237. */
  238. private int findLastRow(int lastrow)
  239. {
  240. int rownum = lastrow - 1;
  241. HSSFRow r = getRow(rownum);
  242. while (r == null && rownum > 0)
  243. {
  244. r = getRow(--rownum);
  245. }
  246. if (r == null)
  247. return -1;
  248. return rownum;
  249. }
  250. /**
  251. * used internally to refresh the "first row" when the first row is removed.
  252. */
  253. private int findFirstRow(int firstrow)
  254. {
  255. int rownum = firstrow + 1;
  256. HSSFRow r = getRow(rownum);
  257. while (r == null && rownum <= getLastRowNum())
  258. {
  259. r = getRow(++rownum);
  260. }
  261. if (rownum > getLastRowNum())
  262. return -1;
  263. return rownum;
  264. }
  265. /**
  266. * add a row to the sheet
  267. *
  268. * @param addLow whether to add the row to the low level model - false if its already there
  269. */
  270. private void addRow(HSSFRow row, boolean addLow)
  271. {
  272. rows.put(row, row);
  273. if (addLow)
  274. {
  275. sheet.addRow(row.getRowRecord());
  276. }
  277. if (row.getRowNum() > getLastRowNum())
  278. {
  279. lastrow = row.getRowNum();
  280. }
  281. if (row.getRowNum() < getFirstRowNum())
  282. {
  283. firstrow = row.getRowNum();
  284. }
  285. }
  286. /**
  287. * Returns the logical row (not physical) 0-based. If you ask for a row that is not
  288. * defined you get a null. This is to say row 4 represents the fifth row on a sheet.
  289. * @param rownum row to get
  290. * @return HSSFRow representing the rownumber or null if its not defined on the sheet
  291. */
  292. public HSSFRow getRow(int rownum)
  293. {
  294. HSSFRow row = new HSSFRow();
  295. //row.setRowNum((short) rownum);
  296. row.setRowNum( rownum);
  297. return (HSSFRow) rows.get(row);
  298. }
  299. /**
  300. * Returns the number of phsyically defined rows (NOT the number of rows in the sheet)
  301. */
  302. public int getPhysicalNumberOfRows()
  303. {
  304. return rows.size();
  305. }
  306. /**
  307. * gets the first row on the sheet
  308. * @return the number of the first logical row on the sheet
  309. */
  310. public int getFirstRowNum()
  311. {
  312. return firstrow;
  313. }
  314. /**
  315. * gets the last row on the sheet
  316. * @return last row contained n this sheet.
  317. */
  318. public int getLastRowNum()
  319. {
  320. return lastrow;
  321. }
  322. /**
  323. * Get the visibility state for a given column.
  324. * @param column - the column to get (0-based)
  325. * @param hidden - the visiblity state of the column
  326. */
  327. public void setColumnHidden(short column, boolean hidden)
  328. {
  329. sheet.setColumnHidden(column, hidden);
  330. }
  331. /**
  332. * Get the hidden state for a given column.
  333. * @param column - the column to set (0-based)
  334. * @return hidden - the visiblity state of the column
  335. */
  336. public boolean isColumnHidden(short column)
  337. {
  338. return sheet.isColumnHidden(column);
  339. }
  340. /**
  341. * set the width (in units of 1/256th of a character width)
  342. * @param column - the column to set (0-based)
  343. * @param width - the width in units of 1/256th of a character width
  344. */
  345. public void setColumnWidth(short column, short width)
  346. {
  347. sheet.setColumnWidth(column, width);
  348. }
  349. /**
  350. * get the width (in units of 1/256th of a character width )
  351. * @param column - the column to set (0-based)
  352. * @return width - the width in units of 1/256th of a character width
  353. */
  354. public short getColumnWidth(short column)
  355. {
  356. return sheet.getColumnWidth(column);
  357. }
  358. /**
  359. * get the default column width for the sheet (if the columns do not define their own width) in
  360. * characters
  361. * @return default column width
  362. */
  363. public short getDefaultColumnWidth()
  364. {
  365. return sheet.getDefaultColumnWidth();
  366. }
  367. /**
  368. * get the default row height for the sheet (if the rows do not define their own height) in
  369. * twips (1/20 of a point)
  370. * @return default row height
  371. */
  372. public short getDefaultRowHeight()
  373. {
  374. return sheet.getDefaultRowHeight();
  375. }
  376. /**
  377. * get the default row height for the sheet (if the rows do not define their own height) in
  378. * points.
  379. * @return default row height in points
  380. */
  381. public float getDefaultRowHeightInPoints()
  382. {
  383. return (sheet.getDefaultRowHeight() / 20);
  384. }
  385. /**
  386. * set the default column width for the sheet (if the columns do not define their own width) in
  387. * characters
  388. * @param width default column width
  389. */
  390. public void setDefaultColumnWidth(short width)
  391. {
  392. sheet.setDefaultColumnWidth(width);
  393. }
  394. /**
  395. * set the default row height for the sheet (if the rows do not define their own height) in
  396. * twips (1/20 of a point)
  397. * @param height default row height
  398. */
  399. public void setDefaultRowHeight(short height)
  400. {
  401. sheet.setDefaultRowHeight(height);
  402. }
  403. /**
  404. * set the default row height for the sheet (if the rows do not define their own height) in
  405. * points
  406. * @param height default row height
  407. */
  408. public void setDefaultRowHeightInPoints(float height)
  409. {
  410. sheet.setDefaultRowHeight((short) (height * 20));
  411. }
  412. /**
  413. * get whether gridlines are printed.
  414. * @return true if printed
  415. */
  416. public boolean isGridsPrinted()
  417. {
  418. return sheet.isGridsPrinted();
  419. }
  420. /**
  421. * set whether gridlines printed.
  422. * @param value false if not printed.
  423. */
  424. public void setGridsPrinted(boolean value)
  425. {
  426. sheet.setGridsPrinted(value);
  427. }
  428. /**
  429. * adds a merged region of cells (hence those cells form one)
  430. * @param region (rowfrom/colfrom-rowto/colto) to merge
  431. * @return index of this region
  432. */
  433. public int addMergedRegion(Region region)
  434. {
  435. //return sheet.addMergedRegion((short) region.getRowFrom(),
  436. return sheet.addMergedRegion( region.getRowFrom(),
  437. region.getColumnFrom(),
  438. //(short) region.getRowTo(),
  439. region.getRowTo(),
  440. region.getColumnTo());
  441. }
  442. /**
  443. * determines whether the output is vertically centered on the page.
  444. * @param value true to vertically center, false otherwise.
  445. */
  446. public void setVerticallyCenter(boolean value)
  447. {
  448. VCenterRecord record =
  449. (VCenterRecord) sheet.findFirstRecordBySid(VCenterRecord.sid);
  450. record.setVCenter(value);
  451. }
  452. /**
  453. * Determine whether printed output for this sheet will be vertically centered.
  454. */
  455. public boolean getVerticallyCenter(boolean value)
  456. {
  457. VCenterRecord record =
  458. (VCenterRecord) sheet.findFirstRecordBySid(VCenterRecord.sid);
  459. return record.getVCenter();
  460. }
  461. /**
  462. * determines whether the output is horizontally centered on the page.
  463. * @param value true to horizontally center, false otherwise.
  464. */
  465. public void setHorizontallyCenter(boolean value)
  466. {
  467. HCenterRecord record =
  468. (HCenterRecord) sheet.findFirstRecordBySid(HCenterRecord.sid);
  469. record.setHCenter(value);
  470. }
  471. /**
  472. * Determine whether printed output for this sheet will be horizontally centered.
  473. */
  474. public boolean getHorizontallyCenter()
  475. {
  476. HCenterRecord record =
  477. (HCenterRecord) sheet.findFirstRecordBySid(HCenterRecord.sid);
  478. return record.getHCenter();
  479. }
  480. /**
  481. * removes a merged region of cells (hence letting them free)
  482. * @param index of the region to unmerge
  483. */
  484. public void removeMergedRegion(int index)
  485. {
  486. sheet.removeMergedRegion(index);
  487. }
  488. /**
  489. * returns the number of merged regions
  490. * @return number of merged regions
  491. */
  492. public int getNumMergedRegions()
  493. {
  494. return sheet.getNumMergedRegions();
  495. }
  496. /**
  497. * gets the region at a particular index
  498. * @param index of the region to fetch
  499. * @return the merged region (simple eh?)
  500. */
  501. public Region getMergedRegionAt(int index)
  502. {
  503. return new Region(sheet.getMergedRegionAt(index));
  504. }
  505. /**
  506. * @return an iterator of the PHYSICAL rows. Meaning the 3rd element may not
  507. * be the third row if say for instance the second row is undefined.
  508. */
  509. public Iterator rowIterator()
  510. {
  511. return rows.values().iterator();
  512. }
  513. /**
  514. * used internally in the API to get the low level Sheet record represented by this
  515. * Object.
  516. * @return Sheet - low level representation of this HSSFSheet.
  517. */
  518. protected Sheet getSheet()
  519. {
  520. return sheet;
  521. }
  522. /**
  523. * whether alternate expression evaluation is on
  524. * @param b alternative expression evaluation or not
  525. */
  526. public void setAlternativeExpression(boolean b)
  527. {
  528. WSBoolRecord record =
  529. (WSBoolRecord) sheet.findFirstRecordBySid(WSBoolRecord.sid);
  530. record.setAlternateExpression(b);
  531. }
  532. /**
  533. * whether alternative formula entry is on
  534. * @param b alternative formulas or not
  535. */
  536. public void setAlternativeFormula(boolean b)
  537. {
  538. WSBoolRecord record =
  539. (WSBoolRecord) sheet.findFirstRecordBySid(WSBoolRecord.sid);
  540. record.setAlternateFormula(b);
  541. }
  542. /**
  543. * show automatic page breaks or not
  544. * @param b whether to show auto page breaks
  545. */
  546. public void setAutobreaks(boolean b)
  547. {
  548. WSBoolRecord record =
  549. (WSBoolRecord) sheet.findFirstRecordBySid(WSBoolRecord.sid);
  550. record.setAutobreaks(b);
  551. }
  552. /**
  553. * set whether sheet is a dialog sheet or not
  554. * @param b isDialog or not
  555. */
  556. public void setDialog(boolean b)
  557. {
  558. WSBoolRecord record =
  559. (WSBoolRecord) sheet.findFirstRecordBySid(WSBoolRecord.sid);
  560. record.setDialog(b);
  561. }
  562. /**
  563. * set whether to display the guts or not
  564. *
  565. * @param b guts or no guts (or glory)
  566. */
  567. public void setDisplayGuts(boolean b)
  568. {
  569. WSBoolRecord record =
  570. (WSBoolRecord) sheet.findFirstRecordBySid(WSBoolRecord.sid);
  571. record.setDisplayGuts(b);
  572. }
  573. /**
  574. * fit to page option is on
  575. * @param b fit or not
  576. */
  577. public void setFitToPage(boolean b)
  578. {
  579. WSBoolRecord record =
  580. (WSBoolRecord) sheet.findFirstRecordBySid(WSBoolRecord.sid);
  581. record.setFitToPage(b);
  582. }
  583. /**
  584. * set if row summaries appear below detail in the outline
  585. * @param b below or not
  586. */
  587. public void setRowSumsBelow(boolean b)
  588. {
  589. WSBoolRecord record =
  590. (WSBoolRecord) sheet.findFirstRecordBySid(WSBoolRecord.sid);
  591. record.setRowSumsBelow(b);
  592. }
  593. /**
  594. * set if col summaries appear right of the detail in the outline
  595. * @param b right or not
  596. */
  597. public void setRowSumsRight(boolean b)
  598. {
  599. WSBoolRecord record =
  600. (WSBoolRecord) sheet.findFirstRecordBySid(WSBoolRecord.sid);
  601. record.setRowSumsRight(b);
  602. }
  603. /**
  604. * whether alternate expression evaluation is on
  605. * @return alternative expression evaluation or not
  606. */
  607. public boolean getAlternateExpression()
  608. {
  609. return ((WSBoolRecord) sheet.findFirstRecordBySid(WSBoolRecord.sid))
  610. .getAlternateExpression();
  611. }
  612. /**
  613. * whether alternative formula entry is on
  614. * @return alternative formulas or not
  615. */
  616. public boolean getAlternateFormula()
  617. {
  618. return ((WSBoolRecord) sheet.findFirstRecordBySid(WSBoolRecord.sid))
  619. .getAlternateFormula();
  620. }
  621. /**
  622. * show automatic page breaks or not
  623. * @return whether to show auto page breaks
  624. */
  625. public boolean getAutobreaks()
  626. {
  627. return ((WSBoolRecord) sheet.findFirstRecordBySid(WSBoolRecord.sid))
  628. .getAutobreaks();
  629. }
  630. /**
  631. * get whether sheet is a dialog sheet or not
  632. * @return isDialog or not
  633. */
  634. public boolean getDialog()
  635. {
  636. return ((WSBoolRecord) sheet.findFirstRecordBySid(WSBoolRecord.sid))
  637. .getDialog();
  638. }
  639. /**
  640. * get whether to display the guts or not
  641. *
  642. * @return guts or no guts (or glory)
  643. */
  644. public boolean getDisplayGuts()
  645. {
  646. return ((WSBoolRecord) sheet.findFirstRecordBySid(WSBoolRecord.sid))
  647. .getDisplayGuts();
  648. }
  649. /**
  650. * fit to page option is on
  651. * @return fit or not
  652. */
  653. public boolean getFitToPage()
  654. {
  655. return ((WSBoolRecord) sheet.findFirstRecordBySid(WSBoolRecord.sid))
  656. .getFitToPage();
  657. }
  658. /**
  659. * get if row summaries appear below detail in the outline
  660. * @return below or not
  661. */
  662. public boolean getRowSumsBelow()
  663. {
  664. return ((WSBoolRecord) sheet.findFirstRecordBySid(WSBoolRecord.sid))
  665. .getRowSumsBelow();
  666. }
  667. /**
  668. * get if col summaries appear right of the detail in the outline
  669. * @return right or not
  670. */
  671. public boolean getRowSumsRight()
  672. {
  673. return ((WSBoolRecord) sheet.findFirstRecordBySid(WSBoolRecord.sid))
  674. .getRowSumsRight();
  675. }
  676. /**
  677. * Returns whether gridlines are printed.
  678. * @return Gridlines are printed
  679. */
  680. public boolean isPrintGridlines() {
  681. return getSheet().getPrintGridlines().getPrintGridlines();
  682. }
  683. /**
  684. * Turns on or off the printing of gridlines.
  685. * @param newPrintGridlines boolean to turn on or off the printing of
  686. * gridlines
  687. */
  688. public void setPrintGridlines( boolean newPrintGridlines )
  689. {
  690. getSheet().getPrintGridlines().setPrintGridlines( newPrintGridlines );
  691. }
  692. /**
  693. * Gets the print setup object.
  694. * @return The user model for the print setup object.
  695. */
  696. public HSSFPrintSetup getPrintSetup()
  697. {
  698. return new HSSFPrintSetup( getSheet().getPrintSetup() );
  699. }
  700. /**
  701. * Gets the user model for the document header.
  702. * @return The Document header.
  703. */
  704. public HSSFHeader getHeader()
  705. {
  706. return new HSSFHeader( getSheet().getHeader() );
  707. }
  708. /**
  709. * Gets the user model for the document footer.
  710. * @return The Document footer.
  711. */
  712. public HSSFFooter getFooter()
  713. {
  714. return new HSSFFooter( getSheet().getFooter() );
  715. }
  716. /**
  717. * Sets whether sheet is selected.
  718. * @param sel Whether to select the sheet or deselect the sheet.
  719. */
  720. public void setSelected( boolean sel )
  721. {
  722. getSheet().setSelected( sel );
  723. }
  724. /**
  725. * Gets the size of the margin in inches.
  726. * @param margin which margin to get
  727. * @return the size of the margin
  728. */
  729. public double getMargin( short margin )
  730. {
  731. return getSheet().getMargin( margin );
  732. }
  733. /**
  734. * Sets the size of the margin in inches.
  735. * @param margin which margin to get
  736. * @param size the size of the margin
  737. */
  738. public void setMargin( short margin, double size )
  739. {
  740. getSheet().setMargin( margin, size );
  741. }
  742. /**
  743. * Answer whether protection is enabled or disabled
  744. * @return true => protection enabled; false => protection disabled
  745. */
  746. public boolean getProtect() {
  747. return getSheet().isProtected()[0];
  748. }
  749. /**
  750. * @return hashed password
  751. */
  752. public short getPassword() {
  753. return getSheet().getPassword().getPassword();
  754. }
  755. /**
  756. * Answer whether object protection is enabled or disabled
  757. * @return true => protection enabled; false => protection disabled
  758. */
  759. public boolean getObjectProtect() {
  760. return getSheet().isProtected()[1];
  761. }
  762. /**
  763. * Answer whether scenario protection is enabled or disabled
  764. * @return true => protection enabled; false => protection disabled
  765. */
  766. public boolean getScenarioProtect() {
  767. return getSheet().isProtected()[2];
  768. }
  769. /**
  770. * Sets the protection on enabled or disabled
  771. * @param protect true => protection enabled; false => protection disabled
  772. * @deprecated use protectSheet(String, boolean, boolean)
  773. */
  774. public void setProtect(boolean protect) {
  775. getSheet().getProtect().setProtect(protect);
  776. }
  777. /**
  778. * Sets the protection enabled as well as the password
  779. * @param password to set for protection
  780. */
  781. public void protectSheet(String password) {
  782. getSheet().protectSheet(password, true, true); //protect objs&scenarios(normal)
  783. }
  784. /**
  785. * Sets the zoom magnication for the sheet. The zoom is expressed as a
  786. * fraction. For example to express a zoom of 75% use 3 for the numerator
  787. * and 4 for the denominator.
  788. *
  789. * @param numerator The numerator for the zoom magnification.
  790. * @param denominator The denominator for the zoom magnification.
  791. */
  792. public void setZoom( int numerator, int denominator)
  793. {
  794. if (numerator < 1 || numerator > 65535)
  795. throw new IllegalArgumentException("Numerator must be greater than 1 and less than 65536");
  796. if (denominator < 1 || denominator > 65535)
  797. throw new IllegalArgumentException("Denominator must be greater than 1 and less than 65536");
  798. SCLRecord sclRecord = new SCLRecord();
  799. sclRecord.setNumerator((short)numerator);
  800. sclRecord.setDenominator((short)denominator);
  801. getSheet().setSCLRecord(sclRecord);
  802. }
  803. /**
  804. * The top row in the visible view when the sheet is
  805. * first viewed after opening it in a viewer
  806. * @return short indicating the rownum (0 based) of the top row
  807. */
  808. public short getTopRow()
  809. {
  810. return sheet.getTopRow();
  811. }
  812. /**
  813. * The left col in the visible view when the sheet is
  814. * first viewed after opening it in a viewer
  815. * @return short indicating the rownum (0 based) of the top row
  816. */
  817. public short getLeftCol()
  818. {
  819. return sheet.getLeftCol();
  820. }
  821. /**
  822. * Sets desktop window pane display area, when the
  823. * file is first opened in a viewer.
  824. * @param toprow the top row to show in desktop window pane
  825. * @param leftcol the left column to show in desktop window pane
  826. */
  827. public void showInPane(short toprow, short leftcol){
  828. this.sheet.setTopRow((short)toprow);
  829. this.sheet.setLeftCol((short)leftcol);
  830. }
  831. /**
  832. * Shifts the merged regions left or right depending on mode
  833. * <p>
  834. * TODO: MODE , this is only row specific
  835. * @param startRow
  836. * @param endRow
  837. * @param n
  838. * @param isRow
  839. */
  840. protected void shiftMerged(int startRow, int endRow, int n, boolean isRow) {
  841. List shiftedRegions = new ArrayList();
  842. //move merged regions completely if they fall within the new region boundaries when they are shifted
  843. for (int i = 0; i < this.getNumMergedRegions(); i++) {
  844. Region merged = this.getMergedRegionAt(i);
  845. boolean inStart = (merged.getRowFrom() >= startRow || merged.getRowTo() >= startRow);
  846. boolean inEnd = (merged.getRowTo() <= endRow || merged.getRowFrom() <= endRow);
  847. //dont check if it's not within the shifted area
  848. if (! (inStart && inEnd)) continue;
  849. //only shift if the region outside the shifted rows is not merged too
  850. if (!merged.contains(startRow-1, (short)0) && !merged.contains(endRow+1, (short)0)){
  851. merged.setRowFrom(merged.getRowFrom()+n);
  852. merged.setRowTo(merged.getRowTo()+n);
  853. //have to remove/add it back
  854. shiftedRegions.add(merged);
  855. this.removeMergedRegion(i);
  856. i = i -1; // we have to back up now since we removed one
  857. }
  858. }
  859. //readd so it doesn't get shifted again
  860. Iterator iterator = shiftedRegions.iterator();
  861. while (iterator.hasNext()) {
  862. Region region = (Region)iterator.next();
  863. this.addMergedRegion(region);
  864. }
  865. }
  866. /**
  867. * Shifts rows between startRow and endRow n number of rows.
  868. * If you use a negative number, it will shift rows up.
  869. * Code ensures that rows don't wrap around.
  870. *
  871. * Calls shiftRows(startRow, endRow, n, false, false);
  872. *
  873. * <p>
  874. * Additionally shifts merged regions that are completely defined in these
  875. * rows (ie. merged 2 cells on a row to be shifted).
  876. * @param startRow the row to start shifting
  877. * @param endRow the row to end shifting
  878. * @param n the number of rows to shift
  879. */
  880. public void shiftRows( int startRow, int endRow, int n ) {
  881. shiftRows(startRow, endRow, n, false, false);
  882. }
  883. /**
  884. * Shifts rows between startRow and endRow n number of rows.
  885. * If you use a negative number, it will shift rows up.
  886. * Code ensures that rows don't wrap around
  887. *
  888. * <p>
  889. * Additionally shifts merged regions that are completely defined in these
  890. * rows (ie. merged 2 cells on a row to be shifted).
  891. * <p>
  892. * TODO Might want to add bounds checking here
  893. * @param startRow the row to start shifting
  894. * @param endRow the row to end shifting
  895. * @param n the number of rows to shift
  896. * @param copyRowHeight whether to copy the row height during the shift
  897. * @param resetOriginalRowHeight whether to set the original row's height to the default
  898. */
  899. public void shiftRows( int startRow, int endRow, int n, boolean copyRowHeight, boolean resetOriginalRowHeight)
  900. {
  901. int s, e, inc;
  902. if ( n < 0 )
  903. {
  904. s = startRow;
  905. e = endRow;
  906. inc = 1;
  907. }
  908. else
  909. {
  910. s = endRow;
  911. e = startRow;
  912. inc = -1;
  913. }
  914. shiftMerged(startRow, endRow, n, true);
  915. sheet.shiftRowBreaks(startRow, endRow, n);
  916. for ( int rowNum = s; rowNum >= startRow && rowNum <= endRow && rowNum >= 0 && rowNum < 65536; rowNum += inc )
  917. {
  918. HSSFRow row = getRow( rowNum );
  919. HSSFRow row2Replace = getRow( rowNum + n );
  920. if ( row2Replace == null )
  921. row2Replace = createRow( rowNum + n );
  922. HSSFCell cell;
  923. // Removes the cells before over writting them.
  924. for ( short col = row2Replace.getFirstCellNum(); col <= row2Replace.getLastCellNum(); col++ )
  925. {
  926. cell = row2Replace.getCell( col );
  927. if ( cell != null )
  928. row2Replace.removeCell( cell );
  929. }
  930. if (row == null) continue; // Nothing to do for this row
  931. else {
  932. if (copyRowHeight) {
  933. row2Replace.setHeight(row.getHeight());
  934. }
  935. if (resetOriginalRowHeight) {
  936. row.setHeight((short)0xff);
  937. }
  938. }
  939. for ( short col = row.getFirstCellNum(); col <= row.getLastCellNum(); col++ )
  940. {
  941. cell = row.getCell( col );
  942. if ( cell != null )
  943. {
  944. row.removeCell( cell );
  945. CellValueRecordInterface cellRecord = cell.getCellValueRecord();
  946. cellRecord.setRow( rowNum + n );
  947. row2Replace.createCellFromRecord( cellRecord );
  948. sheet.addValueRecord( rowNum + n, cellRecord );
  949. }
  950. }
  951. }
  952. if ( endRow == lastrow || endRow + n > lastrow ) lastrow = Math.min( endRow + n, 65535 );
  953. if ( startRow == firstrow || startRow + n < firstrow ) firstrow = Math.max( startRow + n, 0 );
  954. }
  955. protected void insertChartRecords( List records )
  956. {
  957. int window2Loc = sheet.findFirstRecordLocBySid( WindowTwoRecord.sid );
  958. sheet.getRecords().addAll( window2Loc, records );
  959. }
  960. /**
  961. * Creates a split (freezepane). Any existing freezepane or split pane is overwritten.
  962. * @param colSplit Horizonatal position of split.
  963. * @param rowSplit Vertical position of split.
  964. * @param topRow Top row visible in bottom pane
  965. * @param leftmostColumn Left column visible in right pane.
  966. */
  967. public void createFreezePane(int colSplit, int rowSplit, int leftmostColumn, int topRow )
  968. {
  969. if (colSplit < 0 || colSplit > 255) throw new IllegalArgumentException("Column must be between 0 and 255");
  970. if (rowSplit < 0 || rowSplit > 65535) throw new IllegalArgumentException("Row must be between 0 and 65535");
  971. if (leftmostColumn < colSplit) throw new IllegalArgumentException("leftmostColumn parameter must not be less than colSplit parameter");
  972. if (topRow < rowSplit) throw new IllegalArgumentException("topRow parameter must not be less than leftmostColumn parameter");
  973. getSheet().createFreezePane( colSplit, rowSplit, topRow, leftmostColumn );
  974. }
  975. /**
  976. * Creates a split (freezepane). Any existing freezepane or split pane is overwritten.
  977. * @param colSplit Horizonatal position of split.
  978. * @param rowSplit Vertical position of split.
  979. */
  980. public void createFreezePane( int colSplit, int rowSplit )
  981. {
  982. createFreezePane( colSplit, rowSplit, colSplit, rowSplit );
  983. }
  984. /**
  985. * Creates a split pane. Any existing freezepane or split pane is overwritten.
  986. * @param xSplitPos Horizonatal position of split (in 1/20th of a point).
  987. * @param ySplitPos Vertical position of split (in 1/20th of a point).
  988. * @param topRow Top row visible in bottom pane
  989. * @param leftmostColumn Left column visible in right pane.
  990. * @param activePane Active pane. One of: PANE_LOWER_RIGHT,
  991. * PANE_UPPER_RIGHT, PANE_LOWER_LEFT, PANE_UPPER_LEFT
  992. * @see #PANE_LOWER_LEFT
  993. * @see #PANE_LOWER_RIGHT
  994. * @see #PANE_UPPER_LEFT
  995. * @see #PANE_UPPER_RIGHT
  996. */
  997. public void createSplitPane(int xSplitPos, int ySplitPos, int leftmostColumn, int topRow, int activePane )
  998. {
  999. getSheet().createSplitPane( xSplitPos, ySplitPos, topRow, leftmostColumn, activePane );
  1000. }
  1001. /**
  1002. * Returns the information regarding the currently configured pane (split or freeze).
  1003. * @return null if no pane configured, or the pane information.
  1004. */
  1005. public PaneInformation getPaneInformation() {
  1006. return getSheet().getPaneInformation();
  1007. }
  1008. /**
  1009. * Sets whether the gridlines are shown in a viewer.
  1010. * @param show whether to show gridlines or not
  1011. */
  1012. public void setDisplayGridlines(boolean show) {
  1013. sheet.setDisplayGridlines(show);
  1014. }
  1015. /**
  1016. * Returns if gridlines are displayed.
  1017. * @return whether gridlines are displayed
  1018. */
  1019. public boolean isDisplayGridlines() {
  1020. return sheet.isDisplayGridlines();
  1021. }
  1022. /**
  1023. * Sets whether the formulas are shown in a viewer.
  1024. * @param show whether to show formulas or not
  1025. */
  1026. public void setDisplayFormulas(boolean show) {
  1027. sheet.setDisplayFormulas(show);
  1028. }
  1029. /**
  1030. * Returns if formulas are displayed.
  1031. * @return whether formulas are displayed
  1032. */
  1033. public boolean isDisplayFormulas() {
  1034. return sheet.isDisplayFormulas();
  1035. }
  1036. /**
  1037. * Sets whether the RowColHeadings are shown in a viewer.
  1038. * @param show whether to show RowColHeadings or not
  1039. */
  1040. public void setDisplayRowColHeadings(boolean show) {
  1041. sheet.setDisplayRowColHeadings(show);
  1042. }
  1043. /**
  1044. * Returns if RowColHeadings are displayed.
  1045. * @return whether RowColHeadings are displayed
  1046. */
  1047. public boolean isDisplayRowColHeadings() {
  1048. return sheet.isDisplayRowColHeadings();
  1049. }
  1050. /**
  1051. * Sets a page break at the indicated row
  1052. * @param row FIXME: Document this!
  1053. */
  1054. public void setRowBreak(int row) {
  1055. validateRow(row);
  1056. sheet.setRowBreak(row, (short)0, (short)255);
  1057. }
  1058. /**
  1059. * Determines if there is a page break at the indicated row
  1060. * @param row FIXME: Document this!
  1061. * @return FIXME: Document this!
  1062. */
  1063. public boolean isRowBroken(int row) {
  1064. return sheet.isRowBroken(row);
  1065. }
  1066. /**
  1067. * Removes the page break at the indicated row
  1068. * @param row
  1069. */
  1070. public void removeRowBreak(int row) {
  1071. sheet.removeRowBreak(row);
  1072. }
  1073. /**
  1074. * Retrieves all the horizontal page breaks
  1075. * @return all the horizontal page breaks, or null if there are no row page breaks
  1076. */
  1077. public int[] getRowBreaks(){
  1078. //we can probably cache this information, but this should be a sparsely used function
  1079. int count = sheet.getNumRowBreaks();
  1080. if (count > 0) {
  1081. int[] returnValue = new int[count];
  1082. Iterator iterator = sheet.getRowBreaks();
  1083. int i = 0;
  1084. while (iterator.hasNext()) {
  1085. PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.next();
  1086. returnValue[i++] = (int)breakItem.main;
  1087. }
  1088. return returnValue;
  1089. }
  1090. return null;
  1091. }
  1092. /**
  1093. * Retrieves all the vertical page breaks
  1094. * @return all the vertical page breaks, or null if there are no column page breaks
  1095. */
  1096. public short[] getColumnBreaks(){
  1097. //we can probably cache this information, but this should be a sparsely used function
  1098. int count = sheet.getNumColumnBreaks();
  1099. if (count > 0) {
  1100. short[] returnValue = new short[count];
  1101. Iterator iterator = sheet.getColumnBreaks();
  1102. int i = 0;
  1103. while (iterator.hasNext()) {
  1104. PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.next();
  1105. returnValue[i++] = breakItem.main;
  1106. }
  1107. return returnValue;
  1108. }
  1109. return null;
  1110. }
  1111. /**
  1112. * Sets a page break at the indicated column
  1113. * @param column
  1114. */
  1115. public void setColumnBreak(short column) {
  1116. validateColumn(column);
  1117. sheet.setColumnBreak(column, (short)0, (short)65535);
  1118. }
  1119. /**
  1120. * Determines if there is a page break at the indicated column
  1121. * @param column FIXME: Document this!
  1122. * @return FIXME: Document this!
  1123. */
  1124. public boolean isColumnBroken(short column) {
  1125. return sheet.isColumnBroken(column);
  1126. }
  1127. /**
  1128. * Removes a page break at the indicated column
  1129. * @param column
  1130. */
  1131. public void removeColumnBreak(short column) {
  1132. sheet.removeColumnBreak(column);
  1133. }
  1134. /**
  1135. * Runs a bounds check for row numbers
  1136. * @param row
  1137. */
  1138. protected void validateRow(int row) {
  1139. if (row > 65535) throw new IllegalArgumentException("Maximum row number is 65535");
  1140. if (row < 0) throw new IllegalArgumentException("Minumum row number is 0");
  1141. }
  1142. /**
  1143. * Runs a bounds check for column numbers
  1144. * @param column
  1145. */
  1146. protected void validateColumn(short column) {
  1147. if (column > 255) throw new IllegalArgumentException("Maximum column number is 255");
  1148. if (column < 0) throw new IllegalArgumentException("Minimum column number is 0");
  1149. }
  1150. /**
  1151. * Aggregates the drawing records and dumps the escher record hierarchy
  1152. * to the standard output.
  1153. */
  1154. public void dumpDrawingRecords(boolean fat)
  1155. {
  1156. sheet.aggregateDrawingRecords(book.getDrawingManager());
  1157. EscherAggregate r = (EscherAggregate) getSheet().findFirstRecordBySid(EscherAggregate.sid);
  1158. List escherRecords = r.getEscherRecords();
  1159. PrintWriter w = new PrintWriter(System.out);
  1160. for ( Iterator iterator = escherRecords.iterator(); iterator.hasNext(); )
  1161. {
  1162. EscherRecord escherRecord = (EscherRecord) iterator.next();
  1163. if (fat)
  1164. System.out.println(escherRecord.toString());
  1165. else
  1166. escherRecord.display(w, 0);
  1167. }
  1168. w.flush();
  1169. }
  1170. /**
  1171. * Creates the toplevel drawing patriarch. This will have the effect of
  1172. * removing any existing drawings on this sheet.
  1173. *
  1174. * @return The new patriarch.
  1175. */
  1176. public HSSFPatriarch createDrawingPatriarch()
  1177. {
  1178. // Create the drawing group if it doesn't already exist.
  1179. book.createDrawingGroup();
  1180. sheet.aggregateDrawingRecords(book.getDrawingManager());
  1181. EscherAggregate agg = (EscherAggregate) sheet.findFirstRecordBySid(EscherAggregate.sid);
  1182. HSSFPatriarch patriarch = new HSSFPatriarch(this);
  1183. agg.clear(); // Initially the behaviour will be to clear out any existing shapes in the sheet when
  1184. // creating a new patriarch.
  1185. agg.setPatriarch(patriarch);
  1186. return patriarch;
  1187. }
  1188. /**
  1189. * Expands or collapses a column group.
  1190. *
  1191. * @param columnNumber One of the columns in the group.
  1192. * @param collapsed true = collapse group, false = expand group.
  1193. */
  1194. public void setColumnGroupCollapsed( short columnNumber, boolean collapsed )
  1195. {
  1196. sheet.setColumnGroupCollapsed( columnNumber, collapsed );
  1197. }
  1198. /**
  1199. * Create an outline for the provided column range.
  1200. *
  1201. * @param fromColumn beginning of the column range.
  1202. * @param toColumn end of the column range.
  1203. */
  1204. public void groupColumn(short fromColumn, short toColumn)
  1205. {
  1206. sheet.groupColumnRange( fromColumn, toColumn, true );
  1207. }
  1208. public void ungroupColumn( short fromColumn, short toColumn )
  1209. {
  1210. sheet.groupColumnRange( fromColumn, toColumn, false );
  1211. }
  1212. public void groupRow(int fromRow, int toRow)
  1213. {
  1214. sheet.groupRowRange( fromRow, toRow, true );
  1215. }
  1216. public void ungroupRow(int fromRow, int toRow)
  1217. {
  1218. sheet.groupRowRange( fromRow, toRow, false );
  1219. }
  1220. public void setRowGroupCollapsed( int row, boolean collapse )
  1221. {
  1222. sheet.setRowGroupCollapsed( row, collapse );
  1223. }
  1224. /**
  1225. * Sets the default column style for a given column. POI will only apply this style to new cells added to the sheet.
  1226. *
  1227. * @param column the column index
  1228. * @param style the style to set
  1229. */
  1230. public void setDefaultColumnStyle(short column, HSSFCellStyle style) {
  1231. sheet.setColumn(column, new Short(style.getIndex()), null, null, null, null);
  1232. }
  1233. /**
  1234. * Adjusts the column width to fit the contents.
  1235. *
  1236. * This process can be relatively slow on large sheets, so this should
  1237. * normally only be called once per column, at the end of your
  1238. * processing.
  1239. *
  1240. * @param column the column index
  1241. */
  1242. public void autoSizeColumn(short column) {
  1243. AttributedString str;
  1244. TextLayout layout;
  1245. /**
  1246. * Excel measures columns in units of 1/256th of a character width
  1247. * but the docs say nothing about what particular character is used.
  1248. * '0' looks to be a good choice.
  1249. */
  1250. char defaultChar = '0';
  1251. /**
  1252. * This is the multiple that the font height is scaled by when determining the
  1253. * boundary of rotated text.
  1254. */
  1255. double fontHeightMultiple = 2.0;
  1256. FontRenderContext frc = new FontRenderContext(null, true, true);
  1257. HSSFWorkbook wb = new HSSFWorkbook(book);
  1258. HSSFFont defaultFont = wb.getFontAt((short) 0);
  1259. str = new AttributedString("" + defaultChar);
  1260. copyAttributes(defaultFont, str, 0, 1);
  1261. layout = new TextLayout(str.getIterator(), frc);
  1262. int defaultCharWidth = (int)layout.getAdvance();
  1263. double width = -1;
  1264. for (Iterator it = rowIterator(); it.hasNext();) {
  1265. HSSFRow row = (HSSFRow) it.next();
  1266. HSSFCell cell = row.getCell(column);
  1267. if (cell == null) continue;
  1268. HSSFCellStyle style = cell.getCellStyle();
  1269. HSSFFont font = wb.getFontAt(style.getFontIndex());
  1270. if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
  1271. HSSFRichTextString rt = cell.getRichStringCellValue();
  1272. String[] lines = rt.getString().split("\\n");
  1273. for (int i = 0; i < lines.length; i++) {
  1274. String txt = lines[i] + defaultChar;
  1275. str = new AttributedString(txt);
  1276. copyAttributes(font, str, 0, txt.length());
  1277. if (rt.numFormattingRuns() > 0) {
  1278. for (int j = 0; j < lines[i].length(); j++) {
  1279. int idx = rt.getFontAtIndex(j);
  1280. if (idx != 0) {
  1281. HSSFFont fnt = wb.getFontAt((short) idx);
  1282. copyAttributes(fnt, str, j, j + 1);
  1283. }
  1284. }
  1285. }
  1286. layout = new TextLayout(str.getIterator(), frc);
  1287. if(style.getRotation() != 0){
  1288. /*
  1289. * Transform the text using a scale so that it's height is increased by a multiple of the leading,
  1290. * and then rotate the text before computing the bounds. The scale results in some whitespace around
  1291. * the unrotated top and bottom of the text that normally wouldn't be present if unscaled, but
  1292. * is added by the standard Excel autosize.
  1293. */
  1294. AffineTransform trans = new AffineTransform();
  1295. trans.concatenate(AffineTransform.getRotateInstance(style.getRotation()*2.0*Math.PI/360.0));
  1296. trans.concatenate(
  1297. AffineTransform.getScaleInstance(1, fontHeightMultiple)
  1298. );
  1299. width = Math.max(width, layout.getOutline(trans).getBounds().getWidth() / defaultCharWidth);
  1300. } else {
  1301. width = Math.max(width, layout.getBounds().getWidth() / defaultCharWidth);
  1302. }
  1303. }
  1304. } else {
  1305. String sval = null;
  1306. if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
  1307. HSSFDataFormat dataformat = wb.createDataFormat();
  1308. short idx = style.getDataFormat();
  1309. String format = dataformat.getFormat(idx).replaceAll("\"", "");
  1310. double value = cell.getNumericCellValue();
  1311. try {
  1312. NumberFormat fmt;
  1313. if ("General".equals(format))
  1314. sval = "" + value;
  1315. else
  1316. {
  1317. fmt = new DecimalFormat(format);
  1318. sval = fmt.format(value);
  1319. }
  1320. } catch (Exception e) {
  1321. sval = "" + value;
  1322. }
  1323. } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
  1324. sval = String.valueOf(cell.getBooleanCellValue());
  1325. }
  1326. String txt = sval + defaultChar;
  1327. str = new AttributedString(txt);
  1328. copyAttributes(font, str, 0, txt.length());
  1329. layout = new TextLayout(str.getIterator(), frc);
  1330. if(style.getRotation() != 0){
  1331. /*
  1332. * Transform the text using a scale so that it's height is increased by a multiple of the leading,
  1333. * and then rotate the text before computing the bounds. The scale results in some whitespace around
  1334. * the unrotated top and bottom of the text that normally wouldn't be present if unscaled, but
  1335. * is added by the standard Excel autosize.
  1336. */
  1337. AffineTransform trans = new AffineTransform();
  1338. trans.concatenate(AffineTransform.getRotateInstance(style.getRotation()*2.0*Math.PI/360.0));
  1339. trans.concatenate(
  1340. AffineTransform.getScaleInstance(1, fontHeightMultiple)
  1341. );
  1342. width = Math.max(width, layout.getOutline(trans).getBounds().getWidth() / defaultCharWidth);
  1343. } else {
  1344. width = Math.max(width, layout.getBounds().getWidth() / defaultCharWidth);
  1345. }
  1346. }
  1347. if (width != -1) {
  1348. if (width > Short.MAX_VALUE) { //width can be bigger that Short.MAX_VALUE!
  1349. width = Short.MAX_VALUE;
  1350. }
  1351. sheet.setColumnWidth(column, (short) (width * 256));
  1352. }
  1353. }
  1354. }
  1355. /**
  1356. * Copy text attributes from the supplied HSSFFont to Java2D AttributedString
  1357. */
  1358. private void copyAttributes(HSSFFont font, AttributedString str, int startIdx, int endIdx){
  1359. str.addAttribute(TextAttribute.FAMILY, font.getFontName(), startIdx, endIdx);
  1360. str.addAttribute(TextAttribute.SIZE, new Float(font.getFontHeightInPoints()));
  1361. if (font.getBoldweight() == HSSFFont.BOLDWEIGHT_BOLD) str.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, startIdx, endIdx);
  1362. i

Large files files are truncated, but you can click here to view the full file