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

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