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.

TestXSSFDataFormat.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.xssf.usermodel;
  16. import static org.junit.Assert.*;
  17. import java.io.IOException;
  18. import org.apache.poi.hssf.HSSFTestDataSamples;
  19. import org.apache.poi.ss.formula.ConditionalFormattingEvaluator;
  20. import org.apache.poi.ss.formula.WorkbookEvaluatorProvider;
  21. import org.apache.poi.ss.usermodel.BaseTestDataFormat;
  22. import org.apache.poi.ss.usermodel.BuiltinFormats;
  23. import org.apache.poi.ss.usermodel.Cell;
  24. import org.apache.poi.ss.usermodel.CellStyle;
  25. import org.apache.poi.ss.usermodel.CellType;
  26. import org.apache.poi.ss.usermodel.DataFormat;
  27. import org.apache.poi.ss.usermodel.DataFormatter;
  28. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  29. import org.apache.poi.ss.usermodel.Workbook;
  30. import org.apache.poi.ss.util.CellReference;
  31. import org.apache.poi.xssf.XSSFITestDataProvider;
  32. import org.apache.poi.xssf.XSSFTestDataSamples;
  33. import org.junit.Test;
  34. /**
  35. * Tests for {@link XSSFDataFormat}
  36. */
  37. public final class TestXSSFDataFormat extends BaseTestDataFormat {
  38. public TestXSSFDataFormat() {
  39. super(XSSFITestDataProvider.instance);
  40. }
  41. /**
  42. * [Bug 49928] formatCellValue returns incorrect value for \u00a3 formatted cells
  43. */
  44. @Override
  45. @Test
  46. public void test49928() throws IOException {
  47. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("49928.xlsx");
  48. doTest49928Core(wb);
  49. DataFormat dataFormat = wb.createDataFormat();
  50. // As of 2015-12-27, there is no way to override a built-in number format with POI XSSFWorkbook
  51. // 49928.xlsx has been saved with a poundFmt that overrides the default value (dollar)
  52. short poundFmtIdx = wb.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getDataFormat();
  53. assertEquals(poundFmtIdx, dataFormat.getFormat(poundFmt));
  54. // now create a custom format with Pound (\u00a3)
  55. String customFmt = "\u00a3##.00[Yellow]";
  56. assertNotBuiltInFormat(customFmt);
  57. short customFmtIdx = dataFormat.getFormat(customFmt);
  58. assertTrue(customFmtIdx >= BuiltinFormats.FIRST_USER_DEFINED_FORMAT_INDEX);
  59. assertEquals(customFmt, dataFormat.getFormat(customFmtIdx));
  60. wb.close();
  61. }
  62. /**
  63. * [Bug 58532] Handle formats that go numnum, numK, numM etc
  64. */
  65. @Override
  66. @Test
  67. public void test58532() throws IOException {
  68. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("FormatKM.xlsx");
  69. doTest58532Core(wb);
  70. wb.close();
  71. }
  72. /**
  73. * [Bug 58778] Built-in number formats can be overridden with XSSFDataFormat.putFormat(int id, String fmt)
  74. */
  75. @Test
  76. public void test58778() throws IOException {
  77. XSSFWorkbook wb1 = new XSSFWorkbook();
  78. Cell cell = wb1.createSheet("bug58778").createRow(0).createCell(0);
  79. cell.setCellValue(5.25);
  80. CellStyle style = wb1.createCellStyle();
  81. XSSFDataFormat dataFormat = wb1.createDataFormat();
  82. short poundFmtIdx = 6;
  83. dataFormat.putFormat(poundFmtIdx, poundFmt);
  84. style.setDataFormat(poundFmtIdx);
  85. cell.setCellStyle(style);
  86. // Cell should appear as "<poundsymbol>5"
  87. XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutCloseAndReadBack(wb1);
  88. cell = wb2.getSheet("bug58778").getRow(0).getCell(0);
  89. assertEquals(5.25, cell.getNumericCellValue(), 0);
  90. style = cell.getCellStyle();
  91. assertEquals(poundFmt, style.getDataFormatString());
  92. assertEquals(poundFmtIdx, style.getDataFormat());
  93. // manually check the file to make sure the cell is rendered as "<poundsymbol>5"
  94. // Verified with LibreOffice 4.2.8.2 on 2015-12-28
  95. wb2.close();
  96. wb1.close();
  97. }
  98. @Test
  99. public void testConditionalFormattingEvaluation() throws IOException {
  100. final Workbook wb = XSSFTestDataSamples.openSampleWorkbook("61060-conditional-number-formatting.xlsx");
  101. final DataFormatter formatter = new DataFormatter();
  102. final FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  103. final ConditionalFormattingEvaluator cfEvaluator = new ConditionalFormattingEvaluator(wb, (WorkbookEvaluatorProvider) evaluator);
  104. CellReference ref = new CellReference("A1");
  105. Cell cell = wb.getSheetAt(0).getRow(ref.getRow()).getCell(ref.getCol());
  106. assertEquals("0.10", formatter.formatCellValue(cell, evaluator, cfEvaluator));
  107. // verify cell format without the conditional rule applied
  108. assertEquals("0.1", formatter.formatCellValue(cell, evaluator));
  109. ref = new CellReference("A3");
  110. cell = wb.getSheetAt(0).getRow(ref.getRow()).getCell(ref.getCol());
  111. assertEquals("-2.00E+03", formatter.formatCellValue(cell, evaluator, cfEvaluator));
  112. // verify cell format without the conditional rule applied
  113. assertEquals("-2000", formatter.formatCellValue(cell, evaluator));
  114. ref = new CellReference("A4");
  115. cell = wb.getSheetAt(0).getRow(ref.getRow()).getCell(ref.getCol());
  116. assertEquals("100", formatter.formatCellValue(cell, evaluator, cfEvaluator));
  117. ref = new CellReference("A5");
  118. cell = wb.getSheetAt(0).getRow(ref.getRow()).getCell(ref.getCol());
  119. assertEquals("$1,000", formatter.formatCellValue(cell, evaluator, cfEvaluator));
  120. // verify cell format without the conditional rule applied
  121. assertEquals("1000", formatter.formatCellValue(cell, evaluator));
  122. wb.close();
  123. }
  124. }