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.

HSSFCellStyle.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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.hssf.usermodel;
  16. import java.util.List;
  17. import java.util.Objects;
  18. import org.apache.poi.common.Duplicatable;
  19. import org.apache.poi.hssf.model.InternalWorkbook;
  20. import org.apache.poi.hssf.record.ExtendedFormatRecord;
  21. import org.apache.poi.hssf.record.FontRecord;
  22. import org.apache.poi.hssf.record.FormatRecord;
  23. import org.apache.poi.hssf.record.StyleRecord;
  24. import org.apache.poi.hssf.util.HSSFColor;
  25. import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
  26. import org.apache.poi.ss.usermodel.BorderStyle;
  27. import org.apache.poi.ss.usermodel.CellStyle;
  28. import org.apache.poi.ss.usermodel.FillPatternType;
  29. import org.apache.poi.ss.usermodel.Font;
  30. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  31. import org.apache.poi.ss.usermodel.VerticalAlignment;
  32. /**
  33. * High level representation of the style of a cell in a sheet of a workbook.
  34. *
  35. * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createCellStyle()
  36. * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(int)
  37. * @see org.apache.poi.hssf.usermodel.HSSFCell#setCellStyle(HSSFCellStyle)
  38. */
  39. public final class HSSFCellStyle implements CellStyle, Duplicatable {
  40. private final ExtendedFormatRecord _format;
  41. private final short _index;
  42. private final InternalWorkbook _workbook;
  43. /** Creates new HSSFCellStyle why would you want to do this?? */
  44. protected HSSFCellStyle(short index, ExtendedFormatRecord rec, HSSFWorkbook workbook)
  45. {
  46. this(index, rec, workbook.getWorkbook());
  47. }
  48. protected HSSFCellStyle(short index, ExtendedFormatRecord rec, InternalWorkbook workbook)
  49. {
  50. _workbook = workbook;
  51. _index = index;
  52. _format = rec;
  53. }
  54. protected HSSFCellStyle(HSSFCellStyle other) {
  55. _workbook = other._workbook;
  56. _index = other._index;
  57. _format = other._format;
  58. }
  59. /**
  60. * get the index within the HSSFWorkbook (sequence within the collection of ExtnededFormat objects)
  61. * @return unique index number of the underlying record this style represents (probably you don't care
  62. * unless you're comparing which one is which)
  63. */
  64. @Override
  65. public short getIndex() {
  66. return _index;
  67. }
  68. /**
  69. * Return the parent style for this cell style.
  70. * In most cases this will be null, but in a few
  71. * cases there'll be a fully defined parent.
  72. */
  73. public HSSFCellStyle getParentStyle() {
  74. short parentIndex = _format.getParentIndex();
  75. // parentIndex equal 0xFFF indicates no inheritance from a cell style XF (See 2.4.353 XF)
  76. if(parentIndex == 0 || parentIndex == 0xFFF) {
  77. return null;
  78. }
  79. return new HSSFCellStyle(
  80. parentIndex,
  81. _workbook.getExFormatAt(parentIndex),
  82. _workbook
  83. );
  84. }
  85. /**
  86. * set the data format (must be a valid format)
  87. * @see org.apache.poi.hssf.usermodel.HSSFDataFormat
  88. */
  89. @Override
  90. public void setDataFormat(short fmt)
  91. {
  92. _format.setFormatIndex(fmt);
  93. }
  94. /**
  95. * get the index of the format
  96. * @see org.apache.poi.hssf.usermodel.HSSFDataFormat
  97. */
  98. @Override
  99. public short getDataFormat()
  100. {
  101. return _format.getFormatIndex();
  102. }
  103. // we keep the cached data in ThreadLocal members in order to
  104. // avoid multi-threading issues when different workbooks are accessed in
  105. // multiple threads at the same time
  106. private static final ThreadLocal<Short> lastDateFormat = ThreadLocal.withInitial(() -> Short.MIN_VALUE);
  107. private static final ThreadLocal<List<FormatRecord>> lastFormats = new ThreadLocal<>();
  108. private static final ThreadLocal<String> getDataFormatStringCache = new ThreadLocal<>();
  109. /**
  110. * Get the contents of the format string, by looking up
  111. * the DataFormat against the bound workbook
  112. * @see org.apache.poi.hssf.usermodel.HSSFDataFormat
  113. * @return the format string or "General" if not found
  114. */
  115. @Override
  116. public String getDataFormatString() {
  117. if (getDataFormatStringCache.get() != null) {
  118. if (lastDateFormat.get() == getDataFormat() && _workbook.getFormats().equals(lastFormats.get())) {
  119. return getDataFormatStringCache.get();
  120. }
  121. }
  122. lastFormats.set(_workbook.getFormats());
  123. lastDateFormat.set(getDataFormat());
  124. getDataFormatStringCache.set(getDataFormatString(_workbook));
  125. return getDataFormatStringCache.get();
  126. }
  127. /**
  128. * Get the contents of the format string, by looking up
  129. * the DataFormat against the supplied workbook
  130. * @see org.apache.poi.hssf.usermodel.HSSFDataFormat
  131. *
  132. * @return the format string or "General" if not found
  133. */
  134. public String getDataFormatString(org.apache.poi.ss.usermodel.Workbook workbook) {
  135. HSSFDataFormat format = new HSSFDataFormat( ((HSSFWorkbook)workbook).getWorkbook() );
  136. int idx = getDataFormat();
  137. return idx == -1 ? "General" : format.getFormat(getDataFormat());
  138. }
  139. /**
  140. * Get the contents of the format string, by looking up
  141. * the DataFormat against the supplied low level workbook
  142. * @see org.apache.poi.hssf.usermodel.HSSFDataFormat
  143. */
  144. public String getDataFormatString(org.apache.poi.hssf.model.InternalWorkbook workbook) {
  145. HSSFDataFormat format = new HSSFDataFormat( workbook );
  146. return format.getFormat(getDataFormat());
  147. }
  148. /**
  149. * set the font for this style
  150. * @param font a font object created or retrieved from the HSSFWorkbook object
  151. * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createFont()
  152. * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(int)
  153. */
  154. @Override
  155. public void setFont(Font font) {
  156. setFont((HSSFFont)font);
  157. }
  158. public void setFont(HSSFFont font) {
  159. _format.setIndentNotParentFont(true);
  160. short fontindex = font.getIndex();
  161. _format.setFontIndex(fontindex);
  162. }
  163. /**
  164. * gets the index of the font for this style
  165. * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short)
  166. */
  167. @Override
  168. @Deprecated
  169. public short getFontIndex()
  170. {
  171. return _format.getFontIndex();
  172. }
  173. /**
  174. * gets the index of the font for this style
  175. * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(int)
  176. * @since 4.0.0
  177. */
  178. @Override
  179. public int getFontIndexAsInt()
  180. {
  181. return _format.getFontIndex();
  182. }
  183. /**
  184. * gets the font for this style
  185. * @param parentWorkbook The HSSFWorkbook that this style belongs to
  186. * @see org.apache.poi.hssf.usermodel.HSSFCellStyle#getFontIndexAsInt()
  187. * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(int)
  188. */
  189. public HSSFFont getFont(org.apache.poi.ss.usermodel.Workbook parentWorkbook) {
  190. return ((HSSFWorkbook) parentWorkbook).getFontAt(getFontIndexAsInt());
  191. }
  192. /**
  193. * set the cell's using this style to be hidden
  194. * @param hidden - whether the cell using this style should be hidden
  195. */
  196. @Override
  197. public void setHidden(boolean hidden)
  198. {
  199. _format.setIndentNotParentCellOptions(true);
  200. _format.setHidden(hidden);
  201. }
  202. /**
  203. * get whether the cell's using this style are to be hidden
  204. * @return hidden - whether the cell using this style should be hidden
  205. */
  206. @Override
  207. public boolean getHidden()
  208. {
  209. return _format.isHidden();
  210. }
  211. /**
  212. * set the cell's using this style to be locked
  213. * @param locked - whether the cell using this style should be locked
  214. */
  215. @Override
  216. public void setLocked(boolean locked)
  217. {
  218. _format.setIndentNotParentCellOptions(true);
  219. _format.setLocked(locked);
  220. }
  221. /**
  222. * get whether the cell's using this style are to be locked
  223. * @return hidden - whether the cell using this style should be locked
  224. */
  225. @Override
  226. public boolean getLocked()
  227. {
  228. return _format.isLocked();
  229. }
  230. /**
  231. * Turn on or off "Quote Prefix" or "123 Prefix" for the style,
  232. * which is used to tell Excel that the thing which looks like
  233. * a number or a formula shouldn't be treated as on.
  234. */
  235. @Override
  236. public void setQuotePrefixed(boolean quotePrefix) {
  237. _format.set123Prefix(quotePrefix);
  238. }
  239. /**
  240. * Is "Quote Prefix" or "123 Prefix" enabled for the cell?
  241. */
  242. @Override
  243. public boolean getQuotePrefixed() {
  244. return _format.get123Prefix();
  245. }
  246. /**
  247. * set the type of horizontal alignment for the cell
  248. * @param align - the type of alignment
  249. */
  250. @Override
  251. public void setAlignment(HorizontalAlignment align)
  252. {
  253. _format.setIndentNotParentAlignment(true);
  254. _format.setAlignment(align.getCode());
  255. }
  256. @Override
  257. public HorizontalAlignment getAlignment()
  258. {
  259. return HorizontalAlignment.forInt(_format.getAlignment());
  260. }
  261. @Override
  262. public HorizontalAlignment getAlignmentEnum()
  263. {
  264. return getAlignment();
  265. }
  266. /**
  267. * set whether the text should be wrapped
  268. * @param wrapped wrap text or not
  269. */
  270. @Override
  271. public void setWrapText(boolean wrapped)
  272. {
  273. _format.setIndentNotParentAlignment(true);
  274. _format.setWrapText(wrapped);
  275. }
  276. /**
  277. * get whether the text should be wrapped
  278. * @return wrap text or not
  279. */
  280. @Override
  281. public boolean getWrapText()
  282. {
  283. return _format.getWrapText();
  284. }
  285. /**
  286. * set the type of vertical alignment for the cell
  287. * @param align the type of alignment
  288. */
  289. @Override
  290. public void setVerticalAlignment(VerticalAlignment align)
  291. {
  292. _format.setVerticalAlignment(align.getCode());
  293. }
  294. @Override
  295. public VerticalAlignment getVerticalAlignment() {
  296. return VerticalAlignment.forInt(_format.getVerticalAlignment());
  297. }
  298. @Override
  299. public VerticalAlignment getVerticalAlignmentEnum() {
  300. return getVerticalAlignment();
  301. }
  302. /**
  303. * set the degree of rotation for the text in the cell
  304. *
  305. * Note: HSSF uses values from -90 to 90 degrees, whereas XSSF
  306. * uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges
  307. * accordingly, however the corresponding getter is returning values in the range mandated by the current type
  308. * of Excel file-format that this CellStyle is applied to.
  309. *
  310. * @param rotation degrees (between -90 and 90 degrees, of 0xff for vertical)
  311. */
  312. @Override
  313. public void setRotation(short rotation)
  314. {
  315. if (rotation == 0xff) {
  316. // Special cases for vertically aligned text
  317. }
  318. else if ((rotation < 0)&&(rotation >= -90)) {
  319. //Take care of the funny 4th quadrant issue
  320. //The 4th quadrant (-1 to -90) is stored as (91 to 180)
  321. rotation = (short)(90 - rotation);
  322. }
  323. else if (rotation > 90 && rotation <= 180) {
  324. // stay compatible with the range used by XSSF, map from ]90..180] to ]0..-90]
  325. // we actually don't need to do anything here as the internal value is stored in [0-180] anyway!
  326. }
  327. else if ((rotation < -90) || (rotation > 90)) {
  328. //Do not allow an incorrect rotation to be set
  329. throw new IllegalArgumentException("The rotation must be between -90 and 90 degrees, or 0xff");
  330. }
  331. _format.setRotation(rotation);
  332. }
  333. /**
  334. * get the degree of rotation for the text in the cell
  335. * @return rotation degrees (between -90 and 90 degrees, or 0xff for vertical)
  336. */
  337. @Override
  338. public short getRotation()
  339. {
  340. short rotation = _format.getRotation();
  341. if (rotation == 0xff) {
  342. // Vertical aligned special case
  343. return rotation;
  344. }
  345. if (rotation > 90) {
  346. //This is actually the 4th quadrant
  347. rotation = (short)(90-rotation);
  348. }
  349. return rotation;
  350. }
  351. /**
  352. * set the number of spaces to indent the text in the cell
  353. * @param indent - number of spaces
  354. */
  355. @Override
  356. public void setIndention(short indent)
  357. {
  358. _format.setIndent(indent);
  359. }
  360. /**
  361. * get the number of spaces to indent the text in the cell
  362. * @return indent - number of spaces
  363. */
  364. @Override
  365. public short getIndention()
  366. {
  367. return _format.getIndent();
  368. }
  369. /**
  370. * set the type of border to use for the left border of the cell
  371. * @param border type
  372. * @since POI 3.15
  373. */
  374. @Override
  375. public void setBorderLeft(BorderStyle border)
  376. {
  377. _format.setIndentNotParentBorder(true);
  378. _format.setBorderLeft(border.getCode());
  379. }
  380. @Override
  381. public BorderStyle getBorderLeft()
  382. {
  383. return BorderStyle.valueOf(_format.getBorderLeft());
  384. }
  385. @Override
  386. public BorderStyle getBorderLeftEnum() { return getBorderLeft(); }
  387. /**
  388. * set the type of border to use for the right border of the cell
  389. * @param border type
  390. * @since POI 3.15
  391. */
  392. @Override
  393. public void setBorderRight(BorderStyle border)
  394. {
  395. _format.setIndentNotParentBorder(true);
  396. _format.setBorderRight(border.getCode());
  397. }
  398. @Override
  399. public BorderStyle getBorderRight()
  400. {
  401. return BorderStyle.valueOf(_format.getBorderRight());
  402. }
  403. @Override
  404. public BorderStyle getBorderRightEnum() { return getBorderRight(); }
  405. /**
  406. * set the type of border to use for the top border of the cell
  407. * @param border type
  408. * @since POI 3.15
  409. */
  410. @Override
  411. public void setBorderTop(BorderStyle border)
  412. {
  413. _format.setIndentNotParentBorder(true);
  414. _format.setBorderTop(border.getCode());
  415. }
  416. @Override
  417. public BorderStyle getBorderTop()
  418. {
  419. return BorderStyle.valueOf(_format.getBorderTop());
  420. }
  421. @Override
  422. public BorderStyle getBorderTopEnum() { return getBorderTop(); }
  423. /**
  424. * set the type of border to use for the bottom border of the cell
  425. * @param border type
  426. * @since 3.15 beta 2
  427. */
  428. @Override
  429. public void setBorderBottom(BorderStyle border)
  430. {
  431. _format.setIndentNotParentBorder(true);
  432. _format.setBorderBottom(border.getCode());
  433. }
  434. @Override
  435. public BorderStyle getBorderBottom()
  436. {
  437. return BorderStyle.valueOf(_format.getBorderBottom());
  438. }
  439. @Override
  440. public BorderStyle getBorderBottomEnum() { return getBorderBottom(); }
  441. /**
  442. * set the color to use for the left border
  443. * @param color The index of the color definition
  444. */
  445. @Override
  446. public void setLeftBorderColor(short color)
  447. {
  448. _format.setLeftBorderPaletteIdx(color);
  449. }
  450. /**
  451. * get the color to use for the left border
  452. * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
  453. * @return The index of the color definition
  454. */
  455. @Override
  456. public short getLeftBorderColor()
  457. {
  458. return _format.getLeftBorderPaletteIdx();
  459. }
  460. /**
  461. * set the color to use for the right border
  462. * @param color The index of the color definition
  463. */
  464. @Override
  465. public void setRightBorderColor(short color)
  466. {
  467. _format.setRightBorderPaletteIdx(color);
  468. }
  469. /**
  470. * get the color to use for the left border
  471. * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
  472. * @return The index of the color definition
  473. */
  474. @Override
  475. public short getRightBorderColor()
  476. {
  477. return _format.getRightBorderPaletteIdx();
  478. }
  479. /**
  480. * set the color to use for the top border
  481. * @param color The index of the color definition
  482. */
  483. @Override
  484. public void setTopBorderColor(short color)
  485. {
  486. _format.setTopBorderPaletteIdx(color);
  487. }
  488. /**
  489. * get the color to use for the top border
  490. * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
  491. * @return The index of the color definition
  492. */
  493. @Override
  494. public short getTopBorderColor()
  495. {
  496. return _format.getTopBorderPaletteIdx();
  497. }
  498. /**
  499. * set the color to use for the bottom border
  500. * @param color The index of the color definition
  501. */
  502. @Override
  503. public void setBottomBorderColor(short color)
  504. {
  505. _format.setBottomBorderPaletteIdx(color);
  506. }
  507. /**
  508. * get the color to use for the left border
  509. * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
  510. * @return The index of the color definition
  511. */
  512. @Override
  513. public short getBottomBorderColor()
  514. {
  515. return _format.getBottomBorderPaletteIdx();
  516. }
  517. /**
  518. * setting to one fills the cell with the foreground color... No idea about
  519. * other values
  520. *
  521. * @param fp fill pattern (set to {@link FillPatternType#SOLID_FOREGROUND} to fill w/foreground color)
  522. */
  523. @Override
  524. public void setFillPattern(FillPatternType fp)
  525. {
  526. _format.setAdtlFillPattern(fp.getCode());
  527. }
  528. @Override
  529. public FillPatternType getFillPattern()
  530. {
  531. return FillPatternType.forInt(_format.getAdtlFillPattern());
  532. }
  533. @Override
  534. public FillPatternType getFillPatternEnum() { return getFillPattern(); }
  535. /**
  536. * Checks if the background and foreground fills are set correctly when one
  537. * or the other is set to the default color.
  538. * <p>Works like the logic table below:</p>
  539. * <p>BACKGROUND FOREGROUND</p>
  540. * <p>NONE AUTOMATIC</p>
  541. * <p>0x41 0x40</p>
  542. * <p>NONE RED/ANYTHING</p>
  543. * <p>0x40 0xSOMETHING</p>
  544. */
  545. private void checkDefaultBackgroundFills() {
  546. final short autoIdx = HSSFColorPredefined.AUTOMATIC.getIndex();
  547. if (_format.getFillForeground() == autoIdx) {
  548. //JMH: Why +1, hell why not. I guess it made some sense to someone at the time. Doesnt
  549. //to me now.... But experience has shown that when the fore is set to AUTOMATIC then the
  550. //background needs to be incremented......
  551. if (_format.getFillBackground() != autoIdx+1) {
  552. setFillBackgroundColor((short)(autoIdx+1));
  553. }
  554. } else if (_format.getFillBackground() == autoIdx+1) {
  555. //Now if the forground changes to a non-AUTOMATIC color the background resets itself!!!
  556. if (_format.getFillForeground() != autoIdx) {
  557. setFillBackgroundColor(autoIdx);
  558. }
  559. }
  560. }
  561. /**
  562. * set the background fill color.
  563. * <p>
  564. * For example:
  565. * <pre>
  566. * cs.setFillPattern(HSSFCellStyle.FINE_DOTS );
  567. * cs.setFillBackgroundColor(new HSSFColor.RED().getIndex());
  568. * </pre>
  569. * optionally a Foreground and background fill can be applied:
  570. * <i>Note: Ensure Foreground color is set prior to background</i>
  571. * <pre>
  572. * cs.setFillPattern(HSSFCellStyle.FINE_DOTS );
  573. * cs.setFillForegroundColor(new HSSFColor.BLUE().getIndex());
  574. * cs.setFillBackgroundColor(new HSSFColor.RED().getIndex());
  575. * </pre>
  576. * or, for the special case of SOLID_FILL:
  577. * <pre>
  578. * cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND );
  579. * cs.setFillForegroundColor(new HSSFColor.RED().getIndex());
  580. * </pre>
  581. * It is necessary to set the fill style in order
  582. * for the color to be shown in the cell.
  583. *
  584. * @param bg color
  585. */
  586. @Override
  587. public void setFillBackgroundColor(short bg)
  588. {
  589. _format.setFillBackground(bg);
  590. checkDefaultBackgroundFills();
  591. }
  592. /**
  593. * Get the background fill color.
  594. * Note - many cells are actually filled with a foreground
  595. * fill, not a background fill - see {@link #getFillForegroundColor()}
  596. * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
  597. * @return fill color
  598. */
  599. @Override
  600. public short getFillBackgroundColor() {
  601. final short autoIndex = HSSFColorPredefined.AUTOMATIC.getIndex();
  602. short result = _format.getFillBackground();
  603. //JMH: Do this ridiculous conversion, and let HSSFCellStyle
  604. //internally migrate back and forth
  605. if (result == autoIndex+1) {
  606. return autoIndex;
  607. }
  608. return result;
  609. }
  610. @Override
  611. public HSSFColor getFillBackgroundColorColor() {
  612. HSSFPalette pallette = new HSSFPalette(
  613. _workbook.getCustomPalette()
  614. );
  615. return pallette.getColor(
  616. getFillBackgroundColor()
  617. );
  618. }
  619. /**
  620. * set the foreground fill color
  621. * <i>Note: Ensure Foreground color is set prior to background color.</i>
  622. * @param bg color
  623. */
  624. @Override
  625. public void setFillForegroundColor(short bg)
  626. {
  627. _format.setFillForeground(bg);
  628. checkDefaultBackgroundFills();
  629. }
  630. /**
  631. * Get the foreground fill color.
  632. * Many cells are filled with this, instead of a
  633. * background color ({@link #getFillBackgroundColor()})
  634. * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
  635. * @return fill color
  636. */
  637. @Override
  638. public short getFillForegroundColor()
  639. {
  640. return _format.getFillForeground();
  641. }
  642. @Override
  643. public HSSFColor getFillForegroundColorColor() {
  644. HSSFPalette pallette = new HSSFPalette(
  645. _workbook.getCustomPalette()
  646. );
  647. return pallette.getColor(
  648. getFillForegroundColor()
  649. );
  650. }
  651. /**
  652. * Gets the name of the user defined style.
  653. * Returns null for built in styles, and
  654. * styles where no name has been defined
  655. */
  656. public String getUserStyleName() {
  657. StyleRecord sr = _workbook.getStyleRecord(_index);
  658. if(sr == null) {
  659. return null;
  660. }
  661. if(sr.isBuiltin()) {
  662. return null;
  663. }
  664. return sr.getName();
  665. }
  666. /**
  667. * Sets the name of the user defined style.
  668. * Will complain if you try this on a built in style.
  669. */
  670. public void setUserStyleName(String styleName) {
  671. StyleRecord sr = _workbook.getStyleRecord(_index);
  672. if(sr == null) {
  673. sr = _workbook.createStyleRecord(_index);
  674. }
  675. // All Style records start as "builtin", but generally
  676. // only 20 and below really need to be
  677. if(sr.isBuiltin() && _index <= 20) {
  678. throw new IllegalArgumentException("Unable to set user specified style names for built in styles!");
  679. }
  680. sr.setName(styleName);
  681. }
  682. /**
  683. * Controls if the Cell should be auto-sized
  684. * to shrink to fit if the text is too long
  685. */
  686. @Override
  687. public void setShrinkToFit(boolean shrinkToFit) {
  688. _format.setShrinkToFit(shrinkToFit);
  689. }
  690. /**
  691. * Should the Cell be auto-sized by Excel to shrink
  692. * it to fit if this text is too long?
  693. */
  694. @Override
  695. public boolean getShrinkToFit() {
  696. return _format.getShrinkToFit();
  697. }
  698. /**
  699. * Get the reading order, for RTL/LTR ordering of
  700. * the text.
  701. * <p>0 means Context (Default), 1 means Left To Right,
  702. * and 2 means Right to Left</p>
  703. *
  704. * @return order - the reading order (0,1,2)
  705. */
  706. public short getReadingOrder() {
  707. return _format.getReadingOrder();
  708. }
  709. /**
  710. * Sets the reading order, for RTL/LTR ordering of
  711. * the text.
  712. * <p>0 means Context (Default), 1 means Left To Right,
  713. * and 2 means Right to Left</p>
  714. *
  715. * @param order - the reading order (0,1,2)
  716. */
  717. public void setReadingOrder(short order) {
  718. _format.setReadingOrder(order);
  719. }
  720. /**
  721. * Verifies that this style belongs to the supplied Workbook.
  722. * Will throw an exception if it belongs to a different one.
  723. * This is normally called when trying to assign a style to a
  724. * cell, to ensure the cell and the style are from the same
  725. * workbook (if they're not, it won't work)
  726. * @throws IllegalArgumentException if there's a workbook mis-match
  727. */
  728. public void verifyBelongsToWorkbook(HSSFWorkbook wb) {
  729. if(wb.getWorkbook() != _workbook) {
  730. throw new IllegalArgumentException("This Style does not belong to the supplied Workbook. Are you trying to assign a style from one workbook to the cell of a differnt workbook?");
  731. }
  732. }
  733. /**
  734. * Clones all the style information from another
  735. * HSSFCellStyle, onto this one. This
  736. * HSSFCellStyle will then have all the same
  737. * properties as the source, but the two may
  738. * be edited independently.
  739. * Any stylings on this HSSFCellStyle will be lost!
  740. *
  741. * The source HSSFCellStyle could be from another
  742. * HSSFWorkbook if you like. This allows you to
  743. * copy styles from one HSSFWorkbook to another.
  744. */
  745. @Override
  746. public void cloneStyleFrom(CellStyle source) {
  747. if(source instanceof HSSFCellStyle) {
  748. this.cloneStyleFrom((HSSFCellStyle)source);
  749. } else {
  750. throw new IllegalArgumentException("Can only clone from one HSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle");
  751. }
  752. }
  753. public void cloneStyleFrom(HSSFCellStyle source) {
  754. // First we need to clone the extended format
  755. // record
  756. _format.cloneStyleFrom(source._format);
  757. // Handle matching things if we cross workbooks
  758. if(_workbook != source._workbook) {
  759. lastDateFormat.set(Short.MIN_VALUE);
  760. lastFormats.remove();
  761. getDataFormatStringCache.remove();
  762. // Then we need to clone the format string,
  763. // and update the format record for this
  764. short fmt = (short)_workbook.createFormat(source.getDataFormatString() );
  765. setDataFormat(fmt);
  766. // Finally we need to clone the font,
  767. // and update the format record for this
  768. FontRecord fr = _workbook.createNewFont();
  769. fr.cloneStyleFrom(
  770. source._workbook.getFontRecordAt(
  771. source.getFontIndexAsInt()
  772. )
  773. );
  774. HSSFFont font = new HSSFFont(
  775. (short)_workbook.getFontIndex(fr), fr
  776. );
  777. setFont(font);
  778. }
  779. }
  780. @Override
  781. public int hashCode() {
  782. return Objects.hash(_format, _index);
  783. }
  784. @Override
  785. public boolean equals(Object obj) {
  786. if (this == obj) {
  787. return true;
  788. }
  789. if (obj == null) {
  790. return false;
  791. }
  792. if (obj instanceof HSSFCellStyle) {
  793. final HSSFCellStyle other = (HSSFCellStyle) obj;
  794. if (_format == null) {
  795. if (other._format != null) {
  796. return false;
  797. }
  798. } else if (!_format.equals(other._format)) {
  799. return false;
  800. }
  801. if (_index != other._index) {
  802. return false;
  803. }
  804. return true;
  805. }
  806. return false;
  807. }
  808. @Override
  809. public HSSFCellStyle copy() {
  810. return new HSSFCellStyle(this);
  811. }
  812. }