Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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