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.

TestCellStyle.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import java.util.Calendar;
  20. import java.util.Date;
  21. import org.apache.poi.hssf.HSSFTestDataSamples;
  22. import org.apache.poi.ss.usermodel.BorderStyle;
  23. import org.apache.poi.ss.usermodel.Cell;
  24. import org.apache.poi.ss.usermodel.CellStyle;
  25. import org.apache.poi.ss.usermodel.Font;
  26. import org.apache.poi.ss.usermodel.Row;
  27. import org.apache.poi.ss.usermodel.Sheet;
  28. import org.apache.poi.ss.usermodel.Workbook;
  29. import org.apache.poi.util.LocaleUtil;
  30. import org.apache.poi.util.TempFile;
  31. import org.junit.Test;
  32. import junit.framework.TestCase;
  33. /**
  34. * Class to test cell styling functionality
  35. */
  36. public final class TestCellStyle extends TestCase {
  37. private static HSSFWorkbook openSample(String sampleFileName) {
  38. return HSSFTestDataSamples.openSampleWorkbook(sampleFileName);
  39. }
  40. /**
  41. * TEST NAME: Test Write Sheet Font <P>
  42. * OBJECTIVE: Test that HSSF can create a simple spreadsheet with numeric and string values and styled with fonts.<P>
  43. * SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects
  44. * Last row, first row is tested against the correct values (99,0).<P>
  45. * FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good.
  46. * HSSFSheet last row or first row is incorrect. <P>
  47. *
  48. */
  49. public void testWriteSheetFont() throws IOException{
  50. File file = TempFile.createTempFile("testWriteSheetFont",
  51. ".xls");
  52. FileOutputStream out = new FileOutputStream(file);
  53. HSSFWorkbook wb = new HSSFWorkbook();
  54. HSSFSheet s = wb.createSheet();
  55. HSSFRow r = null;
  56. HSSFCell c = null;
  57. HSSFFont fnt = wb.createFont();
  58. HSSFCellStyle cs = wb.createCellStyle();
  59. fnt.setColor(HSSFFont.COLOR_RED);
  60. fnt.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  61. cs.setFont(fnt);
  62. for (int rownum = 0; rownum < 100; rownum++) {
  63. r = s.createRow(rownum);
  64. for (int cellnum = 0; cellnum < 50; cellnum += 2) {
  65. c = r.createCell(cellnum);
  66. c.setCellValue(rownum * 10000 + cellnum
  67. + ((( double ) rownum / 1000)
  68. + (( double ) cellnum / 10000)));
  69. c = r.createCell(cellnum + 1);
  70. c.setCellValue("TEST");
  71. c.setCellStyle(cs);
  72. }
  73. }
  74. wb.write(out);
  75. out.close();
  76. SanityChecker sanityChecker = new SanityChecker();
  77. sanityChecker.checkHSSFWorkbook(wb);
  78. assertEquals("LAST ROW == 99", 99, s.getLastRowNum());
  79. assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum());
  80. // assert((s.getLastRowNum() == 99));
  81. }
  82. /**
  83. * Tests that is creating a file with a date or an calendar works correctly.
  84. */
  85. public void testDataStyle() throws IOException {
  86. File file = TempFile.createTempFile("testWriteSheetStyleDate",
  87. ".xls");
  88. FileOutputStream out = new FileOutputStream(file);
  89. HSSFWorkbook wb = new HSSFWorkbook();
  90. HSSFSheet s = wb.createSheet();
  91. HSSFCellStyle cs = wb.createCellStyle();
  92. HSSFRow row = s.createRow(0);
  93. // with Date:
  94. HSSFCell cell = row.createCell(1);
  95. cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
  96. cell.setCellStyle(cs);
  97. cell.setCellValue(new Date());
  98. // with Calendar:
  99. cell = row.createCell(2);
  100. cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
  101. cell.setCellStyle(cs);
  102. Calendar cal = LocaleUtil.getLocaleCalendar();
  103. cell.setCellValue(cal);
  104. wb.write(out);
  105. out.close();
  106. SanityChecker sanityChecker = new SanityChecker();
  107. sanityChecker.checkHSSFWorkbook(wb);
  108. assertEquals("LAST ROW ", 0, s.getLastRowNum());
  109. assertEquals("FIRST ROW ", 0, s.getFirstRowNum());
  110. }
  111. public void testHashEquals() throws IOException {
  112. HSSFWorkbook wb = new HSSFWorkbook();
  113. HSSFSheet s = wb.createSheet();
  114. HSSFCellStyle cs1 = wb.createCellStyle();
  115. HSSFCellStyle cs2 = wb.createCellStyle();
  116. HSSFRow row = s.createRow(0);
  117. HSSFCell cell1 = row.createCell(1);
  118. HSSFCell cell2 = row.createCell(2);
  119. cs1.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
  120. cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/dd/yy"));
  121. cell1.setCellStyle(cs1);
  122. cell1.setCellValue(new Date());
  123. cell2.setCellStyle(cs2);
  124. cell2.setCellValue(new Date());
  125. assertEquals(cs1.hashCode(), cs1.hashCode());
  126. assertEquals(cs2.hashCode(), cs2.hashCode());
  127. assertTrue(cs1.equals(cs1));
  128. assertTrue(cs2.equals(cs2));
  129. // Change cs1, hash will alter
  130. int hash1 = cs1.hashCode();
  131. cs1.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/dd/yy"));
  132. assertFalse(hash1 == cs1.hashCode());
  133. wb.close();
  134. }
  135. /**
  136. * TEST NAME: Test Write Sheet Style <P>
  137. * OBJECTIVE: Test that HSSF can create a simple spreadsheet with numeric and string values and styled with colors
  138. * and borders.<P>
  139. * SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects
  140. * Last row, first row is tested against the correct values (99,0).<P>
  141. * FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good.
  142. * HSSFSheet last row or first row is incorrect. <P>
  143. *
  144. */
  145. public void testWriteSheetStyle() throws IOException {
  146. File file = TempFile.createTempFile("testWriteSheetStyle",
  147. ".xls");
  148. FileOutputStream out = new FileOutputStream(file);
  149. HSSFWorkbook wb = new HSSFWorkbook();
  150. HSSFSheet s = wb.createSheet();
  151. HSSFRow r = null;
  152. HSSFCell c = null;
  153. HSSFFont fnt = wb.createFont();
  154. HSSFCellStyle cs = wb.createCellStyle();
  155. HSSFCellStyle cs2 = wb.createCellStyle();
  156. cs.setBorderBottom(BorderStyle.THIN);
  157. cs.setBorderLeft(BorderStyle.THIN);
  158. cs.setBorderRight(BorderStyle.THIN);
  159. cs.setBorderTop(BorderStyle.THIN);
  160. cs.setFillForegroundColor(( short ) 0xA);
  161. cs.setFillPattern(( short ) 1);
  162. fnt.setColor(( short ) 0xf);
  163. fnt.setItalic(true);
  164. cs2.setFillForegroundColor(( short ) 0x0);
  165. cs2.setFillPattern(( short ) 1);
  166. cs2.setFont(fnt);
  167. for (int rownum = 0; rownum < 100; rownum++) {
  168. r = s.createRow(rownum);
  169. for (int cellnum = 0; cellnum < 50; cellnum += 2) {
  170. c = r.createCell(cellnum);
  171. c.setCellValue(rownum * 10000 + cellnum
  172. + ((( double ) rownum / 1000)
  173. + (( double ) cellnum / 10000)));
  174. c.setCellStyle(cs);
  175. c = r.createCell(cellnum + 1);
  176. c.setCellValue("TEST");
  177. c.setCellStyle(cs2);
  178. }
  179. }
  180. wb.write(out);
  181. out.close();
  182. SanityChecker sanityChecker = new SanityChecker();
  183. sanityChecker.checkHSSFWorkbook(wb);
  184. assertEquals("LAST ROW == 99", 99, s.getLastRowNum());
  185. assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum());
  186. }
  187. /**
  188. * Cloning one HSSFCellStyle onto Another, same
  189. * HSSFWorkbook
  190. */
  191. public void testCloneStyleSameWB() {
  192. HSSFWorkbook wb = new HSSFWorkbook();
  193. HSSFFont fnt = wb.createFont();
  194. fnt.setFontName("TestingFont");
  195. assertEquals(5, wb.getNumberOfFonts());
  196. HSSFCellStyle orig = wb.createCellStyle();
  197. orig.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
  198. orig.setFont(fnt);
  199. orig.setDataFormat((short)18);
  200. assertTrue(HSSFCellStyle.ALIGN_RIGHT == orig.getAlignment());
  201. assertTrue(fnt == orig.getFont(wb));
  202. assertTrue(18 == orig.getDataFormat());
  203. HSSFCellStyle clone = wb.createCellStyle();
  204. assertFalse(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment());
  205. assertFalse(fnt == clone.getFont(wb));
  206. assertFalse(18 == clone.getDataFormat());
  207. clone.cloneStyleFrom(orig);
  208. assertTrue(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment());
  209. assertTrue(fnt == clone.getFont(wb));
  210. assertTrue(18 == clone.getDataFormat());
  211. assertEquals(5, wb.getNumberOfFonts());
  212. }
  213. /**
  214. * Cloning one HSSFCellStyle onto Another, across
  215. * two different HSSFWorkbooks
  216. */
  217. public void testCloneStyleDiffWB() {
  218. HSSFWorkbook wbOrig = new HSSFWorkbook();
  219. HSSFFont fnt = wbOrig.createFont();
  220. fnt.setFontName("TestingFont");
  221. assertEquals(5, wbOrig.getNumberOfFonts());
  222. HSSFDataFormat fmt = wbOrig.createDataFormat();
  223. fmt.getFormat("MadeUpOne");
  224. fmt.getFormat("MadeUpTwo");
  225. HSSFCellStyle orig = wbOrig.createCellStyle();
  226. orig.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
  227. orig.setFont(fnt);
  228. orig.setDataFormat(fmt.getFormat("Test##"));
  229. assertTrue(HSSFCellStyle.ALIGN_RIGHT == orig.getAlignment());
  230. assertTrue(fnt == orig.getFont(wbOrig));
  231. assertTrue(fmt.getFormat("Test##") == orig.getDataFormat());
  232. // Now a style on another workbook
  233. HSSFWorkbook wbClone = new HSSFWorkbook();
  234. assertEquals(4, wbClone.getNumberOfFonts());
  235. HSSFDataFormat fmtClone = wbClone.createDataFormat();
  236. HSSFCellStyle clone = wbClone.createCellStyle();
  237. assertEquals(4, wbClone.getNumberOfFonts());
  238. assertFalse(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment());
  239. assertFalse("TestingFont" == clone.getFont(wbClone).getFontName());
  240. clone.cloneStyleFrom(orig);
  241. assertTrue(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment());
  242. assertTrue("TestingFont" == clone.getFont(wbClone).getFontName());
  243. assertTrue(fmtClone.getFormat("Test##") == clone.getDataFormat());
  244. assertFalse(fmtClone.getFormat("Test##") == fmt.getFormat("Test##"));
  245. assertEquals(5, wbClone.getNumberOfFonts());
  246. }
  247. public void testStyleNames() {
  248. HSSFWorkbook wb = openSample("WithExtendedStyles.xls");
  249. HSSFSheet s = wb.getSheetAt(0);
  250. HSSFCell c1 = s.getRow(0).getCell(0);
  251. HSSFCell c2 = s.getRow(1).getCell(0);
  252. HSSFCell c3 = s.getRow(2).getCell(0);
  253. HSSFCellStyle cs1 = c1.getCellStyle();
  254. HSSFCellStyle cs2 = c2.getCellStyle();
  255. HSSFCellStyle cs3 = c3.getCellStyle();
  256. assertNotNull(cs1);
  257. assertNotNull(cs2);
  258. assertNotNull(cs3);
  259. // Check we got the styles we'd expect
  260. assertEquals(10, cs1.getFont(wb).getFontHeightInPoints());
  261. assertEquals(9, cs2.getFont(wb).getFontHeightInPoints());
  262. assertEquals(12, cs3.getFont(wb).getFontHeightInPoints());
  263. assertEquals(15, cs1.getIndex());
  264. assertEquals(23, cs2.getIndex());
  265. assertEquals(24, cs3.getIndex());
  266. assertNull(cs1.getParentStyle());
  267. assertNotNull(cs2.getParentStyle());
  268. assertNotNull(cs3.getParentStyle());
  269. assertEquals(21, cs2.getParentStyle().getIndex());
  270. assertEquals(22, cs3.getParentStyle().getIndex());
  271. // Now check we can get style records for
  272. // the parent ones
  273. assertNull(wb.getWorkbook().getStyleRecord(15));
  274. assertNull(wb.getWorkbook().getStyleRecord(23));
  275. assertNull(wb.getWorkbook().getStyleRecord(24));
  276. assertNotNull(wb.getWorkbook().getStyleRecord(21));
  277. assertNotNull(wb.getWorkbook().getStyleRecord(22));
  278. // Now check the style names
  279. assertEquals(null, cs1.getUserStyleName());
  280. assertEquals(null, cs2.getUserStyleName());
  281. assertEquals(null, cs3.getUserStyleName());
  282. assertEquals("style1", cs2.getParentStyle().getUserStyleName());
  283. assertEquals("style2", cs3.getParentStyle().getUserStyleName());
  284. // now apply a named style to a new cell
  285. HSSFCell c4 = s.getRow(0).createCell(1);
  286. c4.setCellStyle(cs2);
  287. assertEquals("style1", c4.getCellStyle().getParentStyle().getUserStyleName());
  288. }
  289. public void testGetSetBorderHair() {
  290. HSSFWorkbook wb = openSample("55341_CellStyleBorder.xls");
  291. HSSFSheet s = wb.getSheetAt(0);
  292. HSSFCellStyle cs;
  293. cs = s.getRow(0).getCell(0).getCellStyle();
  294. assertEquals(BorderStyle.HAIR, cs.getBorderRight());
  295. cs = s.getRow(1).getCell(1).getCellStyle();
  296. assertEquals(BorderStyle.DOTTED, cs.getBorderRight());
  297. cs = s.getRow(2).getCell(2).getCellStyle();
  298. assertEquals(BorderStyle.DASH_DOT_DOT, cs.getBorderRight());
  299. cs = s.getRow(3).getCell(3).getCellStyle();
  300. assertEquals(BorderStyle.DASHED, cs.getBorderRight());
  301. cs = s.getRow(4).getCell(4).getCellStyle();
  302. assertEquals(BorderStyle.THIN, cs.getBorderRight());
  303. cs = s.getRow(5).getCell(5).getCellStyle();
  304. assertEquals(BorderStyle.MEDIUM_DASH_DOT_DOT, cs.getBorderRight());
  305. cs = s.getRow(6).getCell(6).getCellStyle();
  306. assertEquals(BorderStyle.SLANTED_DASH_DOT, cs.getBorderRight());
  307. cs = s.getRow(7).getCell(7).getCellStyle();
  308. assertEquals(BorderStyle.MEDIUM_DASH_DOT, cs.getBorderRight());
  309. cs = s.getRow(8).getCell(8).getCellStyle();
  310. assertEquals(BorderStyle.MEDIUM_DASHED, cs.getBorderRight());
  311. cs = s.getRow(9).getCell(9).getCellStyle();
  312. assertEquals(BorderStyle.MEDIUM, cs.getBorderRight());
  313. cs = s.getRow(10).getCell(10).getCellStyle();
  314. assertEquals(BorderStyle.THICK, cs.getBorderRight());
  315. cs = s.getRow(11).getCell(11).getCellStyle();
  316. assertEquals(BorderStyle.DOUBLE, cs.getBorderRight());
  317. }
  318. public void testShrinkToFit() {
  319. // Existing file
  320. HSSFWorkbook wb = openSample("ShrinkToFit.xls");
  321. HSSFSheet s = wb.getSheetAt(0);
  322. HSSFRow r = s.getRow(0);
  323. HSSFCellStyle cs = r.getCell(0).getCellStyle();
  324. assertEquals(true, cs.getShrinkToFit());
  325. // New file
  326. HSSFWorkbook wbOrig = new HSSFWorkbook();
  327. s = wbOrig.createSheet();
  328. r = s.createRow(0);
  329. cs = wbOrig.createCellStyle();
  330. cs.setShrinkToFit(false);
  331. r.createCell(0).setCellStyle(cs);
  332. cs = wbOrig.createCellStyle();
  333. cs.setShrinkToFit(true);
  334. r.createCell(1).setCellStyle(cs);
  335. // Write out, read, and check
  336. wb = HSSFTestDataSamples.writeOutAndReadBack(wbOrig);
  337. s = wb.getSheetAt(0);
  338. r = s.getRow(0);
  339. assertEquals(false, r.getCell(0).getCellStyle().getShrinkToFit());
  340. assertEquals(true, r.getCell(1).getCellStyle().getShrinkToFit());
  341. }
  342. private static class CellFormatBugExample extends Thread {
  343. private String fileName;
  344. private Throwable exception = null;
  345. public CellFormatBugExample(String fileName) {
  346. this.fileName = fileName;
  347. }
  348. @Override
  349. public void run() {
  350. try {
  351. for(int i = 0;i< 10;i++) {
  352. Workbook wb = HSSFTestDataSamples.openSampleWorkbook(fileName);
  353. Sheet sheet = wb.getSheetAt(0);
  354. for (Row row : sheet) {
  355. for (Integer idxCell = 0; idxCell < row.getLastCellNum(); idxCell++) {
  356. Cell cell = row.getCell(idxCell);
  357. cell.getCellStyle().getDataFormatString();
  358. if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
  359. boolean isDate = HSSFDateUtil.isCellDateFormatted(cell);
  360. if (idxCell > 0 && isDate) {
  361. fail("cell " + idxCell + " is not a date: " + idxCell.toString());
  362. }
  363. }
  364. }
  365. }
  366. }
  367. } catch (Throwable e) {
  368. exception = e;
  369. }
  370. }
  371. public Throwable getException() {
  372. return exception;
  373. }
  374. };
  375. public void test56563() throws Throwable {
  376. CellFormatBugExample threadA = new CellFormatBugExample("56563a.xls");
  377. threadA.start();
  378. CellFormatBugExample threadB = new CellFormatBugExample("56563b.xls");
  379. threadB.start();
  380. threadA.join();
  381. threadB.join();
  382. if(threadA.getException() != null) {
  383. throw threadA.getException();
  384. }
  385. if(threadB.getException() != null) {
  386. throw threadB.getException();
  387. }
  388. }
  389. public void test56959() throws IOException {
  390. Workbook wb = new HSSFWorkbook();
  391. Sheet sheet = wb.createSheet("somesheet");
  392. Row row = sheet.createRow(0);
  393. // Create a new font and alter it.
  394. Font font = wb.createFont();
  395. font.setFontHeightInPoints((short)24);
  396. font.setFontName("Courier New");
  397. font.setItalic(true);
  398. font.setStrikeout(true);
  399. font.setColor(Font.COLOR_RED);
  400. CellStyle style = wb.createCellStyle();
  401. style.setBorderBottom(BorderStyle.DOTTED);
  402. style.setFont(font);
  403. Cell cell = row.createCell(0);
  404. cell.setCellStyle(style);
  405. cell.setCellValue("testtext");
  406. Cell newCell = row.createCell(1);
  407. newCell.setCellStyle(style);
  408. newCell.setCellValue("2testtext2");
  409. CellStyle newStyle = newCell.getCellStyle();
  410. assertEquals(BorderStyle.DOTTED, newStyle.getBorderBottom());
  411. assertEquals(Font.COLOR_RED, ((HSSFCellStyle)newStyle).getFont(wb).getColor());
  412. // OutputStream out = new FileOutputStream("/tmp/56959.xls");
  413. // try {
  414. // wb.write(out);
  415. // } finally {
  416. // out.close();
  417. // }
  418. }
  419. @Test
  420. public void test58043() throws IOException {
  421. HSSFWorkbook wb = new HSSFWorkbook();
  422. HSSFCellStyle cellStyle = wb.createCellStyle();
  423. assertEquals(0, cellStyle.getRotation());
  424. cellStyle.setRotation((short)89);
  425. assertEquals(89, cellStyle.getRotation());
  426. cellStyle.setRotation((short)90);
  427. assertEquals(90, cellStyle.getRotation());
  428. cellStyle.setRotation((short)-1);
  429. assertEquals(-1, cellStyle.getRotation());
  430. cellStyle.setRotation((short)-89);
  431. assertEquals(-89, cellStyle.getRotation());
  432. cellStyle.setRotation((short)-90);
  433. assertEquals(-90, cellStyle.getRotation());
  434. cellStyle.setRotation((short)-89);
  435. assertEquals(-89, cellStyle.getRotation());
  436. // values above 90 are mapped to the correct values for compatibility between HSSF and XSSF
  437. cellStyle.setRotation((short)179);
  438. assertEquals(-89, cellStyle.getRotation());
  439. cellStyle.setRotation((short)180);
  440. assertEquals(-90, cellStyle.getRotation());
  441. wb.close();
  442. }
  443. }