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.

TestXSSFCell.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 org.apache.poi.ss.usermodel.*;
  17. import org.apache.poi.xssf.XSSFITestDataProvider;
  18. import org.apache.poi.xssf.model.SharedStringsTable;
  19. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
  20. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
  21. /**
  22. * @author Yegor Kozlov
  23. */
  24. public final class TestXSSFCell extends BaseTestCell {
  25. public TestXSSFCell() {
  26. super(XSSFITestDataProvider.getInstance());
  27. }
  28. /**
  29. * Bug 47026: trouble changing cell type when workbook doesn't contain
  30. * Shared String Table
  31. */
  32. public void test47026_1() {
  33. Workbook source = _testDataProvider.openSampleWorkbook("47026.xlsm");
  34. Sheet sheet = source.getSheetAt(0);
  35. Row row = sheet.getRow(0);
  36. Cell cell = row.getCell(0);
  37. cell.setCellType(Cell.CELL_TYPE_STRING);
  38. cell.setCellValue("456");
  39. }
  40. public void test47026_2() {
  41. Workbook source = _testDataProvider.openSampleWorkbook("47026.xlsm");
  42. Sheet sheet = source.getSheetAt(0);
  43. Row row = sheet.getRow(0);
  44. Cell cell = row.getCell(0);
  45. cell.setCellFormula(null);
  46. cell.setCellValue("456");
  47. }
  48. /**
  49. * Test that we can read inline strings that are expressed directly in the cell definition
  50. * instead of implementing the shared string table.
  51. *
  52. * Some programs, for example, Microsoft Excel Driver for .xlsx insert inline string
  53. * instead of using the shared string table. See bug 47206
  54. */
  55. public void testInlineString() {
  56. XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.openSampleWorkbook("xlsx-jdbc.xlsx");
  57. XSSFSheet sheet = wb.getSheetAt(0);
  58. XSSFRow row = sheet.getRow(1);
  59. XSSFCell cell_0 = row.getCell(0);
  60. assertEquals(STCellType.INT_INLINE_STR, cell_0.getCTCell().getT().intValue());
  61. assertTrue(cell_0.getCTCell().isSetIs());
  62. assertEquals("A Very large string in column 1 AAAAAAAAAAAAAAAAAAAAA", cell_0.getStringCellValue());
  63. XSSFCell cell_1 = row.getCell(1);
  64. assertEquals(STCellType.INT_INLINE_STR, cell_1.getCTCell().getT().intValue());
  65. assertTrue(cell_1.getCTCell().isSetIs());
  66. assertEquals("foo", cell_1.getStringCellValue());
  67. XSSFCell cell_2 = row.getCell(2);
  68. assertEquals(STCellType.INT_INLINE_STR, cell_2.getCTCell().getT().intValue());
  69. assertTrue(cell_2.getCTCell().isSetIs());
  70. assertEquals("bar", row.getCell(2).getStringCellValue());
  71. }
  72. /**
  73. * Bug 47278 - xsi:nil attribute for <t> tag caused Excel 2007 to fail to open workbook
  74. */
  75. public void test47278() {
  76. XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.createWorkbook();
  77. XSSFSheet sheet = wb.createSheet();
  78. XSSFRow row = sheet.createRow(0);
  79. SharedStringsTable sst = wb.getSharedStringSource();
  80. assertEquals(0, sst.getCount());
  81. //case 1. cell.setCellValue(new XSSFRichTextString((String)null));
  82. XSSFCell cell_0 = row.createCell(0);
  83. XSSFRichTextString str = new XSSFRichTextString((String)null);
  84. assertNull(str.getString());
  85. cell_0.setCellValue(str);
  86. assertEquals(0, sst.getCount());
  87. assertEquals(XSSFCell.CELL_TYPE_BLANK, cell_0.getCellType());
  88. //case 2. cell.setCellValue((String)null);
  89. XSSFCell cell_1 = row.createCell(1);
  90. cell_1.setCellValue((String)null);
  91. assertEquals(0, sst.getCount());
  92. assertEquals(XSSFCell.CELL_TYPE_BLANK, cell_1.getCellType());
  93. }
  94. public void testFormulaString() {
  95. XSSFWorkbook wb = new XSSFWorkbook();
  96. XSSFCell cell = wb.createSheet().createRow(0).createCell(0);
  97. CTCell ctCell = cell.getCTCell(); //low-level bean holding cell's xml
  98. cell.setCellFormula("A2");
  99. assertEquals(XSSFCell.CELL_TYPE_FORMULA, cell.getCellType());
  100. //the value is not set and cell's type='N' which means blank
  101. assertEquals(STCellType.N, ctCell.getT());
  102. //set cached formula value
  103. cell.setCellValue("t='str'");
  104. //we are still of 'formula' type
  105. assertEquals(XSSFCell.CELL_TYPE_FORMULA, cell.getCellType());
  106. //cached formula value is set and cell's type='STR'
  107. assertEquals(STCellType.STR, ctCell.getT());
  108. assertEquals("t='str'", cell.getStringCellValue());
  109. //now remove the formula, the cached formula result remains
  110. cell.setCellFormula(null);
  111. assertEquals(XSSFCell.CELL_TYPE_STRING, cell.getCellType());
  112. assertEquals(STCellType.STR, ctCell.getT());
  113. //the line below failed prior to fix of Bug #47889
  114. assertEquals("t='str'", cell.getStringCellValue());
  115. //revert to a blank cell
  116. cell.setCellValue((String)null);
  117. assertEquals(XSSFCell.CELL_TYPE_BLANK, cell.getCellType());
  118. assertEquals(STCellType.N, ctCell.getT());
  119. assertEquals("", cell.getStringCellValue());
  120. }
  121. /**
  122. * Bug 47889: problems when calling XSSFCell.getStringCellValue() on a workbook created in Gnumeric
  123. */
  124. public void test47889() {
  125. XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.openSampleWorkbook("47889.xlsx");
  126. XSSFSheet sh = wb.getSheetAt(0);
  127. XSSFCell cell;
  128. //try a string cell
  129. cell = sh.getRow(0).getCell(0);
  130. assertEquals(XSSFCell.CELL_TYPE_STRING, cell.getCellType());
  131. assertEquals("a", cell.getStringCellValue());
  132. assertEquals("a", cell.toString());
  133. //Gnumeric produces spreadsheets without styles
  134. //make sure we return null for that instead of throwing OutOfBounds
  135. assertEquals(null, cell.getCellStyle());
  136. //try a numeric cell
  137. cell = sh.getRow(1).getCell(0);
  138. assertEquals(XSSFCell.CELL_TYPE_NUMERIC, cell.getCellType());
  139. assertEquals(1.0, cell.getNumericCellValue());
  140. assertEquals("1.0", cell.toString());
  141. //Gnumeric produces spreadsheets without styles
  142. //make sure we return null for that instead of throwing OutOfBounds
  143. assertEquals(null, cell.getCellStyle());
  144. }
  145. }