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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. /**
  24. * @author Yegor Kozlov
  25. */
  26. public final class TestXSSFCell extends BaseTestCell {
  27. public TestXSSFCell() {
  28. super(XSSFITestDataProvider.instance);
  29. }
  30. /**
  31. * Bug 47026: trouble changing cell type when workbook doesn't contain
  32. * Shared String Table
  33. */
  34. public void test47026_1() {
  35. Workbook source = _testDataProvider.openSampleWorkbook("47026.xlsm");
  36. Sheet sheet = source.getSheetAt(0);
  37. Row row = sheet.getRow(0);
  38. Cell cell = row.getCell(0);
  39. cell.setCellType(Cell.CELL_TYPE_STRING);
  40. cell.setCellValue("456");
  41. }
  42. public void test47026_2() {
  43. Workbook source = _testDataProvider.openSampleWorkbook("47026.xlsm");
  44. Sheet sheet = source.getSheetAt(0);
  45. Row row = sheet.getRow(0);
  46. Cell cell = row.getCell(0);
  47. cell.setCellFormula(null);
  48. cell.setCellValue("456");
  49. }
  50. /**
  51. * Test that we can read inline strings that are expressed directly in the cell definition
  52. * instead of implementing the shared string table.
  53. *
  54. * Some programs, for example, Microsoft Excel Driver for .xlsx insert inline string
  55. * instead of using the shared string table. See bug 47206
  56. */
  57. public void testInlineString() {
  58. XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.openSampleWorkbook("xlsx-jdbc.xlsx");
  59. XSSFSheet sheet = wb.getSheetAt(0);
  60. XSSFRow row = sheet.getRow(1);
  61. XSSFCell cell_0 = row.getCell(0);
  62. assertEquals(STCellType.INT_INLINE_STR, cell_0.getCTCell().getT().intValue());
  63. assertTrue(cell_0.getCTCell().isSetIs());
  64. assertEquals("A Very large string in column 1 AAAAAAAAAAAAAAAAAAAAA", cell_0.getStringCellValue());
  65. XSSFCell cell_1 = row.getCell(1);
  66. assertEquals(STCellType.INT_INLINE_STR, cell_1.getCTCell().getT().intValue());
  67. assertTrue(cell_1.getCTCell().isSetIs());
  68. assertEquals("foo", cell_1.getStringCellValue());
  69. XSSFCell cell_2 = row.getCell(2);
  70. assertEquals(STCellType.INT_INLINE_STR, cell_2.getCTCell().getT().intValue());
  71. assertTrue(cell_2.getCTCell().isSetIs());
  72. assertEquals("bar", row.getCell(2).getStringCellValue());
  73. }
  74. /**
  75. * Bug 47278 - xsi:nil attribute for <t> tag caused Excel 2007 to fail to open workbook
  76. */
  77. public void test47278() {
  78. XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.createWorkbook();
  79. XSSFSheet sheet = wb.createSheet();
  80. XSSFRow row = sheet.createRow(0);
  81. SharedStringsTable sst = wb.getSharedStringSource();
  82. assertEquals(0, sst.getCount());
  83. //case 1. cell.setCellValue(new XSSFRichTextString((String)null));
  84. XSSFCell cell_0 = row.createCell(0);
  85. XSSFRichTextString str = new XSSFRichTextString((String)null);
  86. assertNull(str.getString());
  87. cell_0.setCellValue(str);
  88. assertEquals(0, sst.getCount());
  89. assertEquals(XSSFCell.CELL_TYPE_BLANK, cell_0.getCellType());
  90. //case 2. cell.setCellValue((String)null);
  91. XSSFCell cell_1 = row.createCell(1);
  92. cell_1.setCellValue((String)null);
  93. assertEquals(0, sst.getCount());
  94. assertEquals(XSSFCell.CELL_TYPE_BLANK, cell_1.getCellType());
  95. }
  96. public void testFormulaString() {
  97. XSSFWorkbook wb = new XSSFWorkbook();
  98. XSSFCell cell = wb.createSheet().createRow(0).createCell(0);
  99. CTCell ctCell = cell.getCTCell(); //low-level bean holding cell's xml
  100. cell.setCellFormula("A2");
  101. assertEquals(XSSFCell.CELL_TYPE_FORMULA, cell.getCellType());
  102. assertEquals("A2", cell.getCellFormula());
  103. //the value is not set and cell's type='N' which means blank
  104. assertEquals(STCellType.N, ctCell.getT());
  105. //set cached formula value
  106. cell.setCellValue("t='str'");
  107. //we are still of 'formula' type
  108. assertEquals(XSSFCell.CELL_TYPE_FORMULA, cell.getCellType());
  109. assertEquals("A2", cell.getCellFormula());
  110. //cached formula value is set and cell's type='STR'
  111. assertEquals(STCellType.STR, ctCell.getT());
  112. assertEquals("t='str'", cell.getStringCellValue());
  113. //now remove the formula, the cached formula result remains
  114. cell.setCellFormula(null);
  115. assertEquals(XSSFCell.CELL_TYPE_STRING, cell.getCellType());
  116. assertEquals(STCellType.STR, ctCell.getT());
  117. //the line below failed prior to fix of Bug #47889
  118. assertEquals("t='str'", cell.getStringCellValue());
  119. //revert to a blank cell
  120. cell.setCellValue((String)null);
  121. assertEquals(XSSFCell.CELL_TYPE_BLANK, cell.getCellType());
  122. assertEquals(STCellType.N, ctCell.getT());
  123. assertEquals("", cell.getStringCellValue());
  124. }
  125. /**
  126. * Bug 47889: problems when calling XSSFCell.getStringCellValue() on a workbook created in Gnumeric
  127. */
  128. public void test47889() {
  129. XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.openSampleWorkbook("47889.xlsx");
  130. XSSFSheet sh = wb.getSheetAt(0);
  131. XSSFCell cell;
  132. //try a string cell
  133. cell = sh.getRow(0).getCell(0);
  134. assertEquals(XSSFCell.CELL_TYPE_STRING, cell.getCellType());
  135. assertEquals("a", cell.getStringCellValue());
  136. assertEquals("a", cell.toString());
  137. //Gnumeric produces spreadsheets without styles
  138. //make sure we return null for that instead of throwing OutOfBounds
  139. assertEquals(null, cell.getCellStyle());
  140. //try a numeric cell
  141. cell = sh.getRow(1).getCell(0);
  142. assertEquals(XSSFCell.CELL_TYPE_NUMERIC, cell.getCellType());
  143. assertEquals(1.0, cell.getNumericCellValue());
  144. assertEquals("1.0", cell.toString());
  145. //Gnumeric produces spreadsheets without styles
  146. //make sure we return null for that instead of throwing OutOfBounds
  147. assertEquals(null, cell.getCellStyle());
  148. }
  149. /**
  150. * Cell with the formula that returns error must return error code(There was
  151. * an problem that cell could not return error value form formula cell).
  152. */
  153. public void testGetErrorCellValueFromFormulaCell() {
  154. XSSFWorkbook wb = new XSSFWorkbook();
  155. XSSFSheet sheet = wb.createSheet();
  156. XSSFRow row = sheet.createRow(0);
  157. XSSFCell cell = row.createCell(0);
  158. cell.setCellFormula("SQRT(-1)");
  159. wb.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell(cell);
  160. assertEquals(36, cell.getErrorCellValue());
  161. }
  162. public void testMissingRAttribute() {
  163. XSSFWorkbook wb = new XSSFWorkbook();
  164. XSSFSheet sheet = wb.createSheet();
  165. XSSFRow row = sheet.createRow(0);
  166. XSSFCell a1 = row.createCell(0);
  167. a1.setCellValue("A1");
  168. XSSFCell a2 = row.createCell(1);
  169. a2.setCellValue("B1");
  170. XSSFCell a4 = row.createCell(4);
  171. a4.setCellValue("E1");
  172. XSSFCell a6 = row.createCell(5);
  173. a6.setCellValue("F1");
  174. assertCellsWithMissingR(row);
  175. a2.getCTCell().unsetR();
  176. a6.getCTCell().unsetR();
  177. assertCellsWithMissingR(row);
  178. wb = (XSSFWorkbook)_testDataProvider.writeOutAndReadBack(wb);
  179. row = wb.getSheetAt(0).getRow(0);
  180. assertCellsWithMissingR(row);
  181. }
  182. private void assertCellsWithMissingR(XSSFRow row){
  183. XSSFCell a1 = row.getCell(0);
  184. assertNotNull(a1);
  185. XSSFCell a2 = row.getCell(1);
  186. assertNotNull(a2);
  187. XSSFCell a5 = row.getCell(4);
  188. assertNotNull(a5);
  189. XSSFCell a6 = row.getCell(5);
  190. assertNotNull(a6);
  191. assertEquals(6, row.getLastCellNum());
  192. assertEquals(4, row.getPhysicalNumberOfCells());
  193. assertEquals("A1", a1.getStringCellValue());
  194. assertEquals("B1", a2.getStringCellValue());
  195. assertEquals("E1", a5.getStringCellValue());
  196. assertEquals("F1", a6.getStringCellValue());
  197. // even if R attribute is not set,
  198. // POI is able to re-construct it from column and row indexes
  199. assertEquals("A1", a1.getReference());
  200. assertEquals("B1", a2.getReference());
  201. assertEquals("E1", a5.getReference());
  202. assertEquals("F1", a6.getReference());
  203. }
  204. public void testMissingRAttributeBug54288() {
  205. // workbook with cells missing the R attribute
  206. XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.openSampleWorkbook("54288.xlsx");
  207. // same workbook re-saved in Excel 2010, the R attribute is updated for every cell with the right value.
  208. XSSFWorkbook wbRef = (XSSFWorkbook)_testDataProvider.openSampleWorkbook("54288-ref.xlsx");
  209. XSSFSheet sheet = wb.getSheetAt(0);
  210. XSSFSheet sheetRef = wbRef.getSheetAt(0);
  211. assertEquals(sheetRef.getPhysicalNumberOfRows(), sheet.getPhysicalNumberOfRows());
  212. // Test idea: iterate over cells in the reference worksheet, they all have the R attribute set.
  213. // For each cell from the reference sheet find the corresponding cell in the problematic file (with missing R)
  214. // and assert that POI reads them equally:
  215. DataFormatter formater = new DataFormatter();
  216. for(Row r : sheetRef){
  217. XSSFRow rowRef = (XSSFRow)r;
  218. XSSFRow row = sheet.getRow(rowRef.getRowNum());
  219. assertEquals("number of cells in row["+row.getRowNum()+"]",
  220. rowRef.getPhysicalNumberOfCells(), row.getPhysicalNumberOfCells());
  221. for(Cell c : rowRef){
  222. XSSFCell cellRef = (XSSFCell)c;
  223. XSSFCell cell = row.getCell(cellRef.getColumnIndex());
  224. assertEquals(cellRef.getColumnIndex(), cell.getColumnIndex());
  225. assertEquals(cellRef.getReference(), cell.getReference());
  226. if(!cell.getCTCell().isSetR()){
  227. assertTrue("R must e set in cellRef", cellRef.getCTCell().isSetR());
  228. String valRef = formater.formatCellValue(cellRef);
  229. String val = formater.formatCellValue(cell);
  230. assertEquals(valRef, val);
  231. }
  232. }
  233. }
  234. }
  235. }