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

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