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.

TestXSSFTable.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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.assertEquals;
  17. import static org.junit.Assert.assertFalse;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assert.assertNull;
  20. import static org.junit.Assert.assertTrue;
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import org.apache.poi.ss.usermodel.Cell;
  28. import org.apache.poi.ss.util.AreaReference;
  29. import org.apache.poi.ss.util.CellReference;
  30. import org.apache.poi.util.IOUtils;
  31. import org.apache.poi.util.TempFile;
  32. import org.apache.poi.xssf.XSSFTestDataSamples;
  33. import org.apache.poi.xssf.streaming.SXSSFWorkbook;
  34. import org.junit.Test;
  35. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable;
  36. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn;
  37. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo;
  38. public final class TestXSSFTable {
  39. @Test
  40. public void bug56274() throws IOException {
  41. // read sample file
  42. XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("56274.xlsx");
  43. // read the original sheet header order
  44. XSSFRow row = wb1.getSheetAt(0).getRow(0);
  45. List<String> headers = new ArrayList<>();
  46. for (Cell cell : row) {
  47. headers.add(cell.getStringCellValue());
  48. }
  49. // save the worksheet as-is using SXSSF
  50. File outputFile = TempFile.createTempFile("poi-56274", ".xlsx");
  51. SXSSFWorkbook outputWorkbook = new SXSSFWorkbook(wb1);
  52. FileOutputStream fos = new FileOutputStream(outputFile);
  53. outputWorkbook.write(fos);
  54. fos.close();
  55. outputWorkbook.close();
  56. // re-read the saved file and make sure headers in the xml are in the original order
  57. FileInputStream fis = new FileInputStream(outputFile);
  58. XSSFWorkbook wb2 = new XSSFWorkbook(fis);
  59. fis.close();
  60. CTTable ctTable = wb2.getSheetAt(0).getTables().get(0).getCTTable();
  61. CTTableColumn[] ctTableColumnArray = ctTable.getTableColumns().getTableColumnArray();
  62. assertEquals("number of headers in xml table should match number of header cells in worksheet",
  63. headers.size(), ctTableColumnArray.length);
  64. for (int i = 0; i < headers.size(); i++) {
  65. assertEquals("header name in xml table should match number of header cells in worksheet",
  66. headers.get(i), ctTableColumnArray[i].getName());
  67. }
  68. assertTrue(outputFile.delete());
  69. wb2.close();
  70. wb1.close();
  71. }
  72. @Test
  73. public void testCTTableStyleInfo() throws IOException {
  74. XSSFWorkbook outputWorkbook = new XSSFWorkbook();
  75. XSSFSheet sheet = outputWorkbook.createSheet();
  76. //Create
  77. XSSFTable outputTable = sheet.createTable();
  78. outputTable.setDisplayName("Test");
  79. CTTable outputCTTable = outputTable.getCTTable();
  80. //Style configurations
  81. CTTableStyleInfo outputStyleInfo = outputCTTable.addNewTableStyleInfo();
  82. outputStyleInfo.setName("TableStyleLight1");
  83. outputStyleInfo.setShowColumnStripes(false);
  84. outputStyleInfo.setShowRowStripes(true);
  85. XSSFWorkbook inputWorkbook = XSSFTestDataSamples.writeOutAndReadBack(outputWorkbook);
  86. List<XSSFTable> tables = inputWorkbook.getSheetAt(0).getTables();
  87. assertEquals("Tables number", 1, tables.size());
  88. XSSFTable inputTable = tables.get(0);
  89. assertEquals("Table display name", outputTable.getDisplayName(), inputTable.getDisplayName());
  90. CTTableStyleInfo inputStyleInfo = inputTable.getCTTable().getTableStyleInfo();
  91. assertEquals("Style name", outputStyleInfo.getName(), inputStyleInfo.getName());
  92. assertEquals("Show column stripes",
  93. outputStyleInfo.getShowColumnStripes(), inputStyleInfo.getShowColumnStripes());
  94. assertEquals("Show row stripes",
  95. outputStyleInfo.getShowRowStripes(), inputStyleInfo.getShowRowStripes());
  96. inputWorkbook.close();
  97. outputWorkbook.close();
  98. }
  99. @Test
  100. public void findColumnIndex() throws IOException {
  101. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx");
  102. XSSFTable table = wb.getTable("\\_Prime.1");
  103. assertNotNull(table);
  104. assertEquals("column header has special escaped characters",
  105. 0, table.findColumnIndex("calc='#*'#"));
  106. assertEquals(1, table.findColumnIndex("Name"));
  107. assertEquals(2, table.findColumnIndex("Number"));
  108. assertEquals("case insensitive", 2, table.findColumnIndex("NuMbEr"));
  109. // findColumnIndex should return -1 if no column header name matches
  110. assertEquals(-1, table.findColumnIndex(null));
  111. assertEquals(-1, table.findColumnIndex(""));
  112. assertEquals(-1, table.findColumnIndex("one"));
  113. wb.close();
  114. }
  115. @Test
  116. public void findColumnIndexIsRelativeToTableNotSheet() throws IOException {
  117. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("DataTableCities.xlsx");
  118. XSSFTable table = wb.getTable("SmallCity");
  119. // Make sure that XSSFTable.findColumnIndex returns the column index relative to the first
  120. // column in the table, not the column number in the sheet
  121. assertEquals(0, table.findColumnIndex("City")); // column I in worksheet but 0th column in table
  122. assertEquals(1, table.findColumnIndex("Latitude"));
  123. assertEquals(2, table.findColumnIndex("Longitude"));
  124. assertEquals(3, table.findColumnIndex("Population"));
  125. wb.close();
  126. }
  127. @Test
  128. public void getSheetName() throws IOException {
  129. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx");
  130. XSSFTable table = wb.getTable("\\_Prime.1");
  131. assertEquals("Table", table.getSheetName());
  132. wb.close();
  133. }
  134. @Test
  135. public void isHasTotalsRow() throws IOException {
  136. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx");
  137. XSSFTable table = wb.getTable("\\_Prime.1");
  138. assertFalse(table.getTotalsRowCount() > 0);
  139. wb.close();
  140. }
  141. @Test
  142. public void getStartColIndex() throws IOException {
  143. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx");
  144. XSSFTable table = wb.getTable("\\_Prime.1");
  145. assertEquals(0, table.getStartColIndex());
  146. wb.close();
  147. }
  148. @Test
  149. public void getEndColIndex() throws IOException {
  150. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx");
  151. XSSFTable table = wb.getTable("\\_Prime.1");
  152. assertEquals(2, table.getEndColIndex());
  153. wb.close();
  154. }
  155. @Test
  156. public void getStartRowIndex() throws IOException {
  157. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx");
  158. XSSFTable table = wb.getTable("\\_Prime.1");
  159. assertEquals(0, table.getStartRowIndex());
  160. wb.close();
  161. }
  162. @Test
  163. public void getEndRowIndex() throws IOException {
  164. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx");
  165. XSSFTable table = wb.getTable("\\_Prime.1");
  166. assertEquals(6, table.getEndRowIndex());
  167. wb.close();
  168. }
  169. @Test
  170. public void getStartCellReference() throws IOException {
  171. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx");
  172. XSSFTable table = wb.getTable("\\_Prime.1");
  173. assertEquals(new CellReference("A1"), table.getStartCellReference());
  174. wb.close();
  175. }
  176. @Test
  177. public void getEndCellReference() throws IOException {
  178. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx");
  179. XSSFTable table = wb.getTable("\\_Prime.1");
  180. assertEquals(new CellReference("C7"), table.getEndCellReference());
  181. wb.close();
  182. }
  183. @Test
  184. public void getNumberOfMappedColumns() throws IOException {
  185. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx");
  186. XSSFTable table = wb.getTable("\\_Prime.1");
  187. assertEquals(3, table.getNumberOfMappedColumns());
  188. wb.close();
  189. }
  190. @Test
  191. public void getColumnCount() throws IOException {
  192. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx");
  193. XSSFTable table = wb.getTable("\\_Prime.1");
  194. assertEquals(3, table.getColumnCount());
  195. wb.close();
  196. }
  197. @Test
  198. public void getAndSetDisplayName() throws IOException {
  199. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx");
  200. XSSFTable table = wb.getTable("\\_Prime.1");
  201. assertEquals("\\_Prime.1", table.getDisplayName());
  202. table.setDisplayName(null);
  203. assertNull(table.getDisplayName());
  204. assertEquals("\\_Prime.1", table.getName()); // name and display name are different
  205. table.setDisplayName("Display name");
  206. assertEquals("Display name", table.getDisplayName());
  207. assertEquals("\\_Prime.1", table.getName()); // name and display name are different
  208. wb.close();
  209. }
  210. @Test
  211. public void getCellReferences() {
  212. // make sure that cached start and end cell references
  213. // can be synchronized with the underlying CTTable
  214. XSSFWorkbook wb = new XSSFWorkbook();
  215. XSSFSheet sh = wb.createSheet();
  216. XSSFTable table = sh.createTable();
  217. CTTable ctTable = table.getCTTable();
  218. ctTable.setRef("B2:E8");
  219. assertEquals(new CellReference("B2"), table.getStartCellReference());
  220. assertEquals(new CellReference("E8"), table.getEndCellReference());
  221. // At this point start and end cell reference are cached
  222. // and may not follow changes to the underlying CTTable
  223. ctTable.setRef("C1:M3");
  224. assertEquals(new CellReference("B2"), table.getStartCellReference());
  225. assertEquals(new CellReference("E8"), table.getEndCellReference());
  226. // Force a synchronization between CTTable and XSSFTable
  227. // start and end cell references
  228. table.updateReferences();
  229. assertEquals(new CellReference("C1"), table.getStartCellReference());
  230. assertEquals(new CellReference("M3"), table.getEndCellReference());
  231. IOUtils.closeQuietly(wb);
  232. }
  233. @Test
  234. public void getRowCount() {
  235. XSSFWorkbook wb = new XSSFWorkbook();
  236. XSSFSheet sh = wb.createSheet();
  237. XSSFTable table = sh.createTable();
  238. CTTable ctTable = table.getCTTable();
  239. assertEquals(0, table.getRowCount());
  240. ctTable.setRef("B2:B2");
  241. // update cell references to clear the cache
  242. table.updateReferences();
  243. assertEquals(1, table.getRowCount());
  244. ctTable.setRef("B2:B12");
  245. // update cell references to clear the cache
  246. table.updateReferences();
  247. assertEquals(11, table.getRowCount());
  248. IOUtils.closeQuietly(wb);
  249. }
  250. @Test
  251. public void testGetDataRowCount() {
  252. XSSFWorkbook wb = new XSSFWorkbook();
  253. XSSFSheet sh = wb.createSheet();
  254. AreaReference tableArea = new AreaReference("B2:B6", wb.getSpreadsheetVersion());
  255. XSSFTable table = sh.createTable(tableArea);
  256. assertEquals(5, table.getRowCount()); // includes column header
  257. assertEquals(4, table.getDataRowCount());
  258. table.setArea(new AreaReference("B2:B7", wb.getSpreadsheetVersion()));
  259. assertEquals(6, table.getRowCount());
  260. assertEquals(5, table.getDataRowCount());
  261. IOUtils.closeQuietly(wb);
  262. }
  263. @Test
  264. public void testSetDataRowCount() throws IOException {
  265. XSSFWorkbook wb = new XSSFWorkbook();
  266. XSSFSheet sh = wb.createSheet();
  267. // 1 header row + 1 data row
  268. AreaReference tableArea = new AreaReference("C10:C11", wb.getSpreadsheetVersion());
  269. XSSFTable table = sh.createTable(tableArea);
  270. assertEquals(2, table.getRowCount()); // includes all data and header/footer rows
  271. assertEquals(1, table.getHeaderRowCount());
  272. assertEquals(1, table.getDataRowCount());
  273. assertEquals(0, table.getTotalsRowCount());
  274. table.setDataRowCount(5);
  275. assertEquals(6, table.getRowCount());
  276. assertEquals(1, table.getHeaderRowCount());
  277. assertEquals(5, table.getDataRowCount());
  278. assertEquals(0, table.getTotalsRowCount());
  279. assertEquals("C10:C15", table.getArea().formatAsString());
  280. IOUtils.closeQuietly(wb);
  281. }
  282. @Test
  283. public void testSetArea() throws IOException {
  284. XSSFWorkbook wb = new XSSFWorkbook();
  285. XSSFSheet sh = wb.createSheet();
  286. AreaReference tableArea = new AreaReference("B10:D12", wb.getSpreadsheetVersion());
  287. XSSFTable table = sh.createTable(tableArea);
  288. assertEquals(3, table.getColumnCount());
  289. assertEquals(3, table.getRowCount());
  290. // move table without resizing, shouldn't change row or column count
  291. AreaReference tableArea2 = new AreaReference("B11:D13", wb.getSpreadsheetVersion());
  292. table.setArea(tableArea2);
  293. assertEquals(3, table.getColumnCount());
  294. assertEquals(3, table.getRowCount());
  295. // increase size by 1 row and 1 column
  296. AreaReference tableArea3 = new AreaReference("B11:E14", wb.getSpreadsheetVersion());
  297. table.setArea(tableArea3);
  298. assertEquals(4, table.getColumnCount());
  299. assertEquals(4, table.getRowCount());
  300. // reduce size by 2 rows and 2 columns
  301. AreaReference tableArea4 = new AreaReference("C12:D13", wb.getSpreadsheetVersion());
  302. table.setArea(tableArea4);
  303. assertEquals(2, table.getColumnCount());
  304. assertEquals(2, table.getRowCount());
  305. IOUtils.closeQuietly(wb);
  306. }
  307. @Test
  308. public void testCreateColumn() {
  309. XSSFWorkbook wb = new XSSFWorkbook();
  310. XSSFSheet sh = wb.createSheet();
  311. AreaReference tableArea = new AreaReference("A2:A3", wb.getSpreadsheetVersion());
  312. XSSFTable table = sh.createTable(tableArea);
  313. assertEquals(1, table.getColumnCount());
  314. assertEquals(2, table.getRowCount());
  315. // add columns
  316. table.createColumn("Column B");
  317. table.createColumn("Column D");
  318. table.createColumn("Column C", 2); // add between B and D
  319. table.updateReferences();
  320. table.updateHeaders();
  321. assertEquals(4, table.getColumnCount());
  322. assertEquals(2, table.getRowCount());
  323. assertEquals("Column 1", table.getColumns().get(0).getName()); // generated name
  324. assertEquals("Column B", table.getColumns().get(1).getName());
  325. assertEquals("Column C", table.getColumns().get(2).getName());
  326. assertEquals("Column D", table.getColumns().get(3).getName());
  327. IOUtils.closeQuietly(wb);
  328. }
  329. @Test(expected = IllegalArgumentException.class)
  330. public void testCreateColumnInvalidIndex() throws IOException {
  331. try (XSSFWorkbook wb = new XSSFWorkbook();) {
  332. XSSFSheet sh = wb.createSheet();
  333. AreaReference tableArea = new AreaReference("D2:D3", wb.getSpreadsheetVersion());
  334. XSSFTable table = sh.createTable(tableArea);
  335. // add columns
  336. table.createColumn("Column 2", 1);
  337. table.createColumn("Column 3", 3); // out of bounds
  338. }
  339. }
  340. @Test
  341. public void testDifferentHeaderTypes() throws IOException {
  342. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("TablesWithDifferentHeaders.xlsx");
  343. assertEquals(3, wb.getNumberOfSheets());
  344. XSSFSheet s;
  345. XSSFTable t;
  346. // TODO Nicer column fetching
  347. s = wb.getSheet("IntHeaders");
  348. assertEquals(1, s.getTables().size());
  349. t = s.getTables().get(0);
  350. assertEquals("A1:B2", t.getCellReferences().formatAsString());
  351. assertEquals("12", t.getCTTable().getTableColumns().getTableColumnArray(0).getName());
  352. assertEquals("34", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
  353. s = wb.getSheet("FloatHeaders");
  354. assertEquals(1, s.getTables().size());
  355. t = s.getTables().get(0);
  356. assertEquals("A1:B2", t.getCellReferences().formatAsString());
  357. assertEquals("12.34", t.getCTTable().getTableColumns().getTableColumnArray(0).getName());
  358. assertEquals("34.56", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
  359. s = wb.getSheet("NoExplicitHeaders");
  360. assertEquals(1, s.getTables().size());
  361. t = s.getTables().get(0);
  362. assertEquals("A1:B3", t.getCellReferences().formatAsString());
  363. assertEquals("Column1", t.getCTTable().getTableColumns().getTableColumnArray(0).getName());
  364. assertEquals("Column2", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
  365. }
  366. /**
  367. * See https://stackoverflow.com/questions/44407111/apache-poi-cant-format-filled-cells-as-numeric
  368. */
  369. @Test
  370. public void testNumericCellsInTable() throws IOException {
  371. XSSFWorkbook wb = new XSSFWorkbook();
  372. XSSFSheet s = wb.createSheet();
  373. // Create some cells, some numeric, some not
  374. Cell c1 = s.createRow(0).createCell(0);
  375. Cell c2 = s.getRow(0).createCell(1);
  376. Cell c3 = s.getRow(0).createCell(2);
  377. Cell c4 = s.createRow(1).createCell(0);
  378. Cell c5 = s.getRow(1).createCell(1);
  379. Cell c6 = s.getRow(1).createCell(2);
  380. c1.setCellValue(12);
  381. c2.setCellValue(34.56);
  382. c3.setCellValue("ABCD");
  383. c4.setCellValue("AB");
  384. c5.setCellValue("CD");
  385. c6.setCellValue("EF");
  386. // Setting up the table
  387. XSSFTable t = s.createTable(new AreaReference("A1:C3", wb.getSpreadsheetVersion()));
  388. t.setName("TableTest");
  389. t.setDisplayName("CT_Table_Test");
  390. t.createColumn("Column 1");
  391. t.createColumn("Column 2");
  392. t.createColumn("Column 3");
  393. t.setCellReferences(wb.getCreationHelper().createAreaReference(
  394. new CellReference(c1), new CellReference(c6)
  395. ));
  396. // Save and re-load
  397. XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb);
  398. IOUtils.closeQuietly(wb);
  399. s = wb2.getSheetAt(0);
  400. // Check
  401. assertEquals(1, s.getTables().size());
  402. t = s.getTables().get(0);
  403. assertEquals("A1", t.getStartCellReference().formatAsString());
  404. assertEquals("C2", t.getEndCellReference().formatAsString());
  405. // TODO Nicer column fetching
  406. assertEquals("12", t.getCTTable().getTableColumns().getTableColumnArray(0).getName());
  407. assertEquals("34.56", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
  408. assertEquals("ABCD", t.getCTTable().getTableColumns().getTableColumnArray(2).getName());
  409. // Done
  410. IOUtils.closeQuietly(wb2);
  411. }
  412. }