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 29KB

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