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

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