Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

TestCellFormat.java 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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.ss.format;
  16. import static org.junit.Assert.*;
  17. import java.io.IOException;
  18. import java.text.ParseException;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Date;
  21. import java.util.Locale;
  22. import java.util.TimeZone;
  23. import javax.swing.JLabel;
  24. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  25. import org.apache.poi.ss.usermodel.Cell;
  26. import org.apache.poi.ss.usermodel.DateUtil;
  27. import org.apache.poi.ss.usermodel.Row;
  28. import org.apache.poi.ss.usermodel.Sheet;
  29. import org.apache.poi.ss.usermodel.Workbook;
  30. import org.apache.poi.util.LocaleUtil;
  31. import org.junit.AfterClass;
  32. import org.junit.BeforeClass;
  33. import org.junit.Test;
  34. public class TestCellFormat {
  35. private static TimeZone userTimeZone;
  36. @BeforeClass
  37. public static void setTimeZone() {
  38. userTimeZone = LocaleUtil.getUserTimeZone();
  39. LocaleUtil.setUserTimeZone(TimeZone.getTimeZone("CET"));
  40. LocaleUtil.setUserLocale(Locale.US);
  41. }
  42. @AfterClass
  43. public static void resetTimeZone() {
  44. LocaleUtil.setUserTimeZone(userTimeZone);
  45. LocaleUtil.setUserLocale(Locale.ROOT);
  46. }
  47. private static final String _255_POUND_SIGNS;
  48. static {
  49. StringBuilder sb = new StringBuilder();
  50. for (int i = 1; i <= 255; i++) {
  51. sb.append('#');
  52. }
  53. _255_POUND_SIGNS = sb.toString();
  54. }
  55. @Test
  56. public void testSome() {
  57. JLabel l = new JLabel();
  58. CellFormat fmt = CellFormat.getInstance(
  59. "\"$\"#,##0.00_);[Red]\\(\"$\"#,##0.00\\)");
  60. fmt.apply(l, 1.1);
  61. }
  62. @Test
  63. public void testPositiveFormatHasOnePart() {
  64. CellFormat fmt = CellFormat.getInstance("0.00");
  65. CellFormatResult result = fmt.apply(12.345);
  66. assertEquals("12.35", result.text);
  67. }
  68. @Test
  69. public void testNegativeFormatHasOnePart() {
  70. CellFormat fmt = CellFormat.getInstance("0.00");
  71. CellFormatResult result = fmt.apply(-12.345);
  72. assertEquals("-12.35", result.text);
  73. }
  74. @Test
  75. public void testZeroFormatHasOnePart() {
  76. CellFormat fmt = CellFormat.getInstance("0.00");
  77. CellFormatResult result = fmt.apply(0.0);
  78. assertEquals("0.00", result.text);
  79. }
  80. @Test
  81. public void testPositiveFormatHasPosAndNegParts() {
  82. CellFormat fmt = CellFormat.getInstance("0.00;-0.00");
  83. CellFormatResult result = fmt.apply(12.345);
  84. assertEquals("12.35", result.text);
  85. }
  86. @Test
  87. public void testNegativeFormatHasPosAndNegParts() {
  88. CellFormat fmt = CellFormat.getInstance("0.00;-0.00");
  89. CellFormatResult result = fmt.apply(-12.345);
  90. assertEquals("-12.35", result.text);
  91. }
  92. @Test
  93. public void testNegativeFormatHasPosAndNegParts2() {
  94. CellFormat fmt = CellFormat.getInstance("0.00;(0.00)");
  95. CellFormatResult result = fmt.apply(-12.345);
  96. assertEquals("(12.35)", result.text);
  97. }
  98. @Test
  99. public void testZeroFormatHasPosAndNegParts() {
  100. CellFormat fmt = CellFormat.getInstance("0.00;-0.00");
  101. CellFormatResult result = fmt.apply(0.0);
  102. assertEquals("0.00", result.text);
  103. }
  104. @Test
  105. public void testFormatWithThreeSections() {
  106. CellFormat fmt = CellFormat.getInstance("0.00;-0.00;-");
  107. assertEquals("12.35", fmt.apply(12.345).text);
  108. assertEquals("-12.35", fmt.apply(-12.345).text);
  109. assertEquals("-", fmt.apply(0.0).text);
  110. assertEquals("abc", fmt.apply("abc").text);
  111. }
  112. @Test
  113. public void testFormatWithFourSections() {
  114. CellFormat fmt = CellFormat.getInstance("0.00;-0.00;-; @ ");
  115. assertEquals("12.35", fmt.apply(12.345).text);
  116. assertEquals("-12.35", fmt.apply(-12.345).text);
  117. assertEquals("-", fmt.apply(0.0).text);
  118. assertEquals(" abc ", fmt.apply("abc").text);
  119. }
  120. @Test
  121. public void testApplyCellForGeneralFormat() throws Exception {
  122. // Create a workbook, row and cell to test with
  123. Workbook wb = new HSSFWorkbook();
  124. Sheet sheet = wb.createSheet();
  125. Row row = sheet.createRow(0);
  126. Cell cell0 = row.createCell(0);
  127. Cell cell1 = row.createCell(1);
  128. Cell cell2 = row.createCell(2);
  129. Cell cell3 = row.createCell(3);
  130. Cell cell4 = row.createCell(4);
  131. CellFormat cf = CellFormat.getInstance("General");
  132. // case CellType.BLANK
  133. CellFormatResult result0 = cf.apply(cell0);
  134. assertEquals("", result0.text);
  135. // case CellType.BOOLEAN
  136. cell1.setCellValue(true);
  137. CellFormatResult result1 = cf.apply(cell1);
  138. assertEquals("TRUE", result1.text);
  139. // case CellType.NUMERIC
  140. cell2.setCellValue(1.23);
  141. CellFormatResult result2 = cf.apply(cell2);
  142. assertEquals("1.23", result2.text);
  143. cell3.setCellValue(123.0);
  144. CellFormatResult result3 = cf.apply(cell3);
  145. assertEquals("123", result3.text);
  146. // case CellType.STRING
  147. cell4.setCellValue("abc");
  148. CellFormatResult result4 = cf.apply(cell4);
  149. assertEquals("abc", result4.text);
  150. wb.close();
  151. }
  152. @Test
  153. public void testApplyCellForAtFormat() throws Exception {
  154. // Create a workbook, row and cell to test with
  155. Workbook wb = new HSSFWorkbook();
  156. Sheet sheet = wb.createSheet();
  157. Row row = sheet.createRow(0);
  158. Cell cell0 = row.createCell(0);
  159. Cell cell1 = row.createCell(1);
  160. Cell cell2 = row.createCell(2);
  161. Cell cell3 = row.createCell(3);
  162. Cell cell4 = row.createCell(4);
  163. CellFormat cf = CellFormat.getInstance("@");
  164. // case CellType.BLANK
  165. CellFormatResult result0 = cf.apply(cell0);
  166. assertEquals("", result0.text);
  167. // case CellType.BOOLEAN
  168. cell1.setCellValue(true);
  169. CellFormatResult result1 = cf.apply(cell1);
  170. assertEquals("TRUE", result1.text);
  171. // case CellType.NUMERIC
  172. cell2.setCellValue(1.23);
  173. CellFormatResult result2 = cf.apply(cell2);
  174. assertEquals("1.23", result2.text);
  175. cell3.setCellValue(123.0);
  176. CellFormatResult result3 = cf.apply(cell3);
  177. assertEquals("123", result3.text);
  178. // case CellType.STRING
  179. cell4.setCellValue("abc");
  180. CellFormatResult result4 = cf.apply(cell4);
  181. assertEquals("abc", result4.text);
  182. wb.close();
  183. }
  184. @Test
  185. public void testApplyCellForDateFormat() throws Exception {
  186. // Create a workbook, row and cell to test with
  187. Workbook wb = new HSSFWorkbook();
  188. Sheet sheet = wb.createSheet();
  189. Row row = sheet.createRow(0);
  190. Cell cell0 = row.createCell(0);
  191. Cell cell1 = row.createCell(1);
  192. CellFormat cf = CellFormat.getInstance("dd/mm/yyyy");
  193. cell0.setCellValue(10);
  194. CellFormatResult result0 = cf.apply(cell0);
  195. assertEquals("10/01/1900", result0.text);
  196. cell1.setCellValue(-1);
  197. CellFormatResult result1 = cf.apply(cell1);
  198. assertEquals(_255_POUND_SIGNS, result1.text);
  199. wb.close();
  200. }
  201. @Test
  202. public void testApplyCellForTimeFormat() throws Exception {
  203. // Create a workbook, row and cell to test with
  204. Workbook wb = new HSSFWorkbook();
  205. Sheet sheet = wb.createSheet();
  206. Row row = sheet.createRow(0);
  207. Cell cell = row.createCell(0);
  208. CellFormat cf = CellFormat.getInstance("hh:mm");
  209. cell.setCellValue(DateUtil.convertTime("03:04:05"));
  210. CellFormatResult result = cf.apply(cell);
  211. assertEquals("03:04", result.text);
  212. wb.close();
  213. }
  214. @Test
  215. public void testApplyCellForDateFormatAndNegativeFormat() throws Exception {
  216. // Create a workbook, row and cell to test with
  217. Workbook wb = new HSSFWorkbook();
  218. Sheet sheet = wb.createSheet();
  219. Row row = sheet.createRow(0);
  220. Cell cell0 = row.createCell(0);
  221. Cell cell1 = row.createCell(1);
  222. CellFormat cf = CellFormat.getInstance("dd/mm/yyyy;(0)");
  223. cell0.setCellValue(10);
  224. CellFormatResult result0 = cf.apply(cell0);
  225. assertEquals("10/01/1900", result0.text);
  226. cell1.setCellValue(-1);
  227. CellFormatResult result1 = cf.apply(cell1);
  228. assertEquals("(1)", result1.text);
  229. wb.close();
  230. }
  231. @Test
  232. public void testApplyJLabelCellForGeneralFormat() throws Exception {
  233. // Create a workbook, row and cell to test with
  234. Workbook wb = new HSSFWorkbook();
  235. Sheet sheet = wb.createSheet();
  236. Row row = sheet.createRow(0);
  237. Cell cell0 = row.createCell(0);
  238. Cell cell1 = row.createCell(1);
  239. Cell cell2 = row.createCell(2);
  240. Cell cell3 = row.createCell(3);
  241. Cell cell4 = row.createCell(4);
  242. CellFormat cf = CellFormat.getInstance("General");
  243. JLabel label0 = new JLabel();
  244. JLabel label1 = new JLabel();
  245. JLabel label2 = new JLabel();
  246. JLabel label3 = new JLabel();
  247. JLabel label4 = new JLabel();
  248. // case CellType.BLANK
  249. CellFormatResult result0 = cf.apply(label0, cell0);
  250. assertEquals("", result0.text);
  251. assertEquals("", label0.getText());
  252. // case CellType.BOOLEAN
  253. cell1.setCellValue(true);
  254. CellFormatResult result1 = cf.apply(label1, cell1);
  255. assertEquals("TRUE", result1.text);
  256. assertEquals("TRUE", label1.getText());
  257. // case CellType.NUMERIC
  258. cell2.setCellValue(1.23);
  259. CellFormatResult result2 = cf.apply(label2, cell2);
  260. assertEquals("1.23", result2.text);
  261. assertEquals("1.23", label2.getText());
  262. cell3.setCellValue(123.0);
  263. CellFormatResult result3 = cf.apply(label3, cell3);
  264. assertEquals("123", result3.text);
  265. assertEquals("123", label3.getText());
  266. // case CellType.STRING
  267. cell4.setCellValue("abc");
  268. CellFormatResult result4 = cf.apply(label4, cell4);
  269. assertEquals("abc", result4.text);
  270. assertEquals("abc", label4.getText());
  271. wb.close();
  272. }
  273. @Test
  274. public void testApplyJLabelCellForAtFormat() throws Exception {
  275. // Create a workbook, row and cell to test with
  276. Workbook wb = new HSSFWorkbook();
  277. Sheet sheet = wb.createSheet();
  278. Row row = sheet.createRow(0);
  279. Cell cell0 = row.createCell(0);
  280. Cell cell1 = row.createCell(1);
  281. Cell cell2 = row.createCell(2);
  282. Cell cell3 = row.createCell(3);
  283. Cell cell4 = row.createCell(4);
  284. CellFormat cf = CellFormat.getInstance("@");
  285. JLabel label0 = new JLabel();
  286. JLabel label1 = new JLabel();
  287. JLabel label2 = new JLabel();
  288. JLabel label3 = new JLabel();
  289. JLabel label4 = new JLabel();
  290. // case CellType.BLANK
  291. CellFormatResult result0 = cf.apply(label0, cell0);
  292. assertEquals("", result0.text);
  293. assertEquals("", label0.getText());
  294. // case CellType.BOOLEAN
  295. cell1.setCellValue(true);
  296. CellFormatResult result1 = cf.apply(label1, cell1);
  297. assertEquals("TRUE", result1.text);
  298. assertEquals("TRUE", label1.getText());
  299. // case CellType.NUMERIC
  300. cell2.setCellValue(1.23);
  301. CellFormatResult result2 = cf.apply(label2, cell2);
  302. assertEquals("1.23", result2.text);
  303. assertEquals("1.23", label2.getText());
  304. cell3.setCellValue(123.0);
  305. CellFormatResult result3 = cf.apply(label3, cell3);
  306. assertEquals("123", result3.text);
  307. assertEquals("123", label3.getText());
  308. // case CellType.STRING
  309. cell4.setCellValue("abc");
  310. CellFormatResult result4 = cf.apply(label4, cell4);
  311. assertEquals("abc", result4.text);
  312. assertEquals("abc", label4.getText());
  313. wb.close();
  314. }
  315. @Test
  316. public void testApplyJLabelCellForDateFormat() throws Exception {
  317. // Create a workbook, row and cell to test with
  318. Workbook wb = new HSSFWorkbook();
  319. Sheet sheet = wb.createSheet();
  320. Row row = sheet.createRow(0);
  321. Cell cell0 = row.createCell(0);
  322. Cell cell1 = row.createCell(1);
  323. CellFormat cf = CellFormat.getInstance("dd/mm/yyyy");
  324. JLabel label0 = new JLabel();
  325. JLabel label1 = new JLabel();
  326. cell0.setCellValue(10);
  327. CellFormatResult result0 = cf.apply(label0, cell0);
  328. assertEquals("10/01/1900", result0.text);
  329. assertEquals("10/01/1900", label0.getText());
  330. cell1.setCellValue(-1);
  331. CellFormatResult result1 = cf.apply(label1, cell1);
  332. assertEquals(_255_POUND_SIGNS, result1.text);
  333. assertEquals(_255_POUND_SIGNS, label1.getText());
  334. wb.close();
  335. }
  336. @Test
  337. public void testApplyJLabelCellForTimeFormat() throws Exception {
  338. // Create a workbook, row and cell to test with
  339. Workbook wb = new HSSFWorkbook();
  340. Sheet sheet = wb.createSheet();
  341. Row row = sheet.createRow(0);
  342. Cell cell = row.createCell(0);
  343. CellFormat cf = CellFormat.getInstance("hh:mm");
  344. JLabel label = new JLabel();
  345. cell.setCellValue(DateUtil.convertTime("03:04:05"));
  346. CellFormatResult result = cf.apply(label, cell);
  347. assertEquals("03:04", result.text);
  348. assertEquals("03:04", label.getText());
  349. wb.close();
  350. }
  351. @Test
  352. public void testApplyJLabelCellForDateFormatAndNegativeFormat() throws Exception {
  353. // Create a workbook, row and cell to test with
  354. Workbook wb = new HSSFWorkbook();
  355. Sheet sheet = wb.createSheet();
  356. Row row = sheet.createRow(0);
  357. Cell cell0 = row.createCell(0);
  358. Cell cell1 = row.createCell(1);
  359. CellFormat cf = CellFormat.getInstance("dd/mm/yyyy;(0)");
  360. JLabel label0 = new JLabel();
  361. JLabel label1 = new JLabel();
  362. cell0.setCellValue(10);
  363. CellFormatResult result0 = cf.apply(label0, cell0);
  364. assertEquals("10/01/1900", result0.text);
  365. assertEquals("10/01/1900", label0.getText());
  366. cell1.setCellValue(-1);
  367. CellFormatResult result1 = cf.apply(label1, cell1);
  368. assertEquals("(1)", result1.text);
  369. assertEquals("(1)", label1.getText());
  370. wb.close();
  371. }
  372. @Test
  373. public void testApplyFormatHasOnePartAndPartHasCondition() throws Exception {
  374. // Create a workbook, row and cell to test with
  375. Workbook wb = new HSSFWorkbook();
  376. Sheet sheet = wb.createSheet();
  377. Row row = sheet.createRow(0);
  378. Cell cell = row.createCell(0);
  379. CellFormat cf = CellFormat.getInstance("[>=100]0.00");
  380. cell.setCellValue(100);
  381. assertEquals("100.00", cf.apply(cell).text);
  382. cell.setCellValue(10);
  383. assertEquals("10", cf.apply(cell).text);
  384. cell.setCellValue(0.123456789012345);
  385. assertEquals("0.123456789", cf.apply(cell).text);
  386. cell.setCellValue(0);
  387. assertEquals("0", cf.apply(cell).text);
  388. cell.setCellValue("abc");
  389. assertEquals("abc", cf.apply(cell).text);
  390. wb.close();
  391. }
  392. @Test
  393. public void testApplyFormatHasTwoPartsFirstHasCondition() throws Exception {
  394. // Create a workbook, row and cell to test with
  395. Workbook wb = new HSSFWorkbook();
  396. Sheet sheet = wb.createSheet();
  397. Row row = sheet.createRow(0);
  398. Cell cell = row.createCell(0);
  399. CellFormat cf = CellFormat.getInstance("[>=100]0.00;0.000");
  400. cell.setCellValue(100);
  401. assertEquals("100.00", cf.apply(cell).text);
  402. cell.setCellValue(10);
  403. assertEquals("10.000", cf.apply(cell).text);
  404. cell.setCellValue(0.123456789012345);
  405. assertEquals("0.123", cf.apply(cell).text);
  406. cell.setCellValue(0);
  407. assertEquals("0.000", cf.apply(cell).text);
  408. cell.setCellValue(-10);
  409. assertEquals("-10.000", cf.apply(cell).text);
  410. cell.setCellValue("abc");
  411. assertEquals("abc", cf.apply(cell).text);
  412. cell.setCellValue("TRUE");
  413. assertEquals("TRUE", cf.apply(cell).text);
  414. wb.close();
  415. }
  416. @Test
  417. public void testApplyFormatHasTwoPartsBothHaveCondition() throws Exception {
  418. // Create a workbook, row and cell to test with
  419. Workbook wb = new HSSFWorkbook();
  420. Sheet sheet = wb.createSheet();
  421. Row row = sheet.createRow(0);
  422. Cell cell = row.createCell(0);
  423. CellFormat cf = CellFormat.getInstance("[>=100]0.00;[>=10]0.000");
  424. cell.setCellValue(100);
  425. assertEquals("100.00", cf.apply(cell).text);
  426. cell.setCellValue(10);
  427. assertEquals("10.000", cf.apply(cell).text);
  428. cell.setCellValue(0);
  429. assertEquals(_255_POUND_SIGNS, cf.apply(cell).text);
  430. cell.setCellValue(-0.123456789012345);
  431. assertEquals(_255_POUND_SIGNS, cf.apply(cell).text);
  432. cell.setCellValue(-10);
  433. assertEquals(_255_POUND_SIGNS, cf.apply(cell).text);
  434. cell.setCellValue("abc");
  435. assertEquals("abc", cf.apply(cell).text);
  436. wb.close();
  437. }
  438. @Test
  439. public void testApplyFormatHasThreePartsFirstHasCondition() throws Exception {
  440. // Create a workbook, row and cell to test with
  441. Workbook wb = new HSSFWorkbook();
  442. Sheet sheet = wb.createSheet();
  443. Row row = sheet.createRow(0);
  444. Cell cell = row.createCell(0);
  445. CellFormat cf = CellFormat.getInstance("[>=100]0.00;0.000;0.0000");
  446. cell.setCellValue(100);
  447. assertEquals("100.00", cf.apply(cell).text);
  448. cell.setCellValue(10);
  449. assertEquals("10.0000", cf.apply(cell).text);
  450. cell.setCellValue(0.123456789012345);
  451. assertEquals("0.1235", cf.apply(cell).text);
  452. cell.setCellValue(0);
  453. assertEquals("0.0000", cf.apply(cell).text);
  454. // Second format part ('0.000') is used for negative numbers
  455. // so result does not have a minus sign
  456. cell.setCellValue(-10);
  457. assertEquals("10.000", cf.apply(cell).text);
  458. cell.setCellValue("abc");
  459. assertEquals("abc", cf.apply(cell).text);
  460. wb.close();
  461. }
  462. @Test
  463. public void testApplyFormatHasThreePartsFirstTwoHaveCondition() throws Exception {
  464. // Create a workbook, row and cell to test with
  465. Workbook wb = new HSSFWorkbook();
  466. Sheet sheet = wb.createSheet();
  467. Row row = sheet.createRow(0);
  468. Cell cell = row.createCell(0);
  469. CellFormat cf = CellFormat.getInstance("[>=100]0.00;[>=10]0.000;0.0000");
  470. cell.setCellValue(100);
  471. assertEquals("100.00", cf.apply(cell).text);
  472. cell.setCellValue(10);
  473. assertEquals("10.000", cf.apply(cell).text);
  474. cell.setCellValue(0);
  475. assertEquals("0.0000", cf.apply(cell).text);
  476. cell.setCellValue(-10);
  477. assertEquals("-10.0000", cf.apply(cell).text);
  478. cell.setCellValue("abc");
  479. assertEquals("abc", cf.apply(cell).text);
  480. wb.close();
  481. }
  482. @Test
  483. public void testApplyFormatHasThreePartsFirstIsDateFirstTwoHaveCondition() throws Exception {
  484. // Create a workbook, row and cell to test with
  485. Workbook wb = new HSSFWorkbook();
  486. Sheet sheet = wb.createSheet();
  487. Row row = sheet.createRow(0);
  488. Cell cell = row.createCell(0);
  489. CellFormat cf = CellFormat.getInstance("[>=100]0.00;[>=10]dd/mm/yyyy;0.0");
  490. cell.setCellValue(100);
  491. assertEquals("100.00", cf.apply(cell).text);
  492. cell.setCellValue(10);
  493. assertEquals("10/01/1900", cf.apply(cell).text);
  494. cell.setCellValue(0);
  495. assertEquals("0.0", cf.apply(cell).text);
  496. cell.setCellValue(-10);
  497. assertEquals("-10.0", cf.apply(cell).text);
  498. cell.setCellValue("abc");
  499. assertEquals("abc", cf.apply(cell).text);
  500. wb.close();
  501. }
  502. @Test
  503. public void testApplyFormatHasTwoPartsFirstHasConditionSecondIsGeneral() throws Exception {
  504. // Create a workbook, row and cell to test with
  505. Workbook wb = new HSSFWorkbook();
  506. Sheet sheet = wb.createSheet();
  507. Row row = sheet.createRow(0);
  508. Cell cell = row.createCell(0);
  509. CellFormat cf = CellFormat.getInstance("[>=100]0.00;General");
  510. cell.setCellValue(100);
  511. assertEquals("100.00", cf.apply(cell).text);
  512. cell.setCellValue(10);
  513. assertEquals("10", cf.apply(cell).text);
  514. cell.setCellValue(0);
  515. assertEquals("0", cf.apply(cell).text);
  516. cell.setCellValue(-10);
  517. assertEquals("-10", cf.apply(cell).text);
  518. cell.setCellValue("abc");
  519. assertEquals("abc", cf.apply(cell).text);
  520. wb.close();
  521. }
  522. @Test
  523. public void testApplyFormatHasThreePartsFirstTwoHaveConditionThirdIsGeneral() throws Exception {
  524. // Create a workbook, row and cell to test with
  525. Workbook wb = new HSSFWorkbook();
  526. Sheet sheet = wb.createSheet();
  527. Row row = sheet.createRow(0);
  528. Cell cell = row.createCell(0);
  529. CellFormat cf = CellFormat.getInstance("[>=100]0.00;[>=10]0.000;General");
  530. cell.setCellValue(100);
  531. assertEquals("100.00", cf.apply(cell).text);
  532. cell.setCellValue(10);
  533. assertEquals("10.000", cf.apply(cell).text);
  534. cell.setCellValue(0);
  535. assertEquals("0", cf.apply(cell).text);
  536. cell.setCellValue(-10);
  537. assertEquals("-10", cf.apply(cell).text);
  538. cell.setCellValue("abc");
  539. assertEquals("abc", cf.apply(cell).text);
  540. wb.close();
  541. }
  542. @Test
  543. public void testApplyFormatHasFourPartsFirstHasCondition() throws Exception {
  544. // Create a workbook, row and cell to test with
  545. Workbook wb = new HSSFWorkbook();
  546. Sheet sheet = wb.createSheet();
  547. Row row = sheet.createRow(0);
  548. Cell cell = row.createCell(0);
  549. CellFormat cf = CellFormat.getInstance("[>=100]0.00;0.000;0.0000;~~@~~");
  550. cell.setCellValue(100);
  551. assertEquals("100.00", cf.apply(cell).text);
  552. cell.setCellValue(10);
  553. assertEquals("10.0000", cf.apply(cell).text);
  554. cell.setCellValue(0.123456789012345);
  555. assertEquals("0.1235", cf.apply(cell).text);
  556. cell.setCellValue(0);
  557. assertEquals("0.0000", cf.apply(cell).text);
  558. // Second format part ('0.000') is used for negative numbers
  559. // so result does not have a minus sign
  560. cell.setCellValue(-10);
  561. assertEquals("10.000", cf.apply(cell).text);
  562. cell.setCellValue("abc");
  563. assertEquals("~~abc~~", cf.apply(cell).text);
  564. wb.close();
  565. }
  566. @Test
  567. public void testApplyFormatHasFourPartsSecondHasCondition() throws Exception {
  568. // Create a workbook, row and cell to test with
  569. Workbook wb = new HSSFWorkbook();
  570. Sheet sheet = wb.createSheet();
  571. Row row = sheet.createRow(0);
  572. Cell cell = row.createCell(0);
  573. CellFormat cf = CellFormat.getInstance("0.00;[>=100]0.000;0.0000;~~@~~");
  574. cell.setCellValue(100);
  575. assertEquals("100.00", cf.apply(cell).text);
  576. cell.setCellValue(10);
  577. assertEquals("10.00", cf.apply(cell).text);
  578. cell.setCellValue(0.123456789012345);
  579. assertEquals("0.12", cf.apply(cell).text);
  580. cell.setCellValue(0);
  581. assertEquals("0.0000", cf.apply(cell).text);
  582. cell.setCellValue(-10);
  583. assertEquals("-10.0000", cf.apply(cell).text);
  584. cell.setCellValue("abc");
  585. assertEquals("~~abc~~", cf.apply(cell).text);
  586. cell.setCellValue(true);
  587. assertEquals("~~TRUE~~", cf.apply(cell).text);
  588. wb.close();
  589. }
  590. @Test
  591. public void testApplyFormatHasFourPartsFirstTwoHaveCondition() throws Exception {
  592. // Create a workbook, row and cell to test with
  593. Workbook wb = new HSSFWorkbook();
  594. Sheet sheet = wb.createSheet();
  595. Row row = sheet.createRow(0);
  596. Cell cell = row.createCell(0);
  597. CellFormat cf = CellFormat.getInstance("[>=100]0.00;[>=10]0.000;0.0000;~~@~~");
  598. cell.setCellValue(100);
  599. assertEquals("100.00", cf.apply(cell).text);
  600. cell.setCellValue(10);
  601. assertEquals("10.000", cf.apply(cell).text);
  602. cell.setCellValue(0);
  603. assertEquals("0.0000", cf.apply(cell).text);
  604. cell.setCellValue(-10);
  605. assertEquals("-10.0000", cf.apply(cell).text);
  606. cell.setCellValue("abc");
  607. assertEquals("~~abc~~", cf.apply(cell).text);
  608. cell.setCellValue(true);
  609. assertEquals("~~TRUE~~", cf.apply(cell).text);
  610. wb.close();
  611. }
  612. /*
  613. * Test apply(Object value) with a number as parameter
  614. */
  615. @Test
  616. public void testApplyObjectNumber() {
  617. CellFormat cf1 = CellFormat.getInstance("0.000");
  618. assertEquals("1.235", cf1.apply(1.2345).text);
  619. assertEquals("-1.235", cf1.apply(-1.2345).text);
  620. CellFormat cf2 = CellFormat.getInstance("0.000;(0.000)");
  621. assertEquals("1.235", cf2.apply(1.2345).text);
  622. assertEquals("(1.235)", cf2.apply(-1.2345).text);
  623. CellFormat cf3 = CellFormat.getInstance("[>1]0.000;0.0000");
  624. assertEquals("1.235", cf3.apply(1.2345).text);
  625. assertEquals("-1.2345", cf3.apply(-1.2345).text);
  626. CellFormat cf4 = CellFormat.getInstance("0.000;[>1]0.0000");
  627. assertEquals("1.235", cf4.apply(1.2345).text);
  628. assertEquals(_255_POUND_SIGNS, cf4.apply(-1.2345).text);
  629. }
  630. /*
  631. * Test apply(Object value) with a Date as parameter
  632. */
  633. @Test
  634. public void testApplyObjectDate() throws ParseException {
  635. CellFormat cf1 = CellFormat.getInstance("m/d/yyyy");
  636. Date date1 = new SimpleDateFormat("M/d/y", Locale.ROOT).parse("01/11/2012");
  637. //assertEquals("1/11/2012", cf1.apply(date1).text);
  638. }
  639. @Test
  640. public void testApplyCellForDateFormatWithConditions() throws Exception {
  641. // Create a workbook, row and cell to test with
  642. Workbook wb = new HSSFWorkbook();
  643. Sheet sheet = wb.createSheet();
  644. Row row = sheet.createRow(0);
  645. Cell cell = row.createCell(0);
  646. CellFormat cf = CellFormat.getInstance("[<1]hh:mm:ss AM/PM;[>=1]dd/mm/yyyy hh:mm:ss AM/PM;General");
  647. cell.setCellValue(0.5);
  648. assertEquals("12:00:00 PM", cf.apply(cell).text);
  649. cell.setCellValue(1.5);
  650. assertEquals("01/01/1900 12:00:00 PM", cf.apply(cell).text);
  651. cell.setCellValue(-1);
  652. assertEquals(_255_POUND_SIGNS, cf.apply(cell).text);
  653. wb.close();
  654. }
  655. /*
  656. * Test apply(Object value) with a String as parameter
  657. */
  658. @Test
  659. public void testApplyObjectString() {
  660. CellFormat cf = CellFormat.getInstance("0.00");
  661. assertEquals("abc", cf.apply("abc").text);
  662. }
  663. /*
  664. * Test apply(Object value) with a Boolean as parameter
  665. */
  666. @Test
  667. public void testApplyObjectBoolean() {
  668. CellFormat cf1 = CellFormat.getInstance("0");
  669. CellFormat cf2 = CellFormat.getInstance("General");
  670. CellFormat cf3 = CellFormat.getInstance("@");
  671. assertEquals("TRUE", cf1.apply(true).text);
  672. assertEquals("FALSE", cf2.apply(false).text);
  673. assertEquals("TRUE", cf3.apply(true).text);
  674. }
  675. @Test
  676. public void testSimpleFractionFormat() throws IOException {
  677. CellFormat cf1 = CellFormat.getInstance("# ?/?");
  678. // Create a workbook, row and cell to test with
  679. Workbook wb = new HSSFWorkbook();
  680. try {
  681. Sheet sheet = wb.createSheet();
  682. Row row = sheet.createRow(0);
  683. Cell cell = row.createCell(0);
  684. cell.setCellValue(123456.6);
  685. //System.out.println(cf1.apply(cell).text);
  686. assertEquals("123456 3/5", cf1.apply(cell).text);
  687. } finally {
  688. wb.close();
  689. }
  690. }
  691. @Test
  692. public void testAccountingFormats() throws IOException {
  693. char pound = '\u00A3';
  694. char euro = '\u20AC';
  695. // Accounting -> 0 decimal places, default currency symbol
  696. String formatDft = "_-\"$\"* #,##0_-;\\-\"$\"* #,##0_-;_-\"$\"* \"-\"_-;_-@_-";
  697. // Accounting -> 0 decimal places, US currency symbol
  698. String formatUS = "_-[$$-409]* #,##0_ ;_-[$$-409]* -#,##0 ;_-[$$-409]* \"-\"_-;_-@_-";
  699. // Accounting -> 0 decimal places, UK currency symbol
  700. String formatUK = "_-[$"+pound+"-809]* #,##0_-;\\-[$"+pound+"-809]* #,##0_-;_-[$"+pound+"-809]* \"-\"??_-;_-@_-";
  701. // French style accounting, euro sign comes after not before
  702. String formatFR = "_-#,##0* [$"+euro+"-40C]_-;\\-#,##0* [$"+euro+"-40C]_-;_-\"-\"??* [$"+euro+"-40C] _-;_-@_-";
  703. // Has +ve, -ve and zero rules
  704. CellFormat cfDft = CellFormat.getInstance(formatDft);
  705. CellFormat cfUS = CellFormat.getInstance(formatUS);
  706. CellFormat cfUK = CellFormat.getInstance(formatUK);
  707. CellFormat cfFR = CellFormat.getInstance(formatFR);
  708. // For +ve numbers, should be Space + currency symbol + spaces + whole number with commas + space
  709. // (Except French, which is mostly reversed...)
  710. assertEquals(" $ 12 ", cfDft.apply(Double.valueOf(12.33)).text);
  711. assertEquals(" $ 12 ", cfUS.apply(Double.valueOf(12.33)).text);
  712. assertEquals(" "+pound+" 12 ", cfUK.apply(Double.valueOf(12.33)).text);
  713. assertEquals(" 12 "+euro+" ", cfFR.apply(Double.valueOf(12.33)).text);
  714. assertEquals(" $ 16,789 ", cfDft.apply(Double.valueOf(16789.2)).text);
  715. assertEquals(" $ 16,789 ", cfUS.apply(Double.valueOf(16789.2)).text);
  716. assertEquals(" "+pound+" 16,789 ", cfUK.apply(Double.valueOf(16789.2)).text);
  717. assertEquals(" 16,789 "+euro+" ", cfFR.apply(Double.valueOf(16789.2)).text);
  718. // For -ve numbers, gets a bit more complicated...
  719. assertEquals("-$ 12 ", cfDft.apply(Double.valueOf(-12.33)).text);
  720. assertEquals(" $ -12 ", cfUS.apply(Double.valueOf(-12.33)).text);
  721. assertEquals("-"+pound+" 12 ", cfUK.apply(Double.valueOf(-12.33)).text);
  722. assertEquals("-12 "+euro+" ", cfFR.apply(Double.valueOf(-12.33)).text);
  723. assertEquals("-$ 16,789 ", cfDft.apply(Double.valueOf(-16789.2)).text);
  724. assertEquals(" $ -16,789 ", cfUS.apply(Double.valueOf(-16789.2)).text);
  725. assertEquals("-"+pound+" 16,789 ", cfUK.apply(Double.valueOf(-16789.2)).text);
  726. assertEquals("-16,789 "+euro+" ", cfFR.apply(Double.valueOf(-16789.2)).text);
  727. // For zero, should be Space + currency symbol + spaces + Minus + spaces
  728. assertEquals(" $ - ", cfDft.apply(Double.valueOf(0)).text);
  729. // TODO Fix the exception this incorrectly triggers
  730. //assertEquals(" $ - ", cfUS.apply(Double.valueOf(0)).text);
  731. // TODO Fix these to not have an incorrect bonus 0 on the end
  732. //assertEquals(" "+pound+" - ", cfUK.apply(Double.valueOf(0)).text);
  733. //assertEquals(" - "+euro+" ", cfFR.apply(Double.valueOf(0)).text);
  734. }
  735. @Test
  736. public void testThreePartComplexFormat1() {
  737. // verify a rather complex format found e.g. in http://wahl.land-oberoesterreich.gv.at/Downloads/bp10.xls
  738. CellFormatPart posPart = new CellFormatPart("[$-F400]h:mm:ss\\ AM/PM");
  739. assertNotNull(posPart);
  740. assertEquals("1:00:12 AM", posPart.apply(new Date(12345)).text);
  741. CellFormatPart negPart = new CellFormatPart("[$-F40]h:mm:ss\\ AM/PM");
  742. assertNotNull(negPart);
  743. assertEquals("1:00:12 AM", posPart.apply(new Date(12345)).text);
  744. //assertNotNull(new CellFormatPart("_-* \"\"??_-;_-@_-"));
  745. CellFormat instance = CellFormat.getInstance("[$-F400]h:mm:ss\\ AM/PM;[$-F40]h:mm:ss\\ AM/PM;_-* \"\"??_-;_-@_-");
  746. assertNotNull(instance);
  747. assertEquals("1:00:12 AM", instance.apply(new Date(12345)).text);
  748. }
  749. @Test
  750. public void testThreePartComplexFormat2() {
  751. // verify a rather complex format found e.g. in http://wahl.land-oberoesterreich.gv.at/Downloads/bp10.xls
  752. CellFormatPart posPart = new CellFormatPart("dd/mm/yyyy");
  753. assertNotNull(posPart);
  754. assertEquals("01/01/1970", posPart.apply(new Date(12345)).text);
  755. CellFormatPart negPart = new CellFormatPart("dd/mm/yyyy");
  756. assertNotNull(negPart);
  757. assertEquals("01/01/1970", posPart.apply(new Date(12345)).text);
  758. //assertNotNull(new CellFormatPart("_-* \"\"??_-;_-@_-"));
  759. CellFormat instance = CellFormat.getInstance("dd/mm/yyyy;dd/mm/yyyy;_-* \"\"??_-;_-@_-");
  760. assertNotNull(instance);
  761. assertEquals("01/01/1970", instance.apply(new Date(12345)).text);
  762. }
  763. }