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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertFalse;
  18. import static org.junit.jupiter.api.Assertions.assertNotNull;
  19. import static org.junit.jupiter.api.Assertions.assertThrows;
  20. import static org.junit.jupiter.api.Assertions.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 java.util.Locale;
  28. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  29. import org.apache.poi.ss.SpreadsheetVersion;
  30. import org.apache.poi.ss.usermodel.Cell;
  31. import org.apache.poi.ss.usermodel.CellStyle;
  32. import org.apache.poi.ss.usermodel.Row;
  33. import org.apache.poi.ss.util.AreaReference;
  34. import org.apache.poi.ss.util.CellReference;
  35. import org.apache.poi.util.TempFile;
  36. import org.apache.poi.xssf.XSSFTestDataSamples;
  37. import org.apache.poi.xssf.streaming.SXSSFWorkbook;
  38. import org.junit.jupiter.api.Test;
  39. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable;
  40. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn;
  41. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo;
  42. public final class TestXSSFTable {
  43. @Test
  44. void bug56274() throws IOException {
  45. // read sample file
  46. try (XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("56274.xlsx")) {
  47. // read the original sheet header order
  48. XSSFRow row = wb1.getSheetAt(0).getRow(0);
  49. List<String> headers = new ArrayList<>();
  50. for (Cell cell : row) {
  51. headers.add(cell.getStringCellValue());
  52. }
  53. // save the worksheet as-is using SXSSF
  54. File outputFile = TempFile.createTempFile("poi-56274", ".xlsx");
  55. SXSSFWorkbook outputWorkbook = new SXSSFWorkbook(wb1);
  56. FileOutputStream fos = new FileOutputStream(outputFile);
  57. outputWorkbook.write(fos);
  58. fos.close();
  59. outputWorkbook.close();
  60. // re-read the saved file and make sure headers in the xml are in the original order
  61. FileInputStream fis = new FileInputStream(outputFile);
  62. XSSFWorkbook wb2 = new XSSFWorkbook(fis);
  63. fis.close();
  64. CTTable ctTable = wb2.getSheetAt(0).getTables().get(0).getCTTable();
  65. CTTableColumn[] ctTableColumnArray = ctTable.getTableColumns().getTableColumnArray();
  66. assertEquals(headers.size(), ctTableColumnArray.length,
  67. "number of headers in xml table should match number of header cells in worksheet");
  68. for (int i = 0; i < headers.size(); i++) {
  69. assertEquals(headers.get(i), ctTableColumnArray[i].getName(),
  70. "header name in xml table should match number of header cells in worksheet");
  71. }
  72. assertTrue(outputFile.delete());
  73. wb2.close();
  74. }
  75. }
  76. @Test
  77. void testCTTableStyleInfo() throws IOException {
  78. try (XSSFWorkbook outputWorkbook = new XSSFWorkbook()) {
  79. XSSFSheet sheet = outputWorkbook.createSheet();
  80. //Create
  81. XSSFTable outputTable = sheet.createTable(null);
  82. outputTable.setDisplayName("Test");
  83. CTTable outputCTTable = outputTable.getCTTable();
  84. //Style configurations
  85. CTTableStyleInfo outputStyleInfo = outputCTTable.addNewTableStyleInfo();
  86. outputStyleInfo.setName("TableStyleLight1");
  87. outputStyleInfo.setShowColumnStripes(false);
  88. outputStyleInfo.setShowRowStripes(true);
  89. try (XSSFWorkbook inputWorkbook = XSSFTestDataSamples.writeOutAndReadBack(outputWorkbook)) {
  90. List<XSSFTable> tables = inputWorkbook.getSheetAt(0).getTables();
  91. assertEquals(1, tables.size(), "Tables number");
  92. XSSFTable inputTable = tables.get(0);
  93. assertEquals(outputTable.getDisplayName(), inputTable.getDisplayName(), "Table display name");
  94. CTTableStyleInfo inputStyleInfo = inputTable.getCTTable().getTableStyleInfo();
  95. assertEquals(outputStyleInfo.getName(), inputStyleInfo.getName(), "Style name");
  96. assertEquals(outputStyleInfo.getShowColumnStripes(), inputStyleInfo.getShowColumnStripes(), "Show column stripes");
  97. assertEquals(outputStyleInfo.getShowRowStripes(), inputStyleInfo.getShowRowStripes(), "Show row stripes");
  98. }
  99. }
  100. }
  101. @Test
  102. void findColumnIndex() throws IOException {
  103. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx")) {
  104. XSSFTable table = wb.getTable("\\_Prime.1");
  105. assertNotNull(table);
  106. assertEquals(0, table.findColumnIndex("calc='#*'#"), "column header has special escaped characters");
  107. assertEquals(1, table.findColumnIndex("Name"));
  108. assertEquals(2, table.findColumnIndex("Number"));
  109. assertEquals(2, table.findColumnIndex("NuMbEr"), "case insensitive");
  110. // findColumnIndex should return -1 if no column header name matches
  111. assertEquals(-1, table.findColumnIndex(null));
  112. assertEquals(-1, table.findColumnIndex(""));
  113. assertEquals(-1, table.findColumnIndex("one"));
  114. }
  115. }
  116. @Test
  117. void findColumnIndexIsRelativeToTableNotSheet() throws IOException {
  118. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("DataTableCities.xlsx")) {
  119. XSSFTable table = wb.getTable("SmallCity");
  120. // Make sure that XSSFTable.findColumnIndex returns the column index relative to the first
  121. // column in the table, not the column number in the sheet
  122. assertEquals(0, table.findColumnIndex("City")); // column I in worksheet but 0th column in table
  123. assertEquals(1, table.findColumnIndex("Latitude"));
  124. assertEquals(2, table.findColumnIndex("Longitude"));
  125. assertEquals(3, table.findColumnIndex("Population"));
  126. }
  127. }
  128. @Test
  129. void getSheetName() throws IOException {
  130. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx")) {
  131. XSSFTable table = wb.getTable("\\_Prime.1");
  132. assertEquals("Table", table.getSheetName());
  133. }
  134. }
  135. @Test
  136. void isHasTotalsRow() throws IOException {
  137. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx")) {
  138. XSSFTable table = wb.getTable("\\_Prime.1");
  139. assertFalse(table.getTotalsRowCount() > 0);
  140. }
  141. }
  142. @Test
  143. void getStartColIndex() throws IOException {
  144. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx")) {
  145. XSSFTable table = wb.getTable("\\_Prime.1");
  146. assertEquals(0, table.getStartColIndex());
  147. }
  148. }
  149. @Test
  150. void getEndColIndex() throws IOException {
  151. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx")) {
  152. XSSFTable table = wb.getTable("\\_Prime.1");
  153. assertEquals(2, table.getEndColIndex());
  154. }
  155. }
  156. @Test
  157. void getStartRowIndex() throws IOException {
  158. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx")) {
  159. XSSFTable table = wb.getTable("\\_Prime.1");
  160. assertEquals(0, table.getStartRowIndex());
  161. }
  162. }
  163. @Test
  164. void getEndRowIndex() throws IOException {
  165. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx")) {
  166. XSSFTable table = wb.getTable("\\_Prime.1");
  167. assertEquals(6, table.getEndRowIndex());
  168. }
  169. }
  170. @Test
  171. void getStartCellReference() throws IOException {
  172. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx")) {
  173. XSSFTable table = wb.getTable("\\_Prime.1");
  174. assertEquals(new CellReference("A1"), table.getStartCellReference());
  175. }
  176. }
  177. @Test
  178. void getEndCellReference() throws IOException {
  179. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx")) {
  180. XSSFTable table = wb.getTable("\\_Prime.1");
  181. assertEquals(new CellReference("C7"), table.getEndCellReference());
  182. }
  183. }
  184. @Test
  185. void getEndCellReferenceFromSingleCellTable() throws IOException {
  186. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("SingleCellTable.xlsx")) {
  187. XSSFTable table = wb.getTable("Table3");
  188. assertEquals(new CellReference("A2"), table.getEndCellReference());
  189. }
  190. }
  191. @Test
  192. void getColumnCount() throws IOException {
  193. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx")) {
  194. XSSFTable table = wb.getTable("\\_Prime.1");
  195. assertEquals(3, table.getColumnCount());
  196. }
  197. }
  198. @Test
  199. void getAndSetDisplayName() throws IOException {
  200. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx")) {
  201. XSSFTable table = wb.getTable("\\_Prime.1");
  202. assertEquals("\\_Prime.1", table.getDisplayName());
  203. table.setDisplayName("Display name");
  204. assertEquals("Display name", table.getDisplayName());
  205. assertEquals("\\_Prime.1", table.getName()); // name and display name are different
  206. }
  207. }
  208. @Test
  209. void getCellReferences() throws IOException {
  210. // make sure that cached start and end cell references
  211. // can be synchronized with the underlying CTTable
  212. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  213. XSSFSheet sh = wb.createSheet();
  214. XSSFTable table = sh.createTable(null);
  215. assertNotNull(table.getDisplayName());
  216. assertNotNull(table.getCTTable().getDisplayName());
  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. }
  232. }
  233. @Test
  234. void getRowCount() throws IOException {
  235. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  236. XSSFSheet sh = wb.createSheet();
  237. XSSFTable table = sh.createTable(null);
  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. }
  249. }
  250. @Test
  251. void testGetDataRowCount() throws IOException {
  252. try (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. }
  262. }
  263. @Test
  264. void testSetDataRowCount() throws IOException {
  265. try (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. }
  281. }
  282. @Test
  283. void testCreateTableIds() throws IOException {
  284. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  285. XSSFSheet sheet = wb.createSheet();
  286. AreaReference reference1 = wb.getCreationHelper().createAreaReference(
  287. new CellReference(0, 0), new CellReference(2, 2));
  288. XSSFTable table1 = sheet.createTable(reference1);
  289. assertEquals("A1:C3", table1.getCTTable().getRef());
  290. assertNotNull(table1.getDisplayName());
  291. assertNotNull(table1.getCTTable().getDisplayName());
  292. assertEquals(1, table1.getCTTable().getTableColumns().getTableColumnArray(0).getId());
  293. assertEquals(2, table1.getCTTable().getTableColumns().getTableColumnArray(1).getId());
  294. assertEquals(3, table1.getCTTable().getTableColumns().getTableColumnArray(2).getId());
  295. assertEquals(1, table1.getCTTable().getId());
  296. AreaReference reference2 = wb.getCreationHelper().createAreaReference(
  297. new CellReference(10, 10), new CellReference(12, 12));
  298. XSSFTable table2 = sheet.createTable(reference2);
  299. assertEquals("K11:M13", table2.getCTTable().getRef());
  300. // these IDs duplicate those from table1 and may be cause of https://bz.apache.org/bugzilla/show_bug.cgi?id=62906
  301. assertEquals(1, table2.getCTTable().getTableColumns().getTableColumnArray(0).getId());
  302. assertEquals(2, table2.getCTTable().getTableColumns().getTableColumnArray(1).getId());
  303. assertEquals(3, table2.getCTTable().getTableColumns().getTableColumnArray(2).getId());
  304. assertEquals(2, table2.getCTTable().getId());
  305. }
  306. }
  307. @Test
  308. void testSetArea() throws IOException {
  309. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  310. XSSFSheet sh = wb.createSheet();
  311. AreaReference tableArea = new AreaReference("B10:D12", wb.getSpreadsheetVersion());
  312. XSSFTable table = sh.createTable(tableArea);
  313. assertEquals(3, table.getColumnCount());
  314. assertEquals(3, table.getRowCount());
  315. // move table without resizing, shouldn't change row or column count
  316. AreaReference tableArea2 = new AreaReference("B11:D13", wb.getSpreadsheetVersion());
  317. table.setArea(tableArea2);
  318. assertEquals(3, table.getColumnCount());
  319. assertEquals(3, table.getRowCount());
  320. // increase size by 1 row and 1 column
  321. AreaReference tableArea3 = new AreaReference("B11:E14", wb.getSpreadsheetVersion());
  322. table.setArea(tableArea3);
  323. assertEquals(4, table.getColumnCount());
  324. assertEquals(4, table.getRowCount());
  325. // reduce size by 2 rows and 2 columns
  326. AreaReference tableArea4 = new AreaReference("C12:D13", wb.getSpreadsheetVersion());
  327. table.setArea(tableArea4);
  328. assertEquals(2, table.getColumnCount());
  329. assertEquals(2, table.getRowCount());
  330. }
  331. }
  332. @Test
  333. void testCreateColumn() throws IOException {
  334. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  335. XSSFSheet sh = wb.createSheet();
  336. AreaReference tableArea = new AreaReference("A2:A3", wb.getSpreadsheetVersion());
  337. XSSFTable table = sh.createTable(tableArea);
  338. assertEquals(1, table.getColumnCount());
  339. assertEquals(2, table.getRowCount());
  340. // add columns
  341. XSSFTableColumn c1 = table.getColumns().get(0);
  342. XSSFTableColumn cB = table.createColumn("Column B");
  343. XSSFTableColumn cD = table.createColumn("Column D");
  344. XSSFTableColumn cC = table.createColumn("Column C", 2); // add between B and D
  345. table.updateReferences();
  346. table.updateHeaders();
  347. assertEquals(4, table.getColumnCount());
  348. assertEquals(2, table.getRowCount());
  349. // column IDs start at 1, and increase in the order columns are added (see bug #62740)
  350. assertEquals(1, c1.getId(), "Column c ID");
  351. assertTrue (c1.getId() < cB.getId(), "Column B ID");
  352. assertTrue (cB.getId() < cD.getId(), "Column D ID");
  353. assertTrue (cD.getId() < cC.getId(), "Column C ID");
  354. // generated name
  355. assertEquals("Column 1", table.getColumns().get(0).getName());
  356. assertEquals("Column B", table.getColumns().get(1).getName());
  357. assertEquals("Column C", table.getColumns().get(2).getName());
  358. assertEquals("Column D", table.getColumns().get(3).getName());
  359. }
  360. }
  361. @Test
  362. void testCreateColumnInvalidIndex() throws IOException {
  363. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  364. XSSFSheet sh = wb.createSheet();
  365. AreaReference tableArea = new AreaReference("D2:D3", wb.getSpreadsheetVersion());
  366. XSSFTable table = sh.createTable(tableArea);
  367. // add columns
  368. table.createColumn("Column 2", 1);
  369. // out of bounds
  370. assertThrows(IllegalArgumentException.class, () -> table.createColumn("Column 3", 3));
  371. }
  372. }
  373. @Test
  374. void testDifferentHeaderTypes() throws IOException {
  375. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("TablesWithDifferentHeaders.xlsx")) {
  376. assertEquals(3, wb.getNumberOfSheets());
  377. XSSFSheet s;
  378. XSSFTable t;
  379. // TODO Nicer column fetching
  380. s = wb.getSheet("IntHeaders");
  381. assertEquals(1, s.getTables().size());
  382. t = s.getTables().get(0);
  383. assertEquals("A1:B2", t.getCellReferences().formatAsString());
  384. assertEquals("12", t.getCTTable().getTableColumns().getTableColumnArray(0).getName());
  385. assertEquals("34", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
  386. s = wb.getSheet("FloatHeaders");
  387. assertEquals(1, s.getTables().size());
  388. t = s.getTables().get(0);
  389. assertEquals("A1:B2", t.getCellReferences().formatAsString());
  390. assertEquals("12.34", t.getCTTable().getTableColumns().getTableColumnArray(0).getName());
  391. assertEquals("34.56", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
  392. s = wb.getSheet("NoExplicitHeaders");
  393. assertEquals(1, s.getTables().size());
  394. t = s.getTables().get(0);
  395. assertEquals("A1:B3", t.getCellReferences().formatAsString());
  396. assertEquals("Column1", t.getCTTable().getTableColumns().getTableColumnArray(0).getName());
  397. assertEquals("Column2", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
  398. }
  399. }
  400. @Test
  401. void testNumericCellsInTable() throws IOException {
  402. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  403. XSSFSheet s = wb.createSheet();
  404. // Create some cells, some numeric, some not
  405. Cell c1 = s.createRow(0).createCell(0);
  406. Cell c2 = s.getRow(0).createCell(1);
  407. Cell c3 = s.getRow(0).createCell(2);
  408. Cell c4 = s.createRow(1).createCell(0);
  409. Cell c5 = s.getRow(1).createCell(1);
  410. Cell c6 = s.getRow(1).createCell(2);
  411. c1.setCellValue(12);
  412. c2.setCellValue(34.56);
  413. c3.setCellValue("ABCD");
  414. c4.setCellValue("AB");
  415. c5.setCellValue("CD");
  416. c6.setCellValue("EF");
  417. // Setting up the table
  418. XSSFTable t = s.createTable(new AreaReference("A1:C3", wb.getSpreadsheetVersion()));
  419. t.setName("TableTest");
  420. t.setDisplayName("CT_Table_Test");
  421. t.createColumn("Column 1");
  422. t.createColumn("Column 2");
  423. t.createColumn("Column 3");
  424. t.setCellReferences(wb.getCreationHelper().createAreaReference(
  425. new CellReference(c1), new CellReference(c6)
  426. ));
  427. // Save and re-load
  428. try (XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb)) {
  429. s = wb2.getSheetAt(0);
  430. // Check
  431. assertEquals(1, s.getTables().size());
  432. t = s.getTables().get(0);
  433. assertEquals("A1", t.getStartCellReference().formatAsString());
  434. assertEquals("C2", t.getEndCellReference().formatAsString());
  435. // TODO Nicer column fetching
  436. assertEquals("12", t.getCTTable().getTableColumns().getTableColumnArray(0).getName());
  437. assertEquals("34.56", t.getCTTable().getTableColumns().getTableColumnArray(1).getName());
  438. assertEquals("ABCD", t.getCTTable().getTableColumns().getTableColumnArray(2).getName());
  439. }
  440. }
  441. }
  442. @Test
  443. void testSetDisplayName() throws IOException {
  444. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  445. XSSFSheet sheet = wb.createSheet();
  446. AreaReference reference1 = wb.getCreationHelper().createAreaReference(
  447. new CellReference(0, 0), new CellReference(2, 2));
  448. XSSFTable table1 = sheet.createTable(reference1);
  449. table1.setDisplayName("TableTest");
  450. assertEquals("TableTest", table1.getDisplayName());
  451. assertEquals("TableTest", table1.getCTTable().getDisplayName());
  452. }
  453. }
  454. @Test
  455. void testSetDisplayNameNull() throws IOException {
  456. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  457. XSSFSheet sheet = wb.createSheet();
  458. AreaReference reference1 = wb.getCreationHelper().createAreaReference(
  459. new CellReference(0, 0), new CellReference(2, 2));
  460. XSSFTable table1 = sheet.createTable(reference1);
  461. assertThrows(IllegalArgumentException.class, () -> table1.setDisplayName(null));
  462. }
  463. }
  464. @Test
  465. void testSetDisplayNameEmpty() throws IOException {
  466. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  467. XSSFSheet sheet = wb.createSheet();
  468. AreaReference reference1 = wb.getCreationHelper().createAreaReference(
  469. new CellReference(0, 0), new CellReference(2, 2));
  470. XSSFTable table1 = sheet.createTable(reference1);
  471. assertThrows(IllegalArgumentException.class, () -> table1.setDisplayName(""));
  472. }
  473. }
  474. /**
  475. * Delete table2, and create a named range in sheet0; it should automatically be assigned the name "Table4"
  476. */
  477. @Test
  478. void testBug63401And62906() throws IOException {
  479. try (XSSFWorkbook workbook = new XSSFWorkbook()) {
  480. XSSFSheet sheet0 = workbook.createSheet();
  481. XSSFTable table = addTable(sheet0, 3, 0, 2, 2);
  482. assertNotNull(table);
  483. // final String procName = "testXSSFTableGetName";
  484. // final String name = table.getName();
  485. // System.out.printf(Locale.ROOT, "%s: table.getName=%s%n", procName, name);
  486. }
  487. }
  488. @Test
  489. void testBug65669() throws IOException {
  490. String[] testValues = new String[] {"C'olumn", "Column"};
  491. for (String testValue : testValues) {
  492. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  493. XSSFSheet sheet = wb.createSheet();
  494. // Set the values for the table
  495. for (int i = 0; i < 3; i++) {
  496. // Create row
  497. Row row = sheet.createRow(i);
  498. for (int j = 0; j < 3; j++) {
  499. // Create cell
  500. Cell cell = row.createCell(j);
  501. if (i == 0) {
  502. final String columnName = testValue + (j + 1);
  503. cell.setCellValue(columnName);
  504. } else {
  505. if (j != 2) {
  506. cell.setCellValue((i + 1.0) * (j + 1.0));
  507. }
  508. }
  509. }
  510. }
  511. // Create Table
  512. AreaReference reference = wb.getCreationHelper().createAreaReference(
  513. new CellReference(0, 0), new CellReference(2, 2));
  514. XSSFTable table = sheet.createTable(reference);
  515. table.setName("Table1");
  516. table.setDisplayName("Table1");
  517. for (int i = 1; i < 3; i++) {
  518. Cell cell = sheet.getRow(i).getCell(2);
  519. assertNotNull(cell);
  520. cell.setCellFormula("Table1[[#This Row],[" + testValue + "1]]");
  521. }
  522. }
  523. }
  524. }
  525. private static XSSFTable addTable(XSSFSheet sheet,int nRow, int nCol, int nNumRows, int nNumCols) {
  526. for (int i = 0; i < nNumRows; i++) {
  527. XSSFRow row = sheet.createRow(i + nRow);
  528. for (int j = 0; j < nNumCols; j++) {
  529. XSSFCell localXSSFCell = row.createCell(j + nCol);
  530. if (i == 0) {
  531. localXSSFCell.setCellValue(String.format(Locale.ROOT, "Col%d", j + 1));
  532. } else {
  533. localXSSFCell.setCellValue(String.format(Locale.ROOT, "(%d,%d)", i + 1, j + 1));
  534. }
  535. }
  536. }
  537. final CellReference upperLeft = new CellReference(nRow, nCol);
  538. final CellReference lowerRight = new CellReference(nNumRows - 1, nNumCols - 1);
  539. final AreaReference area = new AreaReference(upperLeft, lowerRight, SpreadsheetVersion.EXCEL2007);
  540. return sheet.createTable(area);
  541. }
  542. @Test
  543. void testNamesWithNewLines() throws IOException {
  544. try (
  545. XSSFWorkbook wb = new XSSFWorkbook();
  546. UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()
  547. ) {
  548. XSSFSheet sheet = wb.createSheet();
  549. // headers
  550. XSSFRow headersRow = sheet.createRow(0);
  551. headersRow.createCell(0).setCellValue("Column1");
  552. headersRow.createCell(1).setCellValue("Column2");
  553. // a second row
  554. XSSFRow row = sheet.createRow(1);
  555. row.createCell(0).setCellValue(1);
  556. row.createCell(1).setCellValue(2);
  557. // create a table
  558. AreaReference area = wb.getCreationHelper().createAreaReference(
  559. new CellReference(sheet.getRow(0).getCell(0)),
  560. new CellReference(sheet.getRow(1).getCell(1))
  561. );
  562. XSSFTable table = sheet.createTable(area);
  563. // styling (no problem here)
  564. sheet.setColumnWidth(0, 5000);
  565. sheet.setColumnWidth(1, 5000);
  566. CTTable cttable = table.getCTTable();
  567. cttable.addNewTableStyleInfo();
  568. XSSFTableStyleInfo style = (XSSFTableStyleInfo) table.getStyle();
  569. style.setName("TableStyleMedium6");
  570. style.setShowColumnStripes(false);
  571. style.setShowRowStripes(true);
  572. cttable.addNewAutoFilter().setRef(area.formatAsString());
  573. CellStyle cellStyle = wb.createCellStyle();
  574. cellStyle.setWrapText(true);
  575. headersRow.getCell(0).setCellStyle(cellStyle);
  576. headersRow.getCell(0).setCellValue("Column1\nwith a line break");
  577. wb.write(bos);
  578. try (XSSFWorkbook wb2 = new XSSFWorkbook(bos.toInputStream())) {
  579. XSSFSheet wb2Sheet = wb2.getSheetAt(0);
  580. List<XSSFTable> tables = wb2Sheet.getTables();
  581. assertEquals(1, tables.size());
  582. XSSFTable wb2Table = tables.get(0);
  583. List<XSSFTableColumn> tabColumns = wb2Table.getColumns();
  584. assertEquals(2, tabColumns.size());
  585. assertEquals("Column1_x000a_with a line break", tabColumns.get(0).getName());
  586. }
  587. }
  588. }
  589. }