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

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