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.

TestXSSFBugs.java 55KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  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 static org.junit.Assert.fail;
  22. import java.io.ByteArrayInputStream;
  23. import java.io.ByteArrayOutputStream;
  24. import java.util.List;
  25. import org.apache.poi.EncryptedDocumentException;
  26. import org.apache.poi.POIDataSamples;
  27. import org.apache.poi.POIXMLDocumentPart;
  28. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  29. import org.apache.poi.openxml4j.opc.OPCPackage;
  30. import org.apache.poi.openxml4j.opc.PackagePart;
  31. import org.apache.poi.openxml4j.opc.PackagingURIHelper;
  32. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  33. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  34. import org.apache.poi.ss.formula.WorkbookEvaluator;
  35. import org.apache.poi.ss.formula.eval.ErrorEval;
  36. import org.apache.poi.ss.formula.eval.ValueEval;
  37. import org.apache.poi.ss.formula.functions.Function;
  38. import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
  39. import org.apache.poi.ss.usermodel.Cell;
  40. import org.apache.poi.ss.usermodel.CellStyle;
  41. import org.apache.poi.ss.usermodel.CellValue;
  42. import org.apache.poi.ss.usermodel.ClientAnchor;
  43. import org.apache.poi.ss.usermodel.Comment;
  44. import org.apache.poi.ss.usermodel.CreationHelper;
  45. import org.apache.poi.ss.usermodel.DataFormatter;
  46. import org.apache.poi.ss.usermodel.Drawing;
  47. import org.apache.poi.ss.usermodel.Font;
  48. import org.apache.poi.ss.usermodel.FormulaError;
  49. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  50. import org.apache.poi.ss.usermodel.IndexedColors;
  51. import org.apache.poi.ss.usermodel.Name;
  52. import org.apache.poi.ss.usermodel.Row;
  53. import org.apache.poi.ss.usermodel.Sheet;
  54. import org.apache.poi.ss.usermodel.Workbook;
  55. import org.apache.poi.ss.usermodel.WorkbookFactory;
  56. import org.apache.poi.ss.util.AreaReference;
  57. import org.apache.poi.ss.util.CellReference;
  58. import org.apache.poi.xssf.XSSFITestDataProvider;
  59. import org.apache.poi.xssf.XSSFTestDataSamples;
  60. import org.apache.poi.xssf.model.CalculationChain;
  61. import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
  62. import org.junit.Ignore;
  63. import org.junit.Test;
  64. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
  65. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
  66. public final class TestXSSFBugs extends BaseTestBugzillaIssues {
  67. public TestXSSFBugs() {
  68. super(XSSFITestDataProvider.instance);
  69. }
  70. /**
  71. * test writing a file with large number of unique strings,
  72. * open resulting file in Excel to check results!
  73. */
  74. @Test
  75. public void bug15375_2() {
  76. bug15375(1000);
  77. }
  78. /**
  79. * Named ranges had the right reference, but
  80. * the wrong sheet name
  81. */
  82. @Test
  83. public void bug45430() {
  84. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("45430.xlsx");
  85. assertFalse(wb.isMacroEnabled());
  86. assertEquals(3, wb.getNumberOfNames());
  87. assertEquals(0, wb.getNameAt(0).getCTName().getLocalSheetId());
  88. assertFalse(wb.getNameAt(0).getCTName().isSetLocalSheetId());
  89. assertEquals("SheetA!$A$1", wb.getNameAt(0).getRefersToFormula());
  90. assertEquals("SheetA", wb.getNameAt(0).getSheetName());
  91. assertEquals(0, wb.getNameAt(1).getCTName().getLocalSheetId());
  92. assertFalse(wb.getNameAt(1).getCTName().isSetLocalSheetId());
  93. assertEquals("SheetB!$A$1", wb.getNameAt(1).getRefersToFormula());
  94. assertEquals("SheetB", wb.getNameAt(1).getSheetName());
  95. assertEquals(0, wb.getNameAt(2).getCTName().getLocalSheetId());
  96. assertFalse(wb.getNameAt(2).getCTName().isSetLocalSheetId());
  97. assertEquals("SheetC!$A$1", wb.getNameAt(2).getRefersToFormula());
  98. assertEquals("SheetC", wb.getNameAt(2).getSheetName());
  99. // Save and re-load, still there
  100. XSSFWorkbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  101. assertEquals(3, nwb.getNumberOfNames());
  102. assertEquals("SheetA!$A$1", nwb.getNameAt(0).getRefersToFormula());
  103. }
  104. /**
  105. * We should carry vba macros over after save
  106. */
  107. @Test
  108. public void bug45431() throws Exception {
  109. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("45431.xlsm");
  110. OPCPackage pkg = wb.getPackage();
  111. assertTrue(wb.isMacroEnabled());
  112. // Check the various macro related bits can be found
  113. PackagePart vba = pkg.getPart(
  114. PackagingURIHelper.createPartName("/xl/vbaProject.bin")
  115. );
  116. assertNotNull(vba);
  117. // And the drawing bit
  118. PackagePart drw = pkg.getPart(
  119. PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml")
  120. );
  121. assertNotNull(drw);
  122. // Save and re-open, both still there
  123. XSSFWorkbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  124. OPCPackage nPkg = nwb.getPackage();
  125. assertTrue(nwb.isMacroEnabled());
  126. vba = nPkg.getPart(
  127. PackagingURIHelper.createPartName("/xl/vbaProject.bin")
  128. );
  129. assertNotNull(vba);
  130. drw = nPkg.getPart(
  131. PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml")
  132. );
  133. assertNotNull(drw);
  134. // And again, just to be sure
  135. nwb = XSSFTestDataSamples.writeOutAndReadBack(nwb);
  136. nPkg = nwb.getPackage();
  137. assertTrue(nwb.isMacroEnabled());
  138. vba = nPkg.getPart(
  139. PackagingURIHelper.createPartName("/xl/vbaProject.bin")
  140. );
  141. assertNotNull(vba);
  142. drw = nPkg.getPart(
  143. PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml")
  144. );
  145. assertNotNull(drw);
  146. }
  147. @Test
  148. public void bug47504() {
  149. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("47504.xlsx");
  150. assertEquals(1, wb.getNumberOfSheets());
  151. XSSFSheet sh = wb.getSheetAt(0);
  152. XSSFDrawing drawing = sh.createDrawingPatriarch();
  153. List<POIXMLDocumentPart> rels = drawing.getRelations();
  154. assertEquals(1, rels.size());
  155. assertEquals("Sheet1!A1", rels.get(0).getPackageRelationship().getTargetURI().getFragment());
  156. // And again, just to be sure
  157. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  158. assertEquals(1, wb.getNumberOfSheets());
  159. sh = wb.getSheetAt(0);
  160. drawing = sh.createDrawingPatriarch();
  161. rels = drawing.getRelations();
  162. assertEquals(1, rels.size());
  163. assertEquals("Sheet1!A1", rels.get(0).getPackageRelationship().getTargetURI().getFragment());
  164. }
  165. /**
  166. * Excel will sometimes write a button with a textbox
  167. * containing &gt;br&lt; (not closed!).
  168. * Clearly Excel shouldn't do this, but test that we can
  169. * read the file despite the naughtyness
  170. */
  171. @Test
  172. public void bug49020() throws Exception {
  173. /*XSSFWorkbook wb =*/ XSSFTestDataSamples.openSampleWorkbook("BrNotClosed.xlsx");
  174. }
  175. /**
  176. * ensure that CTPhoneticPr is loaded by the ooxml test suite so that it is included in poi-ooxml-schemas
  177. */
  178. @Test
  179. public void bug49325() throws Exception {
  180. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("49325.xlsx");
  181. CTWorksheet sh = wb.getSheetAt(0).getCTWorksheet();
  182. assertNotNull(sh.getPhoneticPr());
  183. }
  184. /**
  185. * Names which are defined with a Sheet
  186. * should return that sheet index properly
  187. */
  188. @Test
  189. public void bug48923() throws Exception {
  190. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48923.xlsx");
  191. assertEquals(4, wb.getNumberOfNames());
  192. Name b1 = wb.getName("NameB1");
  193. Name b2 = wb.getName("NameB2");
  194. Name sheet2 = wb.getName("NameSheet2");
  195. Name test = wb.getName("Test");
  196. assertNotNull(b1);
  197. assertEquals("NameB1", b1.getNameName());
  198. assertEquals("Sheet1", b1.getSheetName());
  199. assertEquals(-1, b1.getSheetIndex());
  200. assertNotNull(b2);
  201. assertEquals("NameB2", b2.getNameName());
  202. assertEquals("Sheet1", b2.getSheetName());
  203. assertEquals(-1, b2.getSheetIndex());
  204. assertNotNull(sheet2);
  205. assertEquals("NameSheet2", sheet2.getNameName());
  206. assertEquals("Sheet2", sheet2.getSheetName());
  207. assertEquals(-1, sheet2.getSheetIndex());
  208. assertNotNull(test);
  209. assertEquals("Test", test.getNameName());
  210. assertEquals("Sheet1", test.getSheetName());
  211. assertEquals(-1, test.getSheetIndex());
  212. }
  213. /**
  214. * Problem with evaluation formulas due to
  215. * NameXPtgs.
  216. * Blows up on:
  217. * IF(B6= (ROUNDUP(B6,0) + ROUNDDOWN(B6,0))/2, MROUND(B6,2),ROUND(B6,0))
  218. *
  219. * TODO: delete this test case when MROUND and VAR are implemented
  220. */
  221. @Test
  222. public void bug48539() throws Exception {
  223. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48539.xlsx");
  224. assertEquals(3, wb.getNumberOfSheets());
  225. // Try each cell individually
  226. XSSFFormulaEvaluator eval = new XSSFFormulaEvaluator(wb);
  227. for(int i=0; i<wb.getNumberOfSheets(); i++) {
  228. Sheet s = wb.getSheetAt(i);
  229. for(Row r : s) {
  230. for(Cell c : r) {
  231. if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
  232. CellValue cv = eval.evaluate(c);
  233. if(cv.getCellType() == Cell.CELL_TYPE_NUMERIC) {
  234. // assert that the calculated value agrees with
  235. // the cached formula result calculated by Excel
  236. double cachedFormulaResult = c.getNumericCellValue();
  237. double evaluatedFormulaResult = cv.getNumberValue();
  238. assertEquals(c.getCellFormula(), cachedFormulaResult, evaluatedFormulaResult, 1E-7);
  239. }
  240. }
  241. }
  242. }
  243. }
  244. // Now all of them
  245. XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  246. }
  247. /**
  248. * Foreground colours should be found even if
  249. * a theme is used
  250. */
  251. @Test
  252. public void bug48779() throws Exception {
  253. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48779.xlsx");
  254. XSSFCell cell = wb.getSheetAt(0).getRow(0).getCell(0);
  255. XSSFCellStyle cs = cell.getCellStyle();
  256. assertNotNull(cs);
  257. assertEquals(1, cs.getIndex());
  258. // Look at the low level xml elements
  259. assertEquals(2, cs.getCoreXf().getFillId());
  260. assertEquals(0, cs.getCoreXf().getXfId());
  261. assertEquals(true, cs.getCoreXf().getApplyFill());
  262. XSSFCellFill fg = wb.getStylesSource().getFillAt(2);
  263. assertEquals(0, fg.getFillForegroundColor().getIndexed());
  264. assertEquals(0.0, fg.getFillForegroundColor().getTint(), 0);
  265. assertEquals("FFFF0000", fg.getFillForegroundColor().getARGBHex());
  266. assertEquals(64, fg.getFillBackgroundColor().getIndexed());
  267. // Now look higher up
  268. assertNotNull(cs.getFillForegroundXSSFColor());
  269. assertEquals(0, cs.getFillForegroundColor());
  270. assertEquals("FFFF0000", cs.getFillForegroundXSSFColor().getARGBHex());
  271. assertEquals("FFFF0000", cs.getFillForegroundColorColor().getARGBHex());
  272. assertNotNull(cs.getFillBackgroundColor());
  273. assertEquals(64, cs.getFillBackgroundColor());
  274. assertEquals(null, cs.getFillBackgroundXSSFColor().getARGBHex());
  275. assertEquals(null, cs.getFillBackgroundColorColor().getARGBHex());
  276. }
  277. /**
  278. * With HSSF, if you create a font, don't change it, and
  279. * create a 2nd, you really do get two fonts that you
  280. * can alter as and when you want.
  281. * With XSSF, that wasn't the case, but this verfies
  282. * that it now is again
  283. */
  284. @Test
  285. public void bug48718() throws Exception {
  286. // Verify the HSSF behaviour
  287. // Then ensure the same for XSSF
  288. Workbook[] wbs = new Workbook[] {
  289. new HSSFWorkbook(),
  290. new XSSFWorkbook()
  291. };
  292. int[] initialFonts = new int[] { 4, 1 };
  293. for(int i=0; i<wbs.length; i++) {
  294. Workbook wb = wbs[i];
  295. int startingFonts = initialFonts[i];
  296. assertEquals(startingFonts, wb.getNumberOfFonts());
  297. // Get a font, and slightly change it
  298. Font a = wb.createFont();
  299. assertEquals(startingFonts+1, wb.getNumberOfFonts());
  300. a.setFontHeightInPoints((short)23);
  301. assertEquals(startingFonts+1, wb.getNumberOfFonts());
  302. // Get two more, unchanged
  303. /*Font b =*/ wb.createFont();
  304. assertEquals(startingFonts+2, wb.getNumberOfFonts());
  305. /*Font c =*/ wb.createFont();
  306. assertEquals(startingFonts+3, wb.getNumberOfFonts());
  307. }
  308. }
  309. /**
  310. * Ensure General and @ format are working properly
  311. * for integers
  312. */
  313. @Test
  314. public void bug47490() throws Exception {
  315. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("GeneralFormatTests.xlsx");
  316. Sheet s = wb.getSheetAt(1);
  317. Row r;
  318. DataFormatter df = new DataFormatter();
  319. r = s.getRow(1);
  320. assertEquals(1.0, r.getCell(2).getNumericCellValue(), 0);
  321. assertEquals("General", r.getCell(2).getCellStyle().getDataFormatString());
  322. assertEquals("1", df.formatCellValue(r.getCell(2)));
  323. assertEquals("1", df.formatRawCellContents(1.0, -1, "@"));
  324. assertEquals("1", df.formatRawCellContents(1.0, -1, "General"));
  325. r = s.getRow(2);
  326. assertEquals(12.0, r.getCell(2).getNumericCellValue(), 0);
  327. assertEquals("General", r.getCell(2).getCellStyle().getDataFormatString());
  328. assertEquals("12", df.formatCellValue(r.getCell(2)));
  329. assertEquals("12", df.formatRawCellContents(12.0, -1, "@"));
  330. assertEquals("12", df.formatRawCellContents(12.0, -1, "General"));
  331. r = s.getRow(3);
  332. assertEquals(123.0, r.getCell(2).getNumericCellValue(), 0);
  333. assertEquals("General", r.getCell(2).getCellStyle().getDataFormatString());
  334. assertEquals("123", df.formatCellValue(r.getCell(2)));
  335. assertEquals("123", df.formatRawCellContents(123.0, -1, "@"));
  336. assertEquals("123", df.formatRawCellContents(123.0, -1, "General"));
  337. }
  338. /**
  339. * Ensures that XSSF and HSSF agree with each other,
  340. * and with the docs on when fetching the wrong
  341. * kind of value from a Formula cell
  342. */
  343. @Test
  344. public void bug47815() {
  345. Workbook[] wbs = new Workbook[] {
  346. new HSSFWorkbook(),
  347. new XSSFWorkbook()
  348. };
  349. for(Workbook wb : wbs) {
  350. Sheet s = wb.createSheet();
  351. Row r = s.createRow(0);
  352. // Setup
  353. Cell cn = r.createCell(0, Cell.CELL_TYPE_NUMERIC);
  354. cn.setCellValue(1.2);
  355. Cell cs = r.createCell(1, Cell.CELL_TYPE_STRING);
  356. cs.setCellValue("Testing");
  357. Cell cfn = r.createCell(2, Cell.CELL_TYPE_FORMULA);
  358. cfn.setCellFormula("A1");
  359. Cell cfs = r.createCell(3, Cell.CELL_TYPE_FORMULA);
  360. cfs.setCellFormula("B1");
  361. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  362. assertEquals(Cell.CELL_TYPE_NUMERIC, fe.evaluate(cfn).getCellType());
  363. assertEquals(Cell.CELL_TYPE_STRING, fe.evaluate(cfs).getCellType());
  364. fe.evaluateFormulaCell(cfn);
  365. fe.evaluateFormulaCell(cfs);
  366. // Now test
  367. assertEquals(Cell.CELL_TYPE_NUMERIC, cn.getCellType());
  368. assertEquals(Cell.CELL_TYPE_STRING, cs.getCellType());
  369. assertEquals(Cell.CELL_TYPE_FORMULA, cfn.getCellType());
  370. assertEquals(Cell.CELL_TYPE_NUMERIC, cfn.getCachedFormulaResultType());
  371. assertEquals(Cell.CELL_TYPE_FORMULA, cfs.getCellType());
  372. assertEquals(Cell.CELL_TYPE_STRING, cfs.getCachedFormulaResultType());
  373. // Different ways of retrieving
  374. assertEquals(1.2, cn.getNumericCellValue(), 0);
  375. try {
  376. cn.getRichStringCellValue();
  377. fail();
  378. } catch(IllegalStateException e) {}
  379. assertEquals("Testing", cs.getStringCellValue());
  380. try {
  381. cs.getNumericCellValue();
  382. fail();
  383. } catch(IllegalStateException e) {}
  384. assertEquals(1.2, cfn.getNumericCellValue(), 0);
  385. try {
  386. cfn.getRichStringCellValue();
  387. fail();
  388. } catch(IllegalStateException e) {}
  389. assertEquals("Testing", cfs.getStringCellValue());
  390. try {
  391. cfs.getNumericCellValue();
  392. fail();
  393. } catch(IllegalStateException e) {}
  394. }
  395. }
  396. /**
  397. * A problem file from a non-standard source (a scientific instrument that saves its
  398. * output as an .xlsx file) that have two issues:
  399. * 1. The Content Type part name is lower-case: [content_types].xml
  400. * 2. The file appears to use backslashes as path separators
  401. *
  402. * The OPC spec tolerates both of these peculiarities, so does POI
  403. */
  404. @Test
  405. public void bug49609() throws Exception {
  406. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("49609.xlsx");
  407. assertEquals("FAM", wb.getSheetName(0));
  408. assertEquals("Cycle", wb.getSheetAt(0).getRow(0).getCell(1).getStringCellValue());
  409. }
  410. @Test
  411. public void bug49783() throws Exception {
  412. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("49783.xlsx");
  413. Sheet sheet = wb.getSheetAt(0);
  414. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  415. Cell cell;
  416. cell = sheet.getRow(0).getCell(0);
  417. assertEquals("#REF!*#REF!", cell.getCellFormula());
  418. assertEquals(Cell.CELL_TYPE_ERROR, evaluator.evaluateInCell(cell).getCellType());
  419. assertEquals("#REF!", FormulaError.forInt(cell.getErrorCellValue()).getString());
  420. Name nm1 = wb.getName("sale_1");
  421. assertNotNull("name sale_1 should be present", nm1);
  422. assertEquals("Sheet1!#REF!", nm1.getRefersToFormula());
  423. Name nm2 = wb.getName("sale_2");
  424. assertNotNull("name sale_2 should be present", nm2);
  425. assertEquals("Sheet1!#REF!", nm2.getRefersToFormula());
  426. cell = sheet.getRow(1).getCell(0);
  427. assertEquals("sale_1*sale_2", cell.getCellFormula());
  428. assertEquals(Cell.CELL_TYPE_ERROR, evaluator.evaluateInCell(cell).getCellType());
  429. assertEquals("#REF!", FormulaError.forInt(cell.getErrorCellValue()).getString());
  430. }
  431. /**
  432. * Creating a rich string of "hello world" and applying
  433. * a font to characters 1-5 means we have two strings,
  434. * "hello" and " world". As such, we need to apply
  435. * preserve spaces to the 2nd bit, lest we end up
  436. * with something like "helloworld" !
  437. */
  438. @Test
  439. public void bug49941() throws Exception {
  440. XSSFWorkbook wb = new XSSFWorkbook();
  441. XSSFSheet s = wb.createSheet();
  442. XSSFRow r = s.createRow(0);
  443. XSSFCell c = r.createCell(0);
  444. // First without fonts
  445. c.setCellValue(
  446. new XSSFRichTextString(" with spaces ")
  447. );
  448. assertEquals(" with spaces ", c.getRichStringCellValue().toString());
  449. assertEquals(0, c.getRichStringCellValue().getCTRst().sizeOfRArray());
  450. assertEquals(true, c.getRichStringCellValue().getCTRst().isSetT());
  451. // Should have the preserve set
  452. assertEquals(
  453. 1,
  454. c.getRichStringCellValue().getCTRst().xgetT().getDomNode().getAttributes().getLength()
  455. );
  456. assertEquals(
  457. "preserve",
  458. c.getRichStringCellValue().getCTRst().xgetT().getDomNode().getAttributes().item(0).getNodeValue()
  459. );
  460. // Save and check
  461. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  462. s = wb.getSheetAt(0);
  463. r = s.getRow(0);
  464. c = r.getCell(0);
  465. assertEquals(" with spaces ", c.getRichStringCellValue().toString());
  466. assertEquals(0, c.getRichStringCellValue().getCTRst().sizeOfRArray());
  467. assertEquals(true, c.getRichStringCellValue().getCTRst().isSetT());
  468. // Change the string
  469. c.setCellValue(
  470. new XSSFRichTextString("hello world")
  471. );
  472. assertEquals("hello world", c.getRichStringCellValue().toString());
  473. // Won't have preserve
  474. assertEquals(
  475. 0,
  476. c.getRichStringCellValue().getCTRst().xgetT().getDomNode().getAttributes().getLength()
  477. );
  478. // Apply a font
  479. XSSFFont f = wb.createFont();
  480. f.setBold(true);
  481. c.getRichStringCellValue().applyFont(0, 5, f);
  482. assertEquals("hello world", c.getRichStringCellValue().toString());
  483. // Does need preserving on the 2nd part
  484. assertEquals(2, c.getRichStringCellValue().getCTRst().sizeOfRArray());
  485. assertEquals(
  486. 0,
  487. c.getRichStringCellValue().getCTRst().getRArray(0).xgetT().getDomNode().getAttributes().getLength()
  488. );
  489. assertEquals(
  490. 1,
  491. c.getRichStringCellValue().getCTRst().getRArray(1).xgetT().getDomNode().getAttributes().getLength()
  492. );
  493. assertEquals(
  494. "preserve",
  495. c.getRichStringCellValue().getCTRst().getRArray(1).xgetT().getDomNode().getAttributes().item(0).getNodeValue()
  496. );
  497. // Save and check
  498. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  499. s = wb.getSheetAt(0);
  500. r = s.getRow(0);
  501. c = r.getCell(0);
  502. assertEquals("hello world", c.getRichStringCellValue().toString());
  503. }
  504. /**
  505. * Repeatedly writing the same file which has styles
  506. */
  507. @Test
  508. public void bug49940() throws Exception {
  509. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("styles.xlsx");
  510. assertEquals(3, wb.getNumberOfSheets());
  511. assertEquals(10, wb.getStylesSource().getNumCellStyles());
  512. ByteArrayOutputStream b1 = new ByteArrayOutputStream();
  513. ByteArrayOutputStream b2 = new ByteArrayOutputStream();
  514. ByteArrayOutputStream b3 = new ByteArrayOutputStream();
  515. wb.write(b1);
  516. wb.write(b2);
  517. wb.write(b3);
  518. for(byte[] data : new byte[][] {
  519. b1.toByteArray(), b2.toByteArray(), b3.toByteArray()
  520. }) {
  521. ByteArrayInputStream bais = new ByteArrayInputStream(data);
  522. wb = new XSSFWorkbook(bais);
  523. assertEquals(3, wb.getNumberOfSheets());
  524. assertEquals(10, wb.getStylesSource().getNumCellStyles());
  525. }
  526. }
  527. /**
  528. * Various ways of removing a cell formula should all zap
  529. * the calcChain entry.
  530. */
  531. @Test
  532. public void bug49966() throws Exception {
  533. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("shared_formulas.xlsx");
  534. XSSFSheet sheet = wb.getSheetAt(0);
  535. // CalcChain has lots of entries
  536. CalculationChain cc = wb.getCalculationChain();
  537. assertEquals("A2", cc.getCTCalcChain().getCArray(0).getR());
  538. assertEquals("A3", cc.getCTCalcChain().getCArray(1).getR());
  539. assertEquals("A4", cc.getCTCalcChain().getCArray(2).getR());
  540. assertEquals("A5", cc.getCTCalcChain().getCArray(3).getR());
  541. assertEquals("A6", cc.getCTCalcChain().getCArray(4).getR());
  542. assertEquals("A7", cc.getCTCalcChain().getCArray(5).getR());
  543. assertEquals("A8", cc.getCTCalcChain().getCArray(6).getR());
  544. assertEquals(40, cc.getCTCalcChain().sizeOfCArray());
  545. // Try various ways of changing the formulas
  546. // If it stays a formula, chain entry should remain
  547. // Otherwise should go
  548. sheet.getRow(1).getCell(0).setCellFormula("A1"); // stay
  549. sheet.getRow(2).getCell(0).setCellFormula(null); // go
  550. sheet.getRow(3).getCell(0).setCellType(Cell.CELL_TYPE_FORMULA); // stay
  551. sheet.getRow(4).getCell(0).setCellType(Cell.CELL_TYPE_STRING); // go
  552. sheet.getRow(5).removeCell(
  553. sheet.getRow(5).getCell(0) // go
  554. );
  555. sheet.getRow(6).getCell(0).setCellType(Cell.CELL_TYPE_BLANK); // go
  556. sheet.getRow(7).getCell(0).setCellValue((String)null); // go
  557. // Save and check
  558. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  559. assertEquals(35, cc.getCTCalcChain().sizeOfCArray());
  560. cc = wb.getCalculationChain();
  561. assertEquals("A2", cc.getCTCalcChain().getCArray(0).getR());
  562. assertEquals("A4", cc.getCTCalcChain().getCArray(1).getR());
  563. assertEquals("A9", cc.getCTCalcChain().getCArray(2).getR());
  564. }
  565. @Test
  566. public void bug49156() throws Exception {
  567. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("49156.xlsx");
  568. FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  569. Sheet sheet = wb.getSheetAt(0);
  570. for(Row row : sheet){
  571. for(Cell cell : row){
  572. if(cell.getCellType() == Cell.CELL_TYPE_FORMULA){
  573. formulaEvaluator.evaluateInCell(cell); // caused NPE on some cells
  574. }
  575. }
  576. }
  577. }
  578. /**
  579. * Newlines are valid characters in a formula
  580. */
  581. @Test
  582. public void bug50440And51875() throws Exception {
  583. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("NewlineInFormulas.xlsx");
  584. Sheet s = wb.getSheetAt(0);
  585. Cell c = s.getRow(0).getCell(0);
  586. assertEquals("SUM(\n1,2\n)", c.getCellFormula());
  587. assertEquals(3.0, c.getNumericCellValue(), 0);
  588. FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  589. formulaEvaluator.evaluateFormulaCell(c);
  590. assertEquals("SUM(\n1,2\n)", c.getCellFormula());
  591. assertEquals(3.0, c.getNumericCellValue(), 0);
  592. // For 51875
  593. Cell b3 = s.getRow(2).getCell(1);
  594. formulaEvaluator.evaluateFormulaCell(b3);
  595. assertEquals("B1+B2", b3.getCellFormula()); // The newline is lost for shared formulas
  596. assertEquals(3.0, b3.getNumericCellValue(), 0);
  597. }
  598. /**
  599. * Moving a cell comment from one cell to another
  600. */
  601. @Test
  602. public void bug50795() throws Exception {
  603. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50795.xlsx");
  604. XSSFSheet sheet = wb.getSheetAt(0);
  605. XSSFRow row = sheet.getRow(0);
  606. XSSFCell cellWith = row.getCell(0);
  607. XSSFCell cellWithoutComment = row.getCell(1);
  608. assertNotNull(cellWith.getCellComment());
  609. assertNull(cellWithoutComment.getCellComment());
  610. String exp = "\u0410\u0432\u0442\u043e\u0440:\ncomment";
  611. XSSFComment comment = cellWith.getCellComment();
  612. assertEquals(exp, comment.getString().getString());
  613. // Check we can write it out and read it back as-is
  614. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  615. sheet = wb.getSheetAt(0);
  616. row = sheet.getRow(0);
  617. cellWith = row.getCell(0);
  618. cellWithoutComment = row.getCell(1);
  619. // Double check things are as expected
  620. assertNotNull(cellWith.getCellComment());
  621. assertNull(cellWithoutComment.getCellComment());
  622. comment = cellWith.getCellComment();
  623. assertEquals(exp, comment.getString().getString());
  624. // Move the comment
  625. cellWithoutComment.setCellComment(comment);
  626. // Write out and re-check
  627. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  628. sheet = wb.getSheetAt(0);
  629. row = sheet.getRow(0);
  630. // Ensure it swapped over
  631. cellWith = row.getCell(0);
  632. cellWithoutComment = row.getCell(1);
  633. assertNull(cellWith.getCellComment());
  634. assertNotNull(cellWithoutComment.getCellComment());
  635. comment = cellWithoutComment.getCellComment();
  636. assertEquals(exp, comment.getString().getString());
  637. }
  638. /**
  639. * When the cell background colour is set with one of the first
  640. * two columns of the theme colour palette, the colours are
  641. * shades of white or black.
  642. * For those cases, ensure we don't break on reading the colour
  643. */
  644. @Test
  645. public void bug50299() throws Exception {
  646. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("50299.xlsx");
  647. // Check all the colours
  648. for(int sn=0; sn<wb.getNumberOfSheets(); sn++) {
  649. Sheet s = wb.getSheetAt(sn);
  650. for(Row r : s) {
  651. for(Cell c : r) {
  652. CellStyle cs = c.getCellStyle();
  653. if(cs != null) {
  654. cs.getFillForegroundColor();
  655. }
  656. }
  657. }
  658. }
  659. // Check one bit in detail
  660. // Check that we get back foreground=0 for the theme colours,
  661. // and background=64 for the auto colouring
  662. Sheet s = wb.getSheetAt(0);
  663. assertEquals(0, s.getRow(0).getCell(8).getCellStyle().getFillForegroundColor());
  664. assertEquals(64, s.getRow(0).getCell(8).getCellStyle().getFillBackgroundColor());
  665. assertEquals(0, s.getRow(1).getCell(8).getCellStyle().getFillForegroundColor());
  666. assertEquals(64, s.getRow(1).getCell(8).getCellStyle().getFillBackgroundColor());
  667. }
  668. /**
  669. * Excel .xls style indexed colours in a .xlsx file
  670. */
  671. @Test
  672. public void bug50786() throws Exception {
  673. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50786-indexed_colours.xlsx");
  674. XSSFSheet s = wb.getSheetAt(0);
  675. XSSFRow r = s.getRow(2);
  676. // Check we have the right cell
  677. XSSFCell c = r.getCell(1);
  678. assertEquals("test\u00a0", c.getRichStringCellValue().getString());
  679. // It should be light green
  680. XSSFCellStyle cs = c.getCellStyle();
  681. assertEquals(42, cs.getFillForegroundColor());
  682. assertEquals(42, cs.getFillForegroundColorColor().getIndexed());
  683. assertNotNull(cs.getFillForegroundColorColor().getRgb());
  684. assertEquals("FFCCFFCC", cs.getFillForegroundColorColor().getARGBHex());
  685. }
  686. /**
  687. * If the border colours are set with themes, then we
  688. * should still be able to get colours
  689. */
  690. @Test
  691. public void bug50846() throws Exception {
  692. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50846-border_colours.xlsx");
  693. XSSFSheet sheet = wb.getSheetAt(0);
  694. XSSFRow row = sheet.getRow(0);
  695. // Border from a theme, brown
  696. XSSFCell cellT = row.getCell(0);
  697. XSSFCellStyle styleT = cellT.getCellStyle();
  698. XSSFColor colorT = styleT.getBottomBorderXSSFColor();
  699. assertEquals(5, colorT.getTheme());
  700. assertEquals("FFC0504D", colorT.getARGBHex());
  701. // Border from a style direct, red
  702. XSSFCell cellS = row.getCell(1);
  703. XSSFCellStyle styleS = cellS.getCellStyle();
  704. XSSFColor colorS = styleS.getBottomBorderXSSFColor();
  705. assertEquals(0, colorS.getTheme());
  706. assertEquals("FFFF0000", colorS.getARGBHex());
  707. }
  708. /**
  709. * Fonts where their colours come from the theme rather
  710. * then being set explicitly still should allow the
  711. * fetching of the RGB.
  712. */
  713. @Test
  714. public void bug50784() throws Exception {
  715. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50784-font_theme_colours.xlsx");
  716. XSSFSheet s = wb.getSheetAt(0);
  717. XSSFRow r = s.getRow(0);
  718. // Column 1 has a font with regular colours
  719. XSSFCell cr = r.getCell(1);
  720. XSSFFont fr = wb.getFontAt( cr.getCellStyle().getFontIndex() );
  721. XSSFColor colr = fr.getXSSFColor();
  722. // No theme, has colours
  723. assertEquals(0, colr.getTheme());
  724. assertNotNull( colr.getRgb() );
  725. // Column 0 has a font with colours from a theme
  726. XSSFCell ct = r.getCell(0);
  727. XSSFFont ft = wb.getFontAt( ct.getCellStyle().getFontIndex() );
  728. XSSFColor colt = ft.getXSSFColor();
  729. // Has a theme, which has the colours on it
  730. assertEquals(9, colt.getTheme());
  731. XSSFColor themeC = wb.getTheme().getThemeColor(colt.getTheme());
  732. assertNotNull( themeC.getRgb() );
  733. assertNotNull( colt.getRgb() );
  734. assertEquals( themeC.getARGBHex(), colt.getARGBHex() ); // The same colour
  735. }
  736. /**
  737. * New lines were being eaten when setting a font on
  738. * a rich text string
  739. */
  740. @Test
  741. public void bug48877() throws Exception {
  742. String text = "Use \n with word wrap on to create a new line.\n" +
  743. "This line finishes with two trailing spaces. ";
  744. XSSFWorkbook wb = new XSSFWorkbook();
  745. XSSFSheet sheet = wb.createSheet();
  746. Font font1 = wb.createFont();
  747. font1.setColor((short) 20);
  748. Font font2 = wb.createFont();
  749. font2.setColor(Font.COLOR_RED);
  750. Font font3 = wb.getFontAt((short)0);
  751. XSSFRow row = sheet.createRow(2);
  752. XSSFCell cell = row.createCell(2);
  753. XSSFRichTextString richTextString =
  754. wb.getCreationHelper().createRichTextString(text);
  755. // Check the text has the newline
  756. assertEquals(text, richTextString.getString());
  757. // Apply the font
  758. richTextString.applyFont(font3);
  759. richTextString.applyFont(0, 3, font1);
  760. cell.setCellValue(richTextString);
  761. // To enable newlines you need set a cell styles with wrap=true
  762. CellStyle cs = wb.createCellStyle();
  763. cs.setWrapText(true);
  764. cell.setCellStyle(cs);
  765. // Check the text has the
  766. assertEquals(text, cell.getStringCellValue());
  767. // Save the file and re-read it
  768. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  769. sheet = wb.getSheetAt(0);
  770. row = sheet.getRow(2);
  771. cell = row.getCell(2);
  772. assertEquals(text, cell.getStringCellValue());
  773. // Now add a 2nd, and check again
  774. int fontAt = text.indexOf("\n", 6);
  775. cell.getRichStringCellValue().applyFont(10, fontAt+1, font2);
  776. assertEquals(text, cell.getStringCellValue());
  777. assertEquals(4, cell.getRichStringCellValue().numFormattingRuns());
  778. assertEquals("Use", cell.getRichStringCellValue().getCTRst().getRList().get(0).getT());
  779. String r3 = cell.getRichStringCellValue().getCTRst().getRList().get(2).getT();
  780. assertEquals("line.\n", r3.substring(r3.length()-6));
  781. // Save and re-check
  782. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  783. sheet = wb.getSheetAt(0);
  784. row = sheet.getRow(2);
  785. cell = row.getCell(2);
  786. assertEquals(text, cell.getStringCellValue());
  787. // FileOutputStream out = new FileOutputStream("/tmp/test48877.xlsx");
  788. // wb.write(out);
  789. // out.close();
  790. }
  791. /**
  792. * Adding sheets when one has a table, then re-ordering
  793. */
  794. @Test
  795. public void bug50867() throws Exception {
  796. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50867_with_table.xlsx");
  797. assertEquals(3, wb.getNumberOfSheets());
  798. XSSFSheet s1 = wb.getSheetAt(0);
  799. XSSFSheet s2 = wb.getSheetAt(1);
  800. XSSFSheet s3 = wb.getSheetAt(2);
  801. assertEquals(1, s1.getTables().size());
  802. assertEquals(0, s2.getTables().size());
  803. assertEquals(0, s3.getTables().size());
  804. XSSFTable t = s1.getTables().get(0);
  805. assertEquals("Tabella1", t.getName());
  806. assertEquals("Tabella1", t.getDisplayName());
  807. assertEquals("A1:C3", t.getCTTable().getRef());
  808. // Add a sheet and re-order
  809. XSSFSheet s4 = wb.createSheet("NewSheet");
  810. wb.setSheetOrder(s4.getSheetName(), 0);
  811. // Check on tables
  812. assertEquals(1, s1.getTables().size());
  813. assertEquals(0, s2.getTables().size());
  814. assertEquals(0, s3.getTables().size());
  815. assertEquals(0, s4.getTables().size());
  816. // Refetch to get the new order
  817. s1 = wb.getSheetAt(0);
  818. s2 = wb.getSheetAt(1);
  819. s3 = wb.getSheetAt(2);
  820. s4 = wb.getSheetAt(3);
  821. assertEquals(0, s1.getTables().size());
  822. assertEquals(1, s2.getTables().size());
  823. assertEquals(0, s3.getTables().size());
  824. assertEquals(0, s4.getTables().size());
  825. // Save and re-load
  826. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  827. s1 = wb.getSheetAt(0);
  828. s2 = wb.getSheetAt(1);
  829. s3 = wb.getSheetAt(2);
  830. s4 = wb.getSheetAt(3);
  831. assertEquals(0, s1.getTables().size());
  832. assertEquals(1, s2.getTables().size());
  833. assertEquals(0, s3.getTables().size());
  834. assertEquals(0, s4.getTables().size());
  835. t = s2.getTables().get(0);
  836. assertEquals("Tabella1", t.getName());
  837. assertEquals("Tabella1", t.getDisplayName());
  838. assertEquals("A1:C3", t.getCTTable().getRef());
  839. // Add some more tables, and check
  840. t = s2.createTable();
  841. t.setName("New 2");
  842. t.setDisplayName("New 2");
  843. t = s3.createTable();
  844. t.setName("New 3");
  845. t.setDisplayName("New 3");
  846. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  847. s1 = wb.getSheetAt(0);
  848. s2 = wb.getSheetAt(1);
  849. s3 = wb.getSheetAt(2);
  850. s4 = wb.getSheetAt(3);
  851. assertEquals(0, s1.getTables().size());
  852. assertEquals(2, s2.getTables().size());
  853. assertEquals(1, s3.getTables().size());
  854. assertEquals(0, s4.getTables().size());
  855. t = s2.getTables().get(0);
  856. assertEquals("Tabella1", t.getName());
  857. assertEquals("Tabella1", t.getDisplayName());
  858. assertEquals("A1:C3", t.getCTTable().getRef());
  859. t = s2.getTables().get(1);
  860. assertEquals("New 2", t.getName());
  861. assertEquals("New 2", t.getDisplayName());
  862. t = s3.getTables().get(0);
  863. assertEquals("New 3", t.getName());
  864. assertEquals("New 3", t.getDisplayName());
  865. // Check the relationships
  866. assertEquals(0, s1.getRelations().size());
  867. assertEquals(3, s2.getRelations().size());
  868. assertEquals(1, s3.getRelations().size());
  869. assertEquals(0, s4.getRelations().size());
  870. assertEquals(
  871. XSSFRelation.PRINTER_SETTINGS.getContentType(),
  872. s2.getRelations().get(0).getPackagePart().getContentType()
  873. );
  874. assertEquals(
  875. XSSFRelation.TABLE.getContentType(),
  876. s2.getRelations().get(1).getPackagePart().getContentType()
  877. );
  878. assertEquals(
  879. XSSFRelation.TABLE.getContentType(),
  880. s2.getRelations().get(2).getPackagePart().getContentType()
  881. );
  882. assertEquals(
  883. XSSFRelation.TABLE.getContentType(),
  884. s3.getRelations().get(0).getPackagePart().getContentType()
  885. );
  886. assertEquals(
  887. "/xl/tables/table3.xml",
  888. s3.getRelations().get(0).getPackagePart().getPartName().toString()
  889. );
  890. }
  891. /**
  892. * Setting repeating rows and columns shouldn't break
  893. * any print settings that were there before
  894. */
  895. @SuppressWarnings("deprecation")
  896. @Test
  897. public void bug49253() throws Exception {
  898. XSSFWorkbook wb1 = new XSSFWorkbook();
  899. XSSFWorkbook wb2 = new XSSFWorkbook();
  900. // No print settings before repeating
  901. XSSFSheet s1 = wb1.createSheet();
  902. assertEquals(false, s1.getCTWorksheet().isSetPageSetup());
  903. assertEquals(true, s1.getCTWorksheet().isSetPageMargins());
  904. wb1.setRepeatingRowsAndColumns(0, 2, 3, 1, 2);
  905. assertEquals(true, s1.getCTWorksheet().isSetPageSetup());
  906. assertEquals(true, s1.getCTWorksheet().isSetPageMargins());
  907. XSSFPrintSetup ps1 = s1.getPrintSetup();
  908. assertEquals(false, ps1.getValidSettings());
  909. assertEquals(false, ps1.getLandscape());
  910. // Had valid print settings before repeating
  911. XSSFSheet s2 = wb2.createSheet();
  912. XSSFPrintSetup ps2 = s2.getPrintSetup();
  913. assertEquals(true, s2.getCTWorksheet().isSetPageSetup());
  914. assertEquals(true, s2.getCTWorksheet().isSetPageMargins());
  915. ps2.setLandscape(false);
  916. assertEquals(true, ps2.getValidSettings());
  917. assertEquals(false, ps2.getLandscape());
  918. wb2.setRepeatingRowsAndColumns(0, 2, 3, 1, 2);
  919. ps2 = s2.getPrintSetup();
  920. assertEquals(true, s2.getCTWorksheet().isSetPageSetup());
  921. assertEquals(true, s2.getCTWorksheet().isSetPageMargins());
  922. assertEquals(true, ps2.getValidSettings());
  923. assertEquals(false, ps2.getLandscape());
  924. }
  925. /**
  926. * Default Column style
  927. */
  928. @Test
  929. public void bug51037() throws Exception {
  930. XSSFWorkbook wb = new XSSFWorkbook();
  931. XSSFSheet s = wb.createSheet();
  932. CellStyle defaultStyle = wb.getCellStyleAt((short)0);
  933. assertEquals(0, defaultStyle.getIndex());
  934. CellStyle blueStyle = wb.createCellStyle();
  935. blueStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());
  936. blueStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  937. assertEquals(1, blueStyle.getIndex());
  938. CellStyle pinkStyle = wb.createCellStyle();
  939. pinkStyle.setFillForegroundColor(IndexedColors.PINK.getIndex());
  940. pinkStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  941. assertEquals(2, pinkStyle.getIndex());
  942. // Starts empty
  943. assertEquals(1, s.getCTWorksheet().sizeOfColsArray());
  944. CTCols cols = s.getCTWorksheet().getColsArray(0);
  945. assertEquals(0, cols.sizeOfColArray());
  946. // Add some rows and columns
  947. XSSFRow r1 = s.createRow(0);
  948. XSSFRow r2 = s.createRow(1);
  949. r1.createCell(0);
  950. r1.createCell(2);
  951. r2.createCell(0);
  952. r2.createCell(3);
  953. // Check no style is there
  954. assertEquals(1, s.getCTWorksheet().sizeOfColsArray());
  955. assertEquals(0, cols.sizeOfColArray());
  956. assertEquals(defaultStyle, s.getColumnStyle(0));
  957. assertEquals(defaultStyle, s.getColumnStyle(2));
  958. assertEquals(defaultStyle, s.getColumnStyle(3));
  959. // Apply the styles
  960. s.setDefaultColumnStyle(0, pinkStyle);
  961. s.setDefaultColumnStyle(3, blueStyle);
  962. // Check
  963. assertEquals(pinkStyle, s.getColumnStyle(0));
  964. assertEquals(defaultStyle, s.getColumnStyle(2));
  965. assertEquals(blueStyle, s.getColumnStyle(3));
  966. assertEquals(1, s.getCTWorksheet().sizeOfColsArray());
  967. assertEquals(2, cols.sizeOfColArray());
  968. assertEquals(1, cols.getColArray(0).getMin());
  969. assertEquals(1, cols.getColArray(0).getMax());
  970. assertEquals(pinkStyle.getIndex(), cols.getColArray(0).getStyle());
  971. assertEquals(4, cols.getColArray(1).getMin());
  972. assertEquals(4, cols.getColArray(1).getMax());
  973. assertEquals(blueStyle.getIndex(), cols.getColArray(1).getStyle());
  974. // Save, re-load and re-check
  975. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  976. s = wb.getSheetAt(0);
  977. defaultStyle = wb.getCellStyleAt(defaultStyle.getIndex());
  978. blueStyle = wb.getCellStyleAt(blueStyle.getIndex());
  979. pinkStyle = wb.getCellStyleAt(pinkStyle.getIndex());
  980. assertEquals(pinkStyle, s.getColumnStyle(0));
  981. assertEquals(defaultStyle, s.getColumnStyle(2));
  982. assertEquals(blueStyle, s.getColumnStyle(3));
  983. }
  984. /**
  985. * Repeatedly writing a file.
  986. * Something with the SharedStringsTable currently breaks...
  987. */
  988. @Ignore
  989. public void bug46662() throws Exception {
  990. // New file
  991. XSSFWorkbook wb = new XSSFWorkbook();
  992. XSSFTestDataSamples.writeOutAndReadBack(wb);
  993. XSSFTestDataSamples.writeOutAndReadBack(wb);
  994. XSSFTestDataSamples.writeOutAndReadBack(wb);
  995. // Simple file
  996. wb = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx");
  997. XSSFTestDataSamples.writeOutAndReadBack(wb);
  998. XSSFTestDataSamples.writeOutAndReadBack(wb);
  999. XSSFTestDataSamples.writeOutAndReadBack(wb);
  1000. // Complex file
  1001. // TODO
  1002. }
  1003. /**
  1004. * Colours and styles when the list has gaps in it
  1005. */
  1006. @Test
  1007. public void bug51222() throws Exception {
  1008. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51222.xlsx");
  1009. XSSFSheet s = wb.getSheetAt(0);
  1010. XSSFCell cA4_EEECE1 = s.getRow(3).getCell(0);
  1011. XSSFCell cA5_1F497D = s.getRow(4).getCell(0);
  1012. // Check the text
  1013. assertEquals("A4", cA4_EEECE1.getRichStringCellValue().getString());
  1014. assertEquals("A5", cA5_1F497D.getRichStringCellValue().getString());
  1015. // Check the styles assigned to them
  1016. assertEquals(4, cA4_EEECE1.getCTCell().getS());
  1017. assertEquals(5, cA5_1F497D.getCTCell().getS());
  1018. // Check we look up the correct style
  1019. assertEquals(4, cA4_EEECE1.getCellStyle().getIndex());
  1020. assertEquals(5, cA5_1F497D.getCellStyle().getIndex());
  1021. // Check the fills on them at the low level
  1022. assertEquals(5, cA4_EEECE1.getCellStyle().getCoreXf().getFillId());
  1023. assertEquals(6, cA5_1F497D.getCellStyle().getCoreXf().getFillId());
  1024. // These should reference themes 2 and 3
  1025. assertEquals(2, wb.getStylesSource().getFillAt(5).getCTFill().getPatternFill().getFgColor().getTheme());
  1026. assertEquals(3, wb.getStylesSource().getFillAt(6).getCTFill().getPatternFill().getFgColor().getTheme());
  1027. // Ensure we get the right colours for these themes
  1028. // TODO fix
  1029. // assertEquals("FFEEECE1", wb.getTheme().getThemeColor(2).getARGBHex());
  1030. // assertEquals("FF1F497D", wb.getTheme().getThemeColor(3).getARGBHex());
  1031. // Finally check the colours on the styles
  1032. // TODO fix
  1033. // assertEquals("FFEEECE1", cA4_EEECE1.getCellStyle().getFillForegroundXSSFColor().getARGBHex());
  1034. // assertEquals("FF1F497D", cA5_1F497D.getCellStyle().getFillForegroundXSSFColor().getARGBHex());
  1035. }
  1036. @Test
  1037. public void bug51470() throws Exception {
  1038. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51470.xlsx");
  1039. XSSFSheet sh0 = wb.getSheetAt(0);
  1040. XSSFSheet sh1 = wb.cloneSheet(0);
  1041. List<POIXMLDocumentPart> rels0 = sh0.getRelations();
  1042. List<POIXMLDocumentPart> rels1 = sh1.getRelations();
  1043. assertEquals(1, rels0.size());
  1044. assertEquals(1, rels1.size());
  1045. assertEquals(rels0.get(0).getPackageRelationship(), rels1.get(0).getPackageRelationship());
  1046. }
  1047. /**
  1048. * Add comments to Sheet 1, when Sheet 2 already has
  1049. * comments (so /xl/comments1.xml is taken)
  1050. */
  1051. @Test
  1052. public void bug51850() {
  1053. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51850.xlsx");
  1054. XSSFSheet sh1 = wb.getSheetAt(0);
  1055. XSSFSheet sh2 = wb.getSheetAt(1);
  1056. // Sheet 2 has comments
  1057. assertNotNull(sh2.getCommentsTable(false));
  1058. assertEquals(1, sh2.getCommentsTable(false).getNumberOfComments());
  1059. // Sheet 1 doesn't (yet)
  1060. assertNull(sh1.getCommentsTable(false));
  1061. // Try to add comments to Sheet 1
  1062. CreationHelper factory = wb.getCreationHelper();
  1063. Drawing drawing = sh1.createDrawingPatriarch();
  1064. ClientAnchor anchor = factory.createClientAnchor();
  1065. anchor.setCol1(0);
  1066. anchor.setCol2(4);
  1067. anchor.setRow1(0);
  1068. anchor.setRow2(1);
  1069. Comment comment1 = drawing.createCellComment(anchor);
  1070. comment1.setString(
  1071. factory.createRichTextString("I like this cell. It's my favourite."));
  1072. comment1.setAuthor("Bob T. Fish");
  1073. Comment comment2 = drawing.createCellComment(anchor);
  1074. comment2.setString(
  1075. factory.createRichTextString("This is much less fun..."));
  1076. comment2.setAuthor("Bob T. Fish");
  1077. Cell c1 = sh1.getRow(0).createCell(4);
  1078. c1.setCellValue(2.3);
  1079. c1.setCellComment(comment1);
  1080. Cell c2 = sh1.getRow(0).createCell(5);
  1081. c2.setCellValue(2.1);
  1082. c2.setCellComment(comment2);
  1083. // Save and re-load
  1084. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  1085. sh1 = wb.getSheetAt(0);
  1086. sh2 = wb.getSheetAt(1);
  1087. // Check the comments
  1088. assertNotNull(sh2.getCommentsTable(false));
  1089. assertEquals(1, sh2.getCommentsTable(false).getNumberOfComments());
  1090. assertNotNull(sh1.getCommentsTable(false));
  1091. assertEquals(2, sh1.getCommentsTable(false).getNumberOfComments());
  1092. }
  1093. /**
  1094. * Sheet names with a , in them
  1095. */
  1096. @Test
  1097. public void bug51963() throws Exception {
  1098. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51963.xlsx");
  1099. XSSFSheet sheet = wb.getSheetAt(0);
  1100. assertEquals("Abc,1", sheet.getSheetName());
  1101. Name name = wb.getName("Intekon.ProdCodes");
  1102. assertEquals("'Abc,1'!$A$1:$A$2", name.getRefersToFormula());
  1103. AreaReference ref = new AreaReference(name.getRefersToFormula());
  1104. assertEquals(0, ref.getFirstCell().getRow());
  1105. assertEquals(0, ref.getFirstCell().getCol());
  1106. assertEquals(1, ref.getLastCell().getRow());
  1107. assertEquals(0, ref.getLastCell().getCol());
  1108. }
  1109. /**
  1110. * Sum across multiple workbooks
  1111. * eg =SUM($Sheet1.C1:$Sheet4.C1)
  1112. * DISABLED As we can't currently evaluate these
  1113. */
  1114. @Ignore
  1115. public void bug48703() throws Exception {
  1116. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48703.xlsx");
  1117. XSSFSheet sheet = wb.getSheetAt(0);
  1118. // Contains two forms, one with a range and one a list
  1119. XSSFRow r1 = sheet.getRow(0);
  1120. XSSFRow r2 = sheet.getRow(1);
  1121. XSSFCell c1 = r1.getCell(1);
  1122. XSSFCell c2 = r2.getCell(1);
  1123. assertEquals(20.0, c1.getNumericCellValue(), 0);
  1124. assertEquals("SUM(Sheet1!C1,Sheet2!C1,Sheet3!C1,Sheet4!C1)", c1.getCellFormula());
  1125. assertEquals(20.0, c2.getNumericCellValue(), 0);
  1126. assertEquals("SUM(Sheet1:Sheet4!C1)", c2.getCellFormula());
  1127. // Try evaluating both
  1128. XSSFFormulaEvaluator eval = new XSSFFormulaEvaluator(wb);
  1129. eval.evaluateFormulaCell(c1);
  1130. eval.evaluateFormulaCell(c2);
  1131. assertEquals(20.0, c1.getNumericCellValue(), 0);
  1132. assertEquals(20.0, c2.getNumericCellValue(), 0);
  1133. }
  1134. /**
  1135. * Bugzilla 51710: problems reading shared formuals from .xlsx
  1136. */
  1137. @Test
  1138. public void bug51710() {
  1139. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("51710.xlsx");
  1140. final String[] columns = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N"};
  1141. final int rowMax = 500; // bug triggers on row index 59
  1142. Sheet sheet = wb.getSheetAt(0);
  1143. // go through all formula cells
  1144. for (int rInd = 2; rInd <= rowMax; rInd++) {
  1145. Row row = sheet.getRow(rInd);
  1146. for (int cInd = 1; cInd <= 12; cInd++) {
  1147. Cell cell = row.getCell(cInd);
  1148. String formula = cell.getCellFormula();
  1149. CellReference ref = new CellReference(cell);
  1150. //simulate correct answer
  1151. String correct = "$A" + (rInd + 1) + "*" + columns[cInd] + "$2";
  1152. assertEquals("Incorrect formula in " + ref.formatAsString(), correct, formula);
  1153. }
  1154. }
  1155. }
  1156. /**
  1157. * Bug 53101:
  1158. */
  1159. @Test
  1160. public void bug5301(){
  1161. Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("53101.xlsx");
  1162. FormulaEvaluator evaluator =
  1163. workbook.getCreationHelper().createFormulaEvaluator();
  1164. // A1: SUM(B1: IZ1)
  1165. double a1Value =
  1166. evaluator.evaluate(workbook.getSheetAt(0).getRow(0).getCell(0)).getNumberValue();
  1167. // Assert
  1168. assertEquals(259.0, a1Value, 0.0);
  1169. // KY: SUM(B1: IZ1)
  1170. /*double ky1Value =*/
  1171. evaluator.evaluate(workbook.getSheetAt(0).getRow(0).getCell(310)).getNumberValue();
  1172. // Assert
  1173. assertEquals(259.0, a1Value, 0.0);
  1174. }
  1175. @Test
  1176. public void bug54436(){
  1177. Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("54436.xlsx");
  1178. if(!WorkbookEvaluator.getSupportedFunctionNames().contains("GETPIVOTDATA")){
  1179. Function func = new Function() {
  1180. public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
  1181. return ErrorEval.NA;
  1182. }
  1183. };
  1184. WorkbookEvaluator.registerFunction("GETPIVOTDATA", func);
  1185. }
  1186. workbook.getCreationHelper().createFormulaEvaluator().evaluateAll();
  1187. }
  1188. /**
  1189. * Password Protected .xlsx files should give a helpful
  1190. * error message when called via WorkbookFactory.
  1191. * (You need to supply a password explicitly for them)
  1192. */
  1193. @Test(expected=EncryptedDocumentException.class)
  1194. public void bug55692_stream() throws Exception {
  1195. // Directly on a Stream
  1196. WorkbookFactory.create(POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"));
  1197. }
  1198. @Test(expected=EncryptedDocumentException.class)
  1199. public void bug55692_poifs() throws Exception {
  1200. // Via a POIFSFileSystem
  1201. POIFSFileSystem fsP = new POIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"));
  1202. WorkbookFactory.create(fsP);
  1203. }
  1204. @Test(expected=EncryptedDocumentException.class)
  1205. public void bug55692_npoifs() throws Exception {
  1206. // Via a NPOIFSFileSystem
  1207. NPOIFSFileSystem fsNP = new NPOIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"));
  1208. WorkbookFactory.create(fsNP);
  1209. }
  1210. @Test
  1211. public void bug53282() {
  1212. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53282b.xlsx");
  1213. Cell c = wb.getSheetAt(0).getRow(1).getCell(0);
  1214. assertEquals("#@_#", c.getStringCellValue());
  1215. assertEquals("http://invalid.uri", c.getHyperlink().getAddress());
  1216. }
  1217. /**
  1218. * Was giving NullPointerException
  1219. * at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead
  1220. * due to a lack of Styles Table
  1221. */
  1222. @Test
  1223. public void bug56278() throws Exception {
  1224. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56278.xlsx");
  1225. assertEquals(0, wb.getSheetIndex("Market Rates"));
  1226. // Save and re-check
  1227. Workbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  1228. assertEquals(0, nwb.getSheetIndex("Market Rates"));
  1229. }
  1230. @Test
  1231. public void bug56315() {
  1232. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56315.xlsx");
  1233. Cell c = wb.getSheetAt(0).getRow(1).getCell(0);
  1234. CellValue cv = wb.getCreationHelper().createFormulaEvaluator().evaluate(c);
  1235. double rounded = cv.getNumberValue();
  1236. assertEquals(0.1, rounded, 0.0);
  1237. }
  1238. }