Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TestXSSFBugs.java 98KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636
  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.assertArrayEquals;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertFalse;
  19. import static org.junit.Assert.assertNotNull;
  20. import static org.junit.Assert.assertNull;
  21. import static org.junit.Assert.assertTrue;
  22. import static org.junit.Assert.fail;
  23. import java.io.ByteArrayInputStream;
  24. import java.io.ByteArrayOutputStream;
  25. import java.io.File;
  26. import java.io.FileInputStream;
  27. import java.io.FileOutputStream;
  28. import java.io.IOException;
  29. import java.io.InputStream;
  30. import java.io.OutputStream;
  31. import java.util.Arrays;
  32. import java.util.Calendar;
  33. import java.util.List;
  34. import java.util.Map;
  35. import java.util.Set;
  36. import java.util.TreeMap;
  37. import org.apache.poi.EncryptedDocumentException;
  38. import org.apache.poi.POIDataSamples;
  39. import org.apache.poi.POIXMLDocumentPart;
  40. import org.apache.poi.POIXMLException;
  41. import org.apache.poi.POIXMLProperties;
  42. import org.apache.poi.hssf.HSSFTestDataSamples;
  43. import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
  44. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  45. import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
  46. import org.apache.poi.openxml4j.opc.OPCPackage;
  47. import org.apache.poi.openxml4j.opc.PackageAccess;
  48. import org.apache.poi.openxml4j.opc.PackagePart;
  49. import org.apache.poi.openxml4j.opc.PackagingURIHelper;
  50. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  51. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  52. import org.apache.poi.ss.formula.WorkbookEvaluator;
  53. import org.apache.poi.ss.formula.eval.ErrorEval;
  54. import org.apache.poi.ss.formula.eval.NumberEval;
  55. import org.apache.poi.ss.formula.eval.ValueEval;
  56. import org.apache.poi.ss.formula.functions.Function;
  57. import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
  58. import org.apache.poi.ss.usermodel.Cell;
  59. import org.apache.poi.ss.usermodel.CellStyle;
  60. import org.apache.poi.ss.usermodel.CellValue;
  61. import org.apache.poi.ss.usermodel.ClientAnchor;
  62. import org.apache.poi.ss.usermodel.Comment;
  63. import org.apache.poi.ss.usermodel.CreationHelper;
  64. import org.apache.poi.ss.usermodel.DataFormat;
  65. import org.apache.poi.ss.usermodel.DataFormatter;
  66. import org.apache.poi.ss.usermodel.DateUtil;
  67. import org.apache.poi.ss.usermodel.Drawing;
  68. import org.apache.poi.ss.usermodel.Font;
  69. import org.apache.poi.ss.usermodel.FormulaError;
  70. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  71. import org.apache.poi.ss.usermodel.Hyperlink;
  72. import org.apache.poi.ss.usermodel.IndexedColors;
  73. import org.apache.poi.ss.usermodel.Name;
  74. import org.apache.poi.ss.usermodel.PrintSetup;
  75. import org.apache.poi.ss.usermodel.Row;
  76. import org.apache.poi.ss.usermodel.Sheet;
  77. import org.apache.poi.ss.usermodel.Workbook;
  78. import org.apache.poi.ss.usermodel.WorkbookFactory;
  79. import org.apache.poi.ss.util.AreaReference;
  80. import org.apache.poi.ss.util.CellRangeAddress;
  81. import org.apache.poi.ss.util.CellReference;
  82. import org.apache.poi.util.TempFile;
  83. import org.apache.poi.xssf.XLSBUnsupportedException;
  84. import org.apache.poi.xssf.XSSFITestDataProvider;
  85. import org.apache.poi.xssf.XSSFTestDataSamples;
  86. import org.apache.poi.xssf.model.CalculationChain;
  87. import org.apache.poi.xssf.streaming.SXSSFWorkbook;
  88. import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
  89. import org.junit.Ignore;
  90. import org.junit.Test;
  91. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcCell;
  92. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
  93. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName;
  94. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedNames;
  95. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
  96. import org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTFontImpl;
  97. public final class TestXSSFBugs extends BaseTestBugzillaIssues {
  98. public TestXSSFBugs() {
  99. super(XSSFITestDataProvider.instance);
  100. }
  101. /**
  102. * test writing a file with large number of unique strings,
  103. * open resulting file in Excel to check results!
  104. */
  105. @Test
  106. public void bug15375_2() {
  107. bug15375(1000);
  108. }
  109. /**
  110. * Named ranges had the right reference, but
  111. * the wrong sheet name
  112. */
  113. @Test
  114. public void bug45430() {
  115. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("45430.xlsx");
  116. assertFalse(wb.isMacroEnabled());
  117. assertEquals(3, wb.getNumberOfNames());
  118. assertEquals(0, wb.getNameAt(0).getCTName().getLocalSheetId());
  119. assertFalse(wb.getNameAt(0).getCTName().isSetLocalSheetId());
  120. assertEquals("SheetA!$A$1", wb.getNameAt(0).getRefersToFormula());
  121. assertEquals("SheetA", wb.getNameAt(0).getSheetName());
  122. assertEquals(0, wb.getNameAt(1).getCTName().getLocalSheetId());
  123. assertFalse(wb.getNameAt(1).getCTName().isSetLocalSheetId());
  124. assertEquals("SheetB!$A$1", wb.getNameAt(1).getRefersToFormula());
  125. assertEquals("SheetB", wb.getNameAt(1).getSheetName());
  126. assertEquals(0, wb.getNameAt(2).getCTName().getLocalSheetId());
  127. assertFalse(wb.getNameAt(2).getCTName().isSetLocalSheetId());
  128. assertEquals("SheetC!$A$1", wb.getNameAt(2).getRefersToFormula());
  129. assertEquals("SheetC", wb.getNameAt(2).getSheetName());
  130. // Save and re-load, still there
  131. XSSFWorkbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  132. assertEquals(3, nwb.getNumberOfNames());
  133. assertEquals("SheetA!$A$1", nwb.getNameAt(0).getRefersToFormula());
  134. }
  135. /**
  136. * We should carry vba macros over after save
  137. */
  138. @Test
  139. public void bug45431() throws Exception {
  140. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("45431.xlsm");
  141. OPCPackage pkg = wb.getPackage();
  142. assertTrue(wb.isMacroEnabled());
  143. // Check the various macro related bits can be found
  144. PackagePart vba = pkg.getPart(
  145. PackagingURIHelper.createPartName("/xl/vbaProject.bin")
  146. );
  147. assertNotNull(vba);
  148. // And the drawing bit
  149. PackagePart drw = pkg.getPart(
  150. PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml")
  151. );
  152. assertNotNull(drw);
  153. // Save and re-open, both still there
  154. XSSFWorkbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  155. OPCPackage nPkg = nwb.getPackage();
  156. assertTrue(nwb.isMacroEnabled());
  157. vba = nPkg.getPart(
  158. PackagingURIHelper.createPartName("/xl/vbaProject.bin")
  159. );
  160. assertNotNull(vba);
  161. drw = nPkg.getPart(
  162. PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml")
  163. );
  164. assertNotNull(drw);
  165. // And again, just to be sure
  166. nwb = XSSFTestDataSamples.writeOutAndReadBack(nwb);
  167. nPkg = nwb.getPackage();
  168. assertTrue(nwb.isMacroEnabled());
  169. vba = nPkg.getPart(
  170. PackagingURIHelper.createPartName("/xl/vbaProject.bin")
  171. );
  172. assertNotNull(vba);
  173. drw = nPkg.getPart(
  174. PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml")
  175. );
  176. assertNotNull(drw);
  177. }
  178. @Test
  179. public void bug47504() {
  180. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("47504.xlsx");
  181. assertEquals(1, wb.getNumberOfSheets());
  182. XSSFSheet sh = wb.getSheetAt(0);
  183. XSSFDrawing drawing = sh.createDrawingPatriarch();
  184. List<POIXMLDocumentPart> rels = drawing.getRelations();
  185. assertEquals(1, rels.size());
  186. assertEquals("Sheet1!A1", rels.get(0).getPackageRelationship().getTargetURI().getFragment());
  187. // And again, just to be sure
  188. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  189. assertEquals(1, wb.getNumberOfSheets());
  190. sh = wb.getSheetAt(0);
  191. drawing = sh.createDrawingPatriarch();
  192. rels = drawing.getRelations();
  193. assertEquals(1, rels.size());
  194. assertEquals("Sheet1!A1", rels.get(0).getPackageRelationship().getTargetURI().getFragment());
  195. }
  196. /**
  197. * Excel will sometimes write a button with a textbox
  198. * containing &gt;br&lt; (not closed!).
  199. * Clearly Excel shouldn't do this, but test that we can
  200. * read the file despite the naughtyness
  201. */
  202. @Test
  203. public void bug49020() throws Exception {
  204. /*XSSFWorkbook wb =*/ XSSFTestDataSamples.openSampleWorkbook("BrNotClosed.xlsx");
  205. }
  206. /**
  207. * ensure that CTPhoneticPr is loaded by the ooxml test suite so that it is included in poi-ooxml-schemas
  208. */
  209. @Test
  210. public void bug49325() throws Exception {
  211. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("49325.xlsx");
  212. CTWorksheet sh = wb.getSheetAt(0).getCTWorksheet();
  213. assertNotNull(sh.getPhoneticPr());
  214. }
  215. /**
  216. * Names which are defined with a Sheet
  217. * should return that sheet index properly
  218. */
  219. @Test
  220. public void bug48923() throws Exception {
  221. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48923.xlsx");
  222. assertEquals(4, wb.getNumberOfNames());
  223. Name b1 = wb.getName("NameB1");
  224. Name b2 = wb.getName("NameB2");
  225. Name sheet2 = wb.getName("NameSheet2");
  226. Name test = wb.getName("Test");
  227. assertNotNull(b1);
  228. assertEquals("NameB1", b1.getNameName());
  229. assertEquals("Sheet1", b1.getSheetName());
  230. assertEquals(-1, b1.getSheetIndex());
  231. assertNotNull(b2);
  232. assertEquals("NameB2", b2.getNameName());
  233. assertEquals("Sheet1", b2.getSheetName());
  234. assertEquals(-1, b2.getSheetIndex());
  235. assertNotNull(sheet2);
  236. assertEquals("NameSheet2", sheet2.getNameName());
  237. assertEquals("Sheet2", sheet2.getSheetName());
  238. assertEquals(-1, sheet2.getSheetIndex());
  239. assertNotNull(test);
  240. assertEquals("Test", test.getNameName());
  241. assertEquals("Sheet1", test.getSheetName());
  242. assertEquals(-1, test.getSheetIndex());
  243. }
  244. /**
  245. * Problem with evaluation formulas due to
  246. * NameXPtgs.
  247. * Blows up on:
  248. * IF(B6= (ROUNDUP(B6,0) + ROUNDDOWN(B6,0))/2, MROUND(B6,2),ROUND(B6,0))
  249. *
  250. * TODO: delete this test case when MROUND and VAR are implemented
  251. */
  252. @Test
  253. public void bug48539() throws Exception {
  254. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48539.xlsx");
  255. assertEquals(3, wb.getNumberOfSheets());
  256. assertEquals(0, wb.getNumberOfNames());
  257. // Try each cell individually
  258. XSSFFormulaEvaluator eval = new XSSFFormulaEvaluator(wb);
  259. for(int i=0; i<wb.getNumberOfSheets(); i++) {
  260. Sheet s = wb.getSheetAt(i);
  261. for(Row r : s) {
  262. for(Cell c : r) {
  263. if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
  264. String formula = c.getCellFormula();
  265. CellValue cv;
  266. try {
  267. cv = eval.evaluate(c);
  268. } catch (Exception e) {
  269. throw new RuntimeException("Can't evaluate formula: " + formula, e);
  270. }
  271. if(cv.getCellType() == Cell.CELL_TYPE_NUMERIC) {
  272. // assert that the calculated value agrees with
  273. // the cached formula result calculated by Excel
  274. double cachedFormulaResult = c.getNumericCellValue();
  275. double evaluatedFormulaResult = cv.getNumberValue();
  276. assertEquals(c.getCellFormula(), cachedFormulaResult, evaluatedFormulaResult, 1E-7);
  277. }
  278. }
  279. }
  280. }
  281. }
  282. // Now all of them
  283. XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  284. }
  285. /**
  286. * Foreground colours should be found even if
  287. * a theme is used
  288. */
  289. @Test
  290. public void bug48779() throws Exception {
  291. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48779.xlsx");
  292. XSSFCell cell = wb.getSheetAt(0).getRow(0).getCell(0);
  293. XSSFCellStyle cs = cell.getCellStyle();
  294. assertNotNull(cs);
  295. assertEquals(1, cs.getIndex());
  296. // Look at the low level xml elements
  297. assertEquals(2, cs.getCoreXf().getFillId());
  298. assertEquals(0, cs.getCoreXf().getXfId());
  299. assertEquals(true, cs.getCoreXf().getApplyFill());
  300. XSSFCellFill fg = wb.getStylesSource().getFillAt(2);
  301. assertEquals(0, fg.getFillForegroundColor().getIndexed());
  302. assertEquals(0.0, fg.getFillForegroundColor().getTint(), 0);
  303. assertEquals("FFFF0000", fg.getFillForegroundColor().getARGBHex());
  304. assertEquals(64, fg.getFillBackgroundColor().getIndexed());
  305. // Now look higher up
  306. assertNotNull(cs.getFillForegroundXSSFColor());
  307. assertEquals(0, cs.getFillForegroundColor());
  308. assertEquals("FFFF0000", cs.getFillForegroundXSSFColor().getARGBHex());
  309. assertEquals("FFFF0000", cs.getFillForegroundColorColor().getARGBHex());
  310. assertNotNull(cs.getFillBackgroundColor());
  311. assertEquals(64, cs.getFillBackgroundColor());
  312. assertEquals(null, cs.getFillBackgroundXSSFColor().getARGBHex());
  313. assertEquals(null, cs.getFillBackgroundColorColor().getARGBHex());
  314. }
  315. /**
  316. * Ensure General and @ format are working properly
  317. * for integers
  318. */
  319. @Test
  320. public void bug47490() throws Exception {
  321. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("GeneralFormatTests.xlsx");
  322. Sheet s = wb.getSheetAt(1);
  323. Row r;
  324. DataFormatter df = new DataFormatter();
  325. r = s.getRow(1);
  326. assertEquals(1.0, r.getCell(2).getNumericCellValue(), 0);
  327. assertEquals("General", r.getCell(2).getCellStyle().getDataFormatString());
  328. assertEquals("1", df.formatCellValue(r.getCell(2)));
  329. assertEquals("1", df.formatRawCellContents(1.0, -1, "@"));
  330. assertEquals("1", df.formatRawCellContents(1.0, -1, "General"));
  331. r = s.getRow(2);
  332. assertEquals(12.0, r.getCell(2).getNumericCellValue(), 0);
  333. assertEquals("General", r.getCell(2).getCellStyle().getDataFormatString());
  334. assertEquals("12", df.formatCellValue(r.getCell(2)));
  335. assertEquals("12", df.formatRawCellContents(12.0, -1, "@"));
  336. assertEquals("12", df.formatRawCellContents(12.0, -1, "General"));
  337. r = s.getRow(3);
  338. assertEquals(123.0, r.getCell(2).getNumericCellValue(), 0);
  339. assertEquals("General", r.getCell(2).getCellStyle().getDataFormatString());
  340. assertEquals("123", df.formatCellValue(r.getCell(2)));
  341. assertEquals("123", df.formatRawCellContents(123.0, -1, "@"));
  342. assertEquals("123", df.formatRawCellContents(123.0, -1, "General"));
  343. }
  344. /**
  345. * A problem file from a non-standard source (a scientific instrument that saves its
  346. * output as an .xlsx file) that have two issues:
  347. * 1. The Content Type part name is lower-case: [content_types].xml
  348. * 2. The file appears to use backslashes as path separators
  349. *
  350. * The OPC spec tolerates both of these peculiarities, so does POI
  351. */
  352. @Test
  353. public void bug49609() throws Exception {
  354. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("49609.xlsx");
  355. assertEquals("FAM", wb.getSheetName(0));
  356. assertEquals("Cycle", wb.getSheetAt(0).getRow(0).getCell(1).getStringCellValue());
  357. }
  358. @Test
  359. public void bug49783() throws Exception {
  360. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("49783.xlsx");
  361. Sheet sheet = wb.getSheetAt(0);
  362. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  363. Cell cell;
  364. cell = sheet.getRow(0).getCell(0);
  365. assertEquals("#REF!*#REF!", cell.getCellFormula());
  366. assertEquals(Cell.CELL_TYPE_ERROR, evaluator.evaluateInCell(cell).getCellType());
  367. assertEquals("#REF!", FormulaError.forInt(cell.getErrorCellValue()).getString());
  368. Name nm1 = wb.getName("sale_1");
  369. assertNotNull("name sale_1 should be present", nm1);
  370. assertEquals("Sheet1!#REF!", nm1.getRefersToFormula());
  371. Name nm2 = wb.getName("sale_2");
  372. assertNotNull("name sale_2 should be present", nm2);
  373. assertEquals("Sheet1!#REF!", nm2.getRefersToFormula());
  374. cell = sheet.getRow(1).getCell(0);
  375. assertEquals("sale_1*sale_2", cell.getCellFormula());
  376. assertEquals(Cell.CELL_TYPE_ERROR, evaluator.evaluateInCell(cell).getCellType());
  377. assertEquals("#REF!", FormulaError.forInt(cell.getErrorCellValue()).getString());
  378. }
  379. /**
  380. * Creating a rich string of "hello world" and applying
  381. * a font to characters 1-5 means we have two strings,
  382. * "hello" and " world". As such, we need to apply
  383. * preserve spaces to the 2nd bit, lest we end up
  384. * with something like "helloworld" !
  385. */
  386. @Test
  387. public void bug49941() throws Exception {
  388. XSSFWorkbook wb = new XSSFWorkbook();
  389. XSSFSheet s = wb.createSheet();
  390. XSSFRow r = s.createRow(0);
  391. XSSFCell c = r.createCell(0);
  392. // First without fonts
  393. c.setCellValue(
  394. new XSSFRichTextString(" with spaces ")
  395. );
  396. assertEquals(" with spaces ", c.getRichStringCellValue().toString());
  397. assertEquals(0, c.getRichStringCellValue().getCTRst().sizeOfRArray());
  398. assertEquals(true, c.getRichStringCellValue().getCTRst().isSetT());
  399. // Should have the preserve set
  400. assertEquals(
  401. 1,
  402. c.getRichStringCellValue().getCTRst().xgetT().getDomNode().getAttributes().getLength()
  403. );
  404. assertEquals(
  405. "preserve",
  406. c.getRichStringCellValue().getCTRst().xgetT().getDomNode().getAttributes().item(0).getNodeValue()
  407. );
  408. // Save and check
  409. XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
  410. wb.close();
  411. s = wbBack.getSheetAt(0);
  412. r = s.getRow(0);
  413. c = r.getCell(0);
  414. assertEquals(" with spaces ", c.getRichStringCellValue().toString());
  415. assertEquals(0, c.getRichStringCellValue().getCTRst().sizeOfRArray());
  416. assertEquals(true, c.getRichStringCellValue().getCTRst().isSetT());
  417. // Change the string
  418. c.setCellValue(
  419. new XSSFRichTextString("hello world")
  420. );
  421. assertEquals("hello world", c.getRichStringCellValue().toString());
  422. // Won't have preserve
  423. assertEquals(
  424. 0,
  425. c.getRichStringCellValue().getCTRst().xgetT().getDomNode().getAttributes().getLength()
  426. );
  427. // Apply a font
  428. XSSFFont f = wbBack.createFont();
  429. f.setBold(true);
  430. c.getRichStringCellValue().applyFont(0, 5, f);
  431. assertEquals("hello world", c.getRichStringCellValue().toString());
  432. // Does need preserving on the 2nd part
  433. assertEquals(2, c.getRichStringCellValue().getCTRst().sizeOfRArray());
  434. assertEquals(
  435. 0,
  436. c.getRichStringCellValue().getCTRst().getRArray(0).xgetT().getDomNode().getAttributes().getLength()
  437. );
  438. assertEquals(
  439. 1,
  440. c.getRichStringCellValue().getCTRst().getRArray(1).xgetT().getDomNode().getAttributes().getLength()
  441. );
  442. assertEquals(
  443. "preserve",
  444. c.getRichStringCellValue().getCTRst().getRArray(1).xgetT().getDomNode().getAttributes().item(0).getNodeValue()
  445. );
  446. // Save and check
  447. wbBack = XSSFTestDataSamples.writeOutAndReadBack(wbBack);
  448. s = wbBack.getSheetAt(0);
  449. r = s.getRow(0);
  450. c = r.getCell(0);
  451. assertEquals("hello world", c.getRichStringCellValue().toString());
  452. wbBack.close();
  453. }
  454. /**
  455. * Repeatedly writing the same file which has styles
  456. */
  457. @Test
  458. public void bug49940() throws Exception {
  459. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("styles.xlsx");
  460. assertEquals(3, wb.getNumberOfSheets());
  461. assertEquals(10, wb.getStylesSource().getNumCellStyles());
  462. ByteArrayOutputStream b1 = new ByteArrayOutputStream();
  463. ByteArrayOutputStream b2 = new ByteArrayOutputStream();
  464. ByteArrayOutputStream b3 = new ByteArrayOutputStream();
  465. wb.write(b1);
  466. wb.write(b2);
  467. wb.write(b3);
  468. for(byte[] data : new byte[][] {
  469. b1.toByteArray(), b2.toByteArray(), b3.toByteArray()
  470. }) {
  471. ByteArrayInputStream bais = new ByteArrayInputStream(data);
  472. wb = new XSSFWorkbook(bais);
  473. assertEquals(3, wb.getNumberOfSheets());
  474. assertEquals(10, wb.getStylesSource().getNumCellStyles());
  475. }
  476. }
  477. /**
  478. * Various ways of removing a cell formula should all zap the calcChain
  479. * entry.
  480. */
  481. @Test
  482. public void bug49966() throws Exception {
  483. XSSFWorkbook wb = XSSFTestDataSamples
  484. .openSampleWorkbook("shared_formulas.xlsx");
  485. XSSFSheet sheet = wb.getSheetAt(0);
  486. Workbook wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
  487. // CalcChain has lots of entries
  488. CalculationChain cc = wb.getCalculationChain();
  489. assertEquals("A2", cc.getCTCalcChain().getCArray(0).getR());
  490. assertEquals("A3", cc.getCTCalcChain().getCArray(1).getR());
  491. assertEquals("A4", cc.getCTCalcChain().getCArray(2).getR());
  492. assertEquals("A5", cc.getCTCalcChain().getCArray(3).getR());
  493. assertEquals("A6", cc.getCTCalcChain().getCArray(4).getR());
  494. assertEquals("A7", cc.getCTCalcChain().getCArray(5).getR());
  495. assertEquals("A8", cc.getCTCalcChain().getCArray(6).getR());
  496. assertEquals(40, cc.getCTCalcChain().sizeOfCArray());
  497. wbRead.close();
  498. wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
  499. // Try various ways of changing the formulas
  500. // If it stays a formula, chain entry should remain
  501. // Otherwise should go
  502. sheet.getRow(1).getCell(0).setCellFormula("A1"); // stay
  503. sheet.getRow(2).getCell(0).setCellFormula(null); // go
  504. sheet.getRow(3).getCell(0).setCellType(Cell.CELL_TYPE_FORMULA); // stay
  505. wbRead.close();
  506. wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
  507. sheet.getRow(4).getCell(0).setCellType(Cell.CELL_TYPE_STRING); // go
  508. wbRead.close();
  509. wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
  510. validateCells(sheet);
  511. sheet.getRow(5).removeCell(sheet.getRow(5).getCell(0)); // go
  512. validateCells(sheet);
  513. wbRead.close();
  514. wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
  515. sheet.getRow(6).getCell(0).setCellType(Cell.CELL_TYPE_BLANK); // go
  516. wbRead.close();
  517. wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
  518. sheet.getRow(7).getCell(0).setCellValue((String) null); // go
  519. wbRead.close();
  520. wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
  521. // Save and check
  522. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  523. assertEquals(35, cc.getCTCalcChain().sizeOfCArray());
  524. cc = wb.getCalculationChain();
  525. assertEquals("A2", cc.getCTCalcChain().getCArray(0).getR());
  526. assertEquals("A4", cc.getCTCalcChain().getCArray(1).getR());
  527. assertEquals("A9", cc.getCTCalcChain().getCArray(2).getR());
  528. wbRead.close();
  529. }
  530. @Test
  531. public void bug49966Row() throws Exception {
  532. XSSFWorkbook wb = XSSFTestDataSamples
  533. .openSampleWorkbook("shared_formulas.xlsx");
  534. XSSFSheet sheet = wb.getSheetAt(0);
  535. validateCells(sheet);
  536. sheet.getRow(5).removeCell(sheet.getRow(5).getCell(0)); // go
  537. validateCells(sheet);
  538. }
  539. private void validateCells(XSSFSheet sheet) {
  540. for(Row row : sheet) {
  541. // trigger handling
  542. ((XSSFRow)row).onDocumentWrite();
  543. }
  544. }
  545. @Test
  546. public void bug49156() throws Exception {
  547. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("49156.xlsx");
  548. FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  549. Sheet sheet = wb.getSheetAt(0);
  550. for(Row row : sheet){
  551. for(Cell cell : row){
  552. if(cell.getCellType() == Cell.CELL_TYPE_FORMULA){
  553. formulaEvaluator.evaluateInCell(cell); // caused NPE on some cells
  554. }
  555. }
  556. }
  557. }
  558. /**
  559. * Newlines are valid characters in a formula
  560. */
  561. @Test
  562. public void bug50440And51875() throws Exception {
  563. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("NewlineInFormulas.xlsx");
  564. Sheet s = wb.getSheetAt(0);
  565. Cell c = s.getRow(0).getCell(0);
  566. assertEquals("SUM(\n1,2\n)", c.getCellFormula());
  567. assertEquals(3.0, c.getNumericCellValue(), 0);
  568. FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  569. formulaEvaluator.evaluateFormulaCell(c);
  570. assertEquals("SUM(\n1,2\n)", c.getCellFormula());
  571. assertEquals(3.0, c.getNumericCellValue(), 0);
  572. // For 51875
  573. Cell b3 = s.getRow(2).getCell(1);
  574. formulaEvaluator.evaluateFormulaCell(b3);
  575. assertEquals("B1+B2", b3.getCellFormula()); // The newline is lost for shared formulas
  576. assertEquals(3.0, b3.getNumericCellValue(), 0);
  577. }
  578. /**
  579. * Moving a cell comment from one cell to another
  580. */
  581. @Test
  582. public void bug50795() throws Exception {
  583. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50795.xlsx");
  584. XSSFSheet sheet = wb.getSheetAt(0);
  585. XSSFRow row = sheet.getRow(0);
  586. XSSFCell cellWith = row.getCell(0);
  587. XSSFCell cellWithoutComment = row.getCell(1);
  588. assertNotNull(cellWith.getCellComment());
  589. assertNull(cellWithoutComment.getCellComment());
  590. String exp = "\u0410\u0432\u0442\u043e\u0440:\ncomment";
  591. XSSFComment comment = cellWith.getCellComment();
  592. assertEquals(exp, comment.getString().getString());
  593. // Check we can write it out and read it back as-is
  594. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  595. sheet = wb.getSheetAt(0);
  596. row = sheet.getRow(0);
  597. cellWith = row.getCell(0);
  598. cellWithoutComment = row.getCell(1);
  599. // Double check things are as expected
  600. assertNotNull(cellWith.getCellComment());
  601. assertNull(cellWithoutComment.getCellComment());
  602. comment = cellWith.getCellComment();
  603. assertEquals(exp, comment.getString().getString());
  604. // Move the comment
  605. cellWithoutComment.setCellComment(comment);
  606. // Write out and re-check
  607. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  608. sheet = wb.getSheetAt(0);
  609. row = sheet.getRow(0);
  610. // Ensure it swapped over
  611. cellWith = row.getCell(0);
  612. cellWithoutComment = row.getCell(1);
  613. assertNull(cellWith.getCellComment());
  614. assertNotNull(cellWithoutComment.getCellComment());
  615. comment = cellWithoutComment.getCellComment();
  616. assertEquals(exp, comment.getString().getString());
  617. }
  618. /**
  619. * When the cell background colour is set with one of the first
  620. * two columns of the theme colour palette, the colours are
  621. * shades of white or black.
  622. * For those cases, ensure we don't break on reading the colour
  623. */
  624. @Test
  625. public void bug50299() throws Exception {
  626. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("50299.xlsx");
  627. // Check all the colours
  628. for(int sn=0; sn<wb.getNumberOfSheets(); sn++) {
  629. Sheet s = wb.getSheetAt(sn);
  630. for(Row r : s) {
  631. for(Cell c : r) {
  632. CellStyle cs = c.getCellStyle();
  633. if(cs != null) {
  634. cs.getFillForegroundColor();
  635. }
  636. }
  637. }
  638. }
  639. // Check one bit in detail
  640. // Check that we get back foreground=0 for the theme colours,
  641. // and background=64 for the auto colouring
  642. Sheet s = wb.getSheetAt(0);
  643. assertEquals(0, s.getRow(0).getCell(8).getCellStyle().getFillForegroundColor());
  644. assertEquals(64, s.getRow(0).getCell(8).getCellStyle().getFillBackgroundColor());
  645. assertEquals(0, s.getRow(1).getCell(8).getCellStyle().getFillForegroundColor());
  646. assertEquals(64, s.getRow(1).getCell(8).getCellStyle().getFillBackgroundColor());
  647. }
  648. /**
  649. * Excel .xls style indexed colours in a .xlsx file
  650. */
  651. @SuppressWarnings("deprecation")
  652. @Test
  653. public void bug50786() throws Exception {
  654. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50786-indexed_colours.xlsx");
  655. XSSFSheet s = wb.getSheetAt(0);
  656. XSSFRow r = s.getRow(2);
  657. // Check we have the right cell
  658. XSSFCell c = r.getCell(1);
  659. assertEquals("test\u00a0", c.getRichStringCellValue().getString());
  660. // It should be light green
  661. XSSFCellStyle cs = c.getCellStyle();
  662. assertEquals(42, cs.getFillForegroundColor());
  663. assertEquals(42, cs.getFillForegroundColorColor().getIndexed());
  664. assertNotNull(cs.getFillForegroundColorColor().getRgb());
  665. assertEquals("FFCCFFCC", cs.getFillForegroundColorColor().getARGBHex());
  666. }
  667. /**
  668. * If the border colours are set with themes, then we
  669. * should still be able to get colours
  670. */
  671. @Test
  672. public void bug50846() throws Exception {
  673. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50846-border_colours.xlsx");
  674. XSSFSheet sheet = wb.getSheetAt(0);
  675. XSSFRow row = sheet.getRow(0);
  676. // Border from a theme, brown
  677. XSSFCell cellT = row.getCell(0);
  678. XSSFCellStyle styleT = cellT.getCellStyle();
  679. XSSFColor colorT = styleT.getBottomBorderXSSFColor();
  680. assertEquals(5, colorT.getTheme());
  681. assertEquals("FFC0504D", colorT.getARGBHex());
  682. // Border from a style direct, red
  683. XSSFCell cellS = row.getCell(1);
  684. XSSFCellStyle styleS = cellS.getCellStyle();
  685. XSSFColor colorS = styleS.getBottomBorderXSSFColor();
  686. assertEquals(0, colorS.getTheme());
  687. assertEquals("FFFF0000", colorS.getARGBHex());
  688. }
  689. /**
  690. * Fonts where their colours come from the theme rather
  691. * then being set explicitly still should allow the
  692. * fetching of the RGB.
  693. */
  694. @SuppressWarnings("deprecation")
  695. @Test
  696. public void bug50784() throws Exception {
  697. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50784-font_theme_colours.xlsx");
  698. XSSFSheet s = wb.getSheetAt(0);
  699. XSSFRow r = s.getRow(0);
  700. // Column 1 has a font with regular colours
  701. XSSFCell cr = r.getCell(1);
  702. XSSFFont fr = wb.getFontAt( cr.getCellStyle().getFontIndex() );
  703. XSSFColor colr = fr.getXSSFColor();
  704. // No theme, has colours
  705. assertEquals(0, colr.getTheme());
  706. assertNotNull( colr.getRgb() );
  707. // Column 0 has a font with colours from a theme
  708. XSSFCell ct = r.getCell(0);
  709. XSSFFont ft = wb.getFontAt( ct.getCellStyle().getFontIndex() );
  710. XSSFColor colt = ft.getXSSFColor();
  711. // Has a theme, which has the colours on it
  712. assertEquals(9, colt.getTheme());
  713. XSSFColor themeC = wb.getTheme().getThemeColor(colt.getTheme());
  714. assertNotNull( themeC.getRgb() );
  715. assertNotNull( colt.getRgb() );
  716. assertEquals( themeC.getARGBHex(), colt.getARGBHex() ); // The same colour
  717. }
  718. /**
  719. * New lines were being eaten when setting a font on
  720. * a rich text string
  721. */
  722. @Test
  723. public void bug48877() throws Exception {
  724. String text = "Use \n with word wrap on to create a new line.\n" +
  725. "This line finishes with two trailing spaces. ";
  726. XSSFWorkbook wb = new XSSFWorkbook();
  727. XSSFSheet sheet = wb.createSheet();
  728. Font font1 = wb.createFont();
  729. font1.setColor((short) 20);
  730. Font font2 = wb.createFont();
  731. font2.setColor(Font.COLOR_RED);
  732. Font font3 = wb.getFontAt((short)0);
  733. XSSFRow row = sheet.createRow(2);
  734. XSSFCell cell = row.createCell(2);
  735. XSSFRichTextString richTextString =
  736. wb.getCreationHelper().createRichTextString(text);
  737. // Check the text has the newline
  738. assertEquals(text, richTextString.getString());
  739. // Apply the font
  740. richTextString.applyFont(font3);
  741. richTextString.applyFont(0, 3, font1);
  742. cell.setCellValue(richTextString);
  743. // To enable newlines you need set a cell styles with wrap=true
  744. CellStyle cs = wb.createCellStyle();
  745. cs.setWrapText(true);
  746. cell.setCellStyle(cs);
  747. // Check the text has the
  748. assertEquals(text, cell.getStringCellValue());
  749. // Save the file and re-read it
  750. XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
  751. wb.close();
  752. sheet = wbBack.getSheetAt(0);
  753. row = sheet.getRow(2);
  754. cell = row.getCell(2);
  755. assertEquals(text, cell.getStringCellValue());
  756. // Now add a 2nd, and check again
  757. int fontAt = text.indexOf("\n", 6);
  758. cell.getRichStringCellValue().applyFont(10, fontAt+1, font2);
  759. assertEquals(text, cell.getStringCellValue());
  760. assertEquals(4, cell.getRichStringCellValue().numFormattingRuns());
  761. assertEquals("Use", cell.getRichStringCellValue().getCTRst().getRArray(0).getT());
  762. String r3 = cell.getRichStringCellValue().getCTRst().getRArray(2).getT();
  763. assertEquals("line.\n", r3.substring(r3.length()-6));
  764. // Save and re-check
  765. wbBack = XSSFTestDataSamples.writeOutAndReadBack(wbBack);
  766. sheet = wbBack.getSheetAt(0);
  767. row = sheet.getRow(2);
  768. cell = row.getCell(2);
  769. assertEquals(text, cell.getStringCellValue());
  770. wbBack.close();
  771. // FileOutputStream out = new FileOutputStream("/tmp/test48877.xlsx");
  772. // wb.write(out);
  773. // out.close();
  774. }
  775. /**
  776. * Adding sheets when one has a table, then re-ordering
  777. */
  778. @Test
  779. public void bug50867() throws Exception {
  780. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50867_with_table.xlsx");
  781. assertEquals(3, wb.getNumberOfSheets());
  782. XSSFSheet s1 = wb.getSheetAt(0);
  783. XSSFSheet s2 = wb.getSheetAt(1);
  784. XSSFSheet s3 = wb.getSheetAt(2);
  785. assertEquals(1, s1.getTables().size());
  786. assertEquals(0, s2.getTables().size());
  787. assertEquals(0, s3.getTables().size());
  788. XSSFTable t = s1.getTables().get(0);
  789. assertEquals("Tabella1", t.getName());
  790. assertEquals("Tabella1", t.getDisplayName());
  791. assertEquals("A1:C3", t.getCTTable().getRef());
  792. // Add a sheet and re-order
  793. XSSFSheet s4 = wb.createSheet("NewSheet");
  794. wb.setSheetOrder(s4.getSheetName(), 0);
  795. // Check on tables
  796. assertEquals(1, s1.getTables().size());
  797. assertEquals(0, s2.getTables().size());
  798. assertEquals(0, s3.getTables().size());
  799. assertEquals(0, s4.getTables().size());
  800. // Refetch to get the new order
  801. s1 = wb.getSheetAt(0);
  802. s2 = wb.getSheetAt(1);
  803. s3 = wb.getSheetAt(2);
  804. s4 = wb.getSheetAt(3);
  805. assertEquals(0, s1.getTables().size());
  806. assertEquals(1, s2.getTables().size());
  807. assertEquals(0, s3.getTables().size());
  808. assertEquals(0, s4.getTables().size());
  809. // Save and re-load
  810. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  811. s1 = wb.getSheetAt(0);
  812. s2 = wb.getSheetAt(1);
  813. s3 = wb.getSheetAt(2);
  814. s4 = wb.getSheetAt(3);
  815. assertEquals(0, s1.getTables().size());
  816. assertEquals(1, s2.getTables().size());
  817. assertEquals(0, s3.getTables().size());
  818. assertEquals(0, s4.getTables().size());
  819. t = s2.getTables().get(0);
  820. assertEquals("Tabella1", t.getName());
  821. assertEquals("Tabella1", t.getDisplayName());
  822. assertEquals("A1:C3", t.getCTTable().getRef());
  823. // Add some more tables, and check
  824. t = s2.createTable();
  825. t.setName("New 2");
  826. t.setDisplayName("New 2");
  827. t = s3.createTable();
  828. t.setName("New 3");
  829. t.setDisplayName("New 3");
  830. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  831. s1 = wb.getSheetAt(0);
  832. s2 = wb.getSheetAt(1);
  833. s3 = wb.getSheetAt(2);
  834. s4 = wb.getSheetAt(3);
  835. assertEquals(0, s1.getTables().size());
  836. assertEquals(2, s2.getTables().size());
  837. assertEquals(1, s3.getTables().size());
  838. assertEquals(0, s4.getTables().size());
  839. t = s2.getTables().get(0);
  840. assertEquals("Tabella1", t.getName());
  841. assertEquals("Tabella1", t.getDisplayName());
  842. assertEquals("A1:C3", t.getCTTable().getRef());
  843. t = s2.getTables().get(1);
  844. assertEquals("New 2", t.getName());
  845. assertEquals("New 2", t.getDisplayName());
  846. t = s3.getTables().get(0);
  847. assertEquals("New 3", t.getName());
  848. assertEquals("New 3", t.getDisplayName());
  849. // Check the relationships
  850. assertEquals(0, s1.getRelations().size());
  851. assertEquals(3, s2.getRelations().size());
  852. assertEquals(1, s3.getRelations().size());
  853. assertEquals(0, s4.getRelations().size());
  854. assertEquals(
  855. XSSFRelation.PRINTER_SETTINGS.getContentType(),
  856. s2.getRelations().get(0).getPackagePart().getContentType()
  857. );
  858. assertEquals(
  859. XSSFRelation.TABLE.getContentType(),
  860. s2.getRelations().get(1).getPackagePart().getContentType()
  861. );
  862. assertEquals(
  863. XSSFRelation.TABLE.getContentType(),
  864. s2.getRelations().get(2).getPackagePart().getContentType()
  865. );
  866. assertEquals(
  867. XSSFRelation.TABLE.getContentType(),
  868. s3.getRelations().get(0).getPackagePart().getContentType()
  869. );
  870. assertEquals(
  871. "/xl/tables/table3.xml",
  872. s3.getRelations().get(0).getPackagePart().getPartName().toString()
  873. );
  874. }
  875. /**
  876. * Setting repeating rows and columns shouldn't break
  877. * any print settings that were there before
  878. */
  879. @SuppressWarnings("deprecation")
  880. @Test
  881. public void bug49253() throws Exception {
  882. XSSFWorkbook wb1 = new XSSFWorkbook();
  883. XSSFWorkbook wb2 = new XSSFWorkbook();
  884. // No print settings before repeating
  885. XSSFSheet s1 = wb1.createSheet();
  886. assertEquals(false, s1.getCTWorksheet().isSetPageSetup());
  887. assertEquals(true, s1.getCTWorksheet().isSetPageMargins());
  888. wb1.setRepeatingRowsAndColumns(0, 2, 3, 1, 2);
  889. assertEquals(true, s1.getCTWorksheet().isSetPageSetup());
  890. assertEquals(true, s1.getCTWorksheet().isSetPageMargins());
  891. PrintSetup ps1 = s1.getPrintSetup();
  892. assertEquals(false, ps1.getValidSettings());
  893. assertEquals(false, ps1.getLandscape());
  894. // Had valid print settings before repeating
  895. XSSFSheet s2 = wb2.createSheet();
  896. PrintSetup ps2 = s2.getPrintSetup();
  897. assertEquals(true, s2.getCTWorksheet().isSetPageSetup());
  898. assertEquals(true, s2.getCTWorksheet().isSetPageMargins());
  899. ps2.setLandscape(false);
  900. assertEquals(true, ps2.getValidSettings());
  901. assertEquals(false, ps2.getLandscape());
  902. wb2.setRepeatingRowsAndColumns(0, 2, 3, 1, 2);
  903. ps2 = s2.getPrintSetup();
  904. assertEquals(true, s2.getCTWorksheet().isSetPageSetup());
  905. assertEquals(true, s2.getCTWorksheet().isSetPageMargins());
  906. assertEquals(true, ps2.getValidSettings());
  907. assertEquals(false, ps2.getLandscape());
  908. wb1.close();
  909. wb2.close();
  910. }
  911. /**
  912. * Default Column style
  913. */
  914. @Test
  915. public void bug51037() throws Exception {
  916. XSSFWorkbook wb = new XSSFWorkbook();
  917. XSSFSheet s = wb.createSheet();
  918. CellStyle defaultStyle = wb.getCellStyleAt((short)0);
  919. assertEquals(0, defaultStyle.getIndex());
  920. CellStyle blueStyle = wb.createCellStyle();
  921. blueStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());
  922. blueStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  923. assertEquals(1, blueStyle.getIndex());
  924. CellStyle pinkStyle = wb.createCellStyle();
  925. pinkStyle.setFillForegroundColor(IndexedColors.PINK.getIndex());
  926. pinkStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  927. assertEquals(2, pinkStyle.getIndex());
  928. // Starts empty
  929. assertEquals(1, s.getCTWorksheet().sizeOfColsArray());
  930. CTCols cols = s.getCTWorksheet().getColsArray(0);
  931. assertEquals(0, cols.sizeOfColArray());
  932. // Add some rows and columns
  933. XSSFRow r1 = s.createRow(0);
  934. XSSFRow r2 = s.createRow(1);
  935. r1.createCell(0);
  936. r1.createCell(2);
  937. r2.createCell(0);
  938. r2.createCell(3);
  939. // Check no style is there
  940. assertEquals(1, s.getCTWorksheet().sizeOfColsArray());
  941. assertEquals(0, cols.sizeOfColArray());
  942. assertEquals(defaultStyle, s.getColumnStyle(0));
  943. assertEquals(defaultStyle, s.getColumnStyle(2));
  944. assertEquals(defaultStyle, s.getColumnStyle(3));
  945. // Apply the styles
  946. s.setDefaultColumnStyle(0, pinkStyle);
  947. s.setDefaultColumnStyle(3, blueStyle);
  948. // Check
  949. assertEquals(pinkStyle, s.getColumnStyle(0));
  950. assertEquals(defaultStyle, s.getColumnStyle(2));
  951. assertEquals(blueStyle, s.getColumnStyle(3));
  952. assertEquals(1, s.getCTWorksheet().sizeOfColsArray());
  953. assertEquals(2, cols.sizeOfColArray());
  954. assertEquals(1, cols.getColArray(0).getMin());
  955. assertEquals(1, cols.getColArray(0).getMax());
  956. assertEquals(pinkStyle.getIndex(), cols.getColArray(0).getStyle());
  957. assertEquals(4, cols.getColArray(1).getMin());
  958. assertEquals(4, cols.getColArray(1).getMax());
  959. assertEquals(blueStyle.getIndex(), cols.getColArray(1).getStyle());
  960. // Save, re-load and re-check
  961. XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
  962. wb.close();
  963. s = wbBack.getSheetAt(0);
  964. defaultStyle = wbBack.getCellStyleAt(defaultStyle.getIndex());
  965. blueStyle = wbBack.getCellStyleAt(blueStyle.getIndex());
  966. pinkStyle = wbBack.getCellStyleAt(pinkStyle.getIndex());
  967. assertEquals(pinkStyle, s.getColumnStyle(0));
  968. assertEquals(defaultStyle, s.getColumnStyle(2));
  969. assertEquals(blueStyle, s.getColumnStyle(3));
  970. wbBack.close();
  971. }
  972. /**
  973. * Repeatedly writing a file.
  974. * Something with the SharedStringsTable currently breaks...
  975. */
  976. @Test
  977. public void bug46662() throws Exception {
  978. // New file
  979. XSSFWorkbook wb = new XSSFWorkbook();
  980. XSSFTestDataSamples.writeOutAndReadBack(wb);
  981. XSSFTestDataSamples.writeOutAndReadBack(wb);
  982. XSSFTestDataSamples.writeOutAndReadBack(wb);
  983. // Simple file
  984. wb = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx");
  985. XSSFTestDataSamples.writeOutAndReadBack(wb);
  986. XSSFTestDataSamples.writeOutAndReadBack(wb);
  987. XSSFTestDataSamples.writeOutAndReadBack(wb);
  988. // Complex file
  989. // TODO
  990. }
  991. /**
  992. * Colours and styles when the list has gaps in it
  993. */
  994. @Test
  995. public void bug51222() throws Exception {
  996. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51222.xlsx");
  997. XSSFSheet s = wb.getSheetAt(0);
  998. XSSFCell cA4_EEECE1 = s.getRow(3).getCell(0);
  999. XSSFCell cA5_1F497D = s.getRow(4).getCell(0);
  1000. // Check the text
  1001. assertEquals("A4", cA4_EEECE1.getRichStringCellValue().getString());
  1002. assertEquals("A5", cA5_1F497D.getRichStringCellValue().getString());
  1003. // Check the styles assigned to them
  1004. assertEquals(4, cA4_EEECE1.getCTCell().getS());
  1005. assertEquals(5, cA5_1F497D.getCTCell().getS());
  1006. // Check we look up the correct style
  1007. assertEquals(4, cA4_EEECE1.getCellStyle().getIndex());
  1008. assertEquals(5, cA5_1F497D.getCellStyle().getIndex());
  1009. // Check the fills on them at the low level
  1010. assertEquals(5, cA4_EEECE1.getCellStyle().getCoreXf().getFillId());
  1011. assertEquals(6, cA5_1F497D.getCellStyle().getCoreXf().getFillId());
  1012. // These should reference themes 2 and 3
  1013. assertEquals(2, wb.getStylesSource().getFillAt(5).getCTFill().getPatternFill().getFgColor().getTheme());
  1014. assertEquals(3, wb.getStylesSource().getFillAt(6).getCTFill().getPatternFill().getFgColor().getTheme());
  1015. // Ensure we get the right colours for these themes
  1016. // TODO fix
  1017. // assertEquals("FFEEECE1", wb.getTheme().getThemeColor(2).getARGBHex());
  1018. // assertEquals("FF1F497D", wb.getTheme().getThemeColor(3).getARGBHex());
  1019. // Finally check the colours on the styles
  1020. // TODO fix
  1021. // assertEquals("FFEEECE1", cA4_EEECE1.getCellStyle().getFillForegroundXSSFColor().getARGBHex());
  1022. // assertEquals("FF1F497D", cA5_1F497D.getCellStyle().getFillForegroundXSSFColor().getARGBHex());
  1023. }
  1024. @Test
  1025. public void bug51470() throws Exception {
  1026. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51470.xlsx");
  1027. XSSFSheet sh0 = wb.getSheetAt(0);
  1028. XSSFSheet sh1 = wb.cloneSheet(0);
  1029. List<POIXMLDocumentPart> rels0 = sh0.getRelations();
  1030. List<POIXMLDocumentPart> rels1 = sh1.getRelations();
  1031. assertEquals(1, rels0.size());
  1032. assertEquals(1, rels1.size());
  1033. assertEquals(rels0.get(0).getPackageRelationship(), rels1.get(0).getPackageRelationship());
  1034. }
  1035. /**
  1036. * Add comments to Sheet 1, when Sheet 2 already has
  1037. * comments (so /xl/comments1.xml is taken)
  1038. */
  1039. @Test
  1040. public void bug51850() {
  1041. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51850.xlsx");
  1042. XSSFSheet sh1 = wb.getSheetAt(0);
  1043. XSSFSheet sh2 = wb.getSheetAt(1);
  1044. // Sheet 2 has comments
  1045. assertNotNull(sh2.getCommentsTable(false));
  1046. assertEquals(1, sh2.getCommentsTable(false).getNumberOfComments());
  1047. // Sheet 1 doesn't (yet)
  1048. assertNull(sh1.getCommentsTable(false));
  1049. // Try to add comments to Sheet 1
  1050. CreationHelper factory = wb.getCreationHelper();
  1051. Drawing drawing = sh1.createDrawingPatriarch();
  1052. ClientAnchor anchor = factory.createClientAnchor();
  1053. anchor.setCol1(0);
  1054. anchor.setCol2(4);
  1055. anchor.setRow1(0);
  1056. anchor.setRow2(1);
  1057. Comment comment1 = drawing.createCellComment(anchor);
  1058. comment1.setString(
  1059. factory.createRichTextString("I like this cell. It's my favourite."));
  1060. comment1.setAuthor("Bob T. Fish");
  1061. anchor = factory.createClientAnchor();
  1062. anchor.setCol1(0);
  1063. anchor.setCol2(4);
  1064. anchor.setRow1(1);
  1065. anchor.setRow2(1);
  1066. Comment comment2 = drawing.createCellComment(anchor);
  1067. comment2.setString(
  1068. factory.createRichTextString("This is much less fun..."));
  1069. comment2.setAuthor("Bob T. Fish");
  1070. Cell c1 = sh1.getRow(0).createCell(4);
  1071. c1.setCellValue(2.3);
  1072. c1.setCellComment(comment1);
  1073. Cell c2 = sh1.getRow(0).createCell(5);
  1074. c2.setCellValue(2.1);
  1075. c2.setCellComment(comment2);
  1076. // Save and re-load
  1077. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  1078. sh1 = wb.getSheetAt(0);
  1079. sh2 = wb.getSheetAt(1);
  1080. // Check the comments
  1081. assertNotNull(sh2.getCommentsTable(false));
  1082. assertEquals(1, sh2.getCommentsTable(false).getNumberOfComments());
  1083. assertNotNull(sh1.getCommentsTable(false));
  1084. assertEquals(2, sh1.getCommentsTable(false).getNumberOfComments());
  1085. }
  1086. /**
  1087. * Sheet names with a , in them
  1088. */
  1089. @Test
  1090. public void bug51963() throws Exception {
  1091. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51963.xlsx");
  1092. Sheet sheet = wb.getSheetAt(0);
  1093. assertEquals("Abc,1", sheet.getSheetName());
  1094. Name name = wb.getName("Intekon.ProdCodes");
  1095. assertEquals("'Abc,1'!$A$1:$A$2", name.getRefersToFormula());
  1096. @SuppressWarnings("deprecation")
  1097. AreaReference ref = new AreaReference(name.getRefersToFormula());
  1098. assertEquals(0, ref.getFirstCell().getRow());
  1099. assertEquals(0, ref.getFirstCell().getCol());
  1100. assertEquals(1, ref.getLastCell().getRow());
  1101. assertEquals(0, ref.getLastCell().getCol());
  1102. }
  1103. /**
  1104. * Sum across multiple workbooks
  1105. * eg =SUM($Sheet1.C1:$Sheet4.C1)
  1106. * DISABLED As we can't currently evaluate these
  1107. */
  1108. @Ignore
  1109. public void bug48703() throws Exception {
  1110. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48703.xlsx");
  1111. XSSFSheet sheet = wb.getSheetAt(0);
  1112. // Contains two forms, one with a range and one a list
  1113. XSSFRow r1 = sheet.getRow(0);
  1114. XSSFRow r2 = sheet.getRow(1);
  1115. XSSFCell c1 = r1.getCell(1);
  1116. XSSFCell c2 = r2.getCell(1);
  1117. assertEquals(20.0, c1.getNumericCellValue(), 0);
  1118. assertEquals("SUM(Sheet1!C1,Sheet2!C1,Sheet3!C1,Sheet4!C1)", c1.getCellFormula());
  1119. assertEquals(20.0, c2.getNumericCellValue(), 0);
  1120. assertEquals("SUM(Sheet1:Sheet4!C1)", c2.getCellFormula());
  1121. // Try evaluating both
  1122. XSSFFormulaEvaluator eval = new XSSFFormulaEvaluator(wb);
  1123. eval.evaluateFormulaCell(c1);
  1124. eval.evaluateFormulaCell(c2);
  1125. assertEquals(20.0, c1.getNumericCellValue(), 0);
  1126. assertEquals(20.0, c2.getNumericCellValue(), 0);
  1127. }
  1128. /**
  1129. * Bugzilla 51710: problems reading shared formuals from .xlsx
  1130. */
  1131. @Test
  1132. public void bug51710() {
  1133. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("51710.xlsx");
  1134. final String[] columns = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N"};
  1135. final int rowMax = 500; // bug triggers on row index 59
  1136. Sheet sheet = wb.getSheetAt(0);
  1137. // go through all formula cells
  1138. for (int rInd = 2; rInd <= rowMax; rInd++) {
  1139. Row row = sheet.getRow(rInd);
  1140. for (int cInd = 1; cInd <= 12; cInd++) {
  1141. Cell cell = row.getCell(cInd);
  1142. String formula = cell.getCellFormula();
  1143. CellReference ref = new CellReference(cell);
  1144. //simulate correct answer
  1145. String correct = "$A" + (rInd + 1) + "*" + columns[cInd] + "$2";
  1146. assertEquals("Incorrect formula in " + ref.formatAsString(), correct, formula);
  1147. }
  1148. }
  1149. }
  1150. /**
  1151. * Bug 53101:
  1152. */
  1153. @Test
  1154. public void bug5301(){
  1155. Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("53101.xlsx");
  1156. FormulaEvaluator evaluator =
  1157. workbook.getCreationHelper().createFormulaEvaluator();
  1158. // A1: SUM(B1: IZ1)
  1159. double a1Value =
  1160. evaluator.evaluate(workbook.getSheetAt(0).getRow(0).getCell(0)).getNumberValue();
  1161. // Assert
  1162. assertEquals(259.0, a1Value, 0.0);
  1163. // KY: SUM(B1: IZ1)
  1164. /*double ky1Value =*/
  1165. evaluator.evaluate(workbook.getSheetAt(0).getRow(0).getCell(310)).getNumberValue();
  1166. // Assert
  1167. assertEquals(259.0, a1Value, 0.0);
  1168. }
  1169. @Test
  1170. public void bug54436(){
  1171. Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("54436.xlsx");
  1172. if(!WorkbookEvaluator.getSupportedFunctionNames().contains("GETPIVOTDATA")){
  1173. Function func = new Function() {
  1174. @Override
  1175. public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
  1176. return ErrorEval.NA;
  1177. }
  1178. };
  1179. WorkbookEvaluator.registerFunction("GETPIVOTDATA", func);
  1180. }
  1181. workbook.getCreationHelper().createFormulaEvaluator().evaluateAll();
  1182. }
  1183. /**
  1184. * Password Protected .xlsx files should give a helpful
  1185. * error message when called via WorkbookFactory with no password
  1186. */
  1187. @Test(expected=EncryptedDocumentException.class)
  1188. public void bug55692_poifs() throws Exception {
  1189. // Via a POIFSFileSystem
  1190. POIFSFileSystem fsP = new POIFSFileSystem(
  1191. POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"));
  1192. WorkbookFactory.create(fsP);
  1193. }
  1194. public void bug55692_stream() throws Exception {
  1195. // Directly on a Stream, will go via NPOIFS and spot it's
  1196. // actually a .xlsx file encrypted with the default password, and open
  1197. Workbook wb = WorkbookFactory.create(
  1198. POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"));
  1199. assertNotNull(wb);
  1200. assertEquals(3, wb.getNumberOfSheets());
  1201. }
  1202. public void bug55692_npoifs() throws Exception {
  1203. // Via a NPOIFSFileSystem, will spot it's actually a .xlsx file
  1204. // encrypted with the default password, and open
  1205. NPOIFSFileSystem fsNP = new NPOIFSFileSystem(
  1206. POIDataSamples.getPOIFSInstance().openResourceAsStream("protect.xlsx"));
  1207. Workbook wb = WorkbookFactory.create(fsNP);
  1208. assertNotNull(wb);
  1209. assertEquals(3, wb.getNumberOfSheets());
  1210. }
  1211. @Test
  1212. public void bug53282() {
  1213. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53282b.xlsx");
  1214. Cell c = wb.getSheetAt(0).getRow(1).getCell(0);
  1215. assertEquals("#@_#", c.getStringCellValue());
  1216. assertEquals("http://invalid.uri", c.getHyperlink().getAddress());
  1217. }
  1218. /**
  1219. * Was giving NullPointerException
  1220. * at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead
  1221. * due to a lack of Styles Table
  1222. */
  1223. @Test
  1224. public void bug56278() throws Exception {
  1225. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56278.xlsx");
  1226. assertEquals(0, wb.getSheetIndex("Market Rates"));
  1227. // Save and re-check
  1228. Workbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  1229. assertEquals(0, nwb.getSheetIndex("Market Rates"));
  1230. }
  1231. @Test
  1232. public void bug56315() {
  1233. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56315.xlsx");
  1234. Cell c = wb.getSheetAt(0).getRow(1).getCell(0);
  1235. CellValue cv = wb.getCreationHelper().createFormulaEvaluator().evaluate(c);
  1236. double rounded = cv.getNumberValue();
  1237. assertEquals(0.1, rounded, 0.0);
  1238. }
  1239. @Test
  1240. public void bug56468() throws Exception {
  1241. XSSFWorkbook wb = new XSSFWorkbook();
  1242. XSSFSheet sheet = wb.createSheet();
  1243. XSSFRow row = sheet.createRow(0);
  1244. XSSFCell cell = row.createCell(0);
  1245. cell.setCellValue("Hi");
  1246. sheet.setRepeatingRows(new CellRangeAddress(0, 0, 0, 0));
  1247. // small hack to try to make this test stable, previously it failed whenever the two written ZIP files had different file-creation
  1248. // dates stored.
  1249. // We try to do a loop until the current second changes in order to avoid problems with some date information that is written to the ZIP and thus
  1250. // causes differences
  1251. long start = System.currentTimeMillis()/1000;
  1252. while(System.currentTimeMillis()/1000 == start) {
  1253. Thread.sleep(10);
  1254. }
  1255. ByteArrayOutputStream bos = new ByteArrayOutputStream(8096);
  1256. wb.write(bos);
  1257. byte firstSave[] = bos.toByteArray();
  1258. bos.reset();
  1259. wb.write(bos);
  1260. byte secondSave[] = bos.toByteArray();
  1261. /*OutputStream stream = new FileOutputStream("C:\\temp\\poi.xlsx");
  1262. try {
  1263. wb.write(stream);
  1264. } finally {
  1265. stream.close();
  1266. }*/
  1267. assertArrayEquals("Had: \n" + Arrays.toString(firstSave) + " and \n" + Arrays.toString(secondSave),
  1268. firstSave, secondSave);
  1269. wb.close();
  1270. }
  1271. /**
  1272. * ISO-8601 style cell formats with a T in them, eg
  1273. * cell format of "yyyy-MM-ddTHH:mm:ss"
  1274. */
  1275. @Test
  1276. public void bug54034() throws IOException {
  1277. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("54034.xlsx");
  1278. Sheet sheet = wb.getSheet("Sheet1");
  1279. Row row = sheet.getRow(1);
  1280. Cell cell = row.getCell(2);
  1281. assertTrue(DateUtil.isCellDateFormatted(cell));
  1282. DataFormatter fmt = new DataFormatter();
  1283. assertEquals("yyyy\\-mm\\-dd\\Thh:mm", cell.getCellStyle().getDataFormatString());
  1284. assertEquals("2012-08-08T22:59", fmt.formatCellValue(cell));
  1285. }
  1286. @Test
  1287. public void testBug53798XLSX() throws IOException {
  1288. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("53798_shiftNegative_TMPL.xlsx");
  1289. File xlsOutput = TempFile.createTempFile("testBug53798", ".xlsx");
  1290. bug53798Work(wb, xlsOutput);
  1291. }
  1292. @Ignore("Shifting rows is not yet implemented in SXSSFSheet")
  1293. @Test
  1294. public void testBug53798XLSXStream() throws IOException {
  1295. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("53798_shiftNegative_TMPL.xlsx");
  1296. File xlsOutput = TempFile.createTempFile("testBug53798", ".xlsx");
  1297. bug53798Work(new SXSSFWorkbook(wb), xlsOutput);
  1298. }
  1299. @Test
  1300. public void testBug53798XLS() throws IOException {
  1301. Workbook wb = HSSFTestDataSamples.openSampleWorkbook("53798_shiftNegative_TMPL.xls");
  1302. File xlsOutput = TempFile.createTempFile("testBug53798", ".xls");
  1303. bug53798Work(wb, xlsOutput);
  1304. }
  1305. /**
  1306. * SUMIF was throwing a NPE on some formulas
  1307. */
  1308. @Test
  1309. public void testBug56420SumIfNPE() throws Exception {
  1310. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56420.xlsx");
  1311. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  1312. Sheet sheet = wb.getSheetAt(0);
  1313. Row r = sheet.getRow(2);
  1314. Cell c = r.getCell(2);
  1315. assertEquals("SUMIF($A$1:$A$4,A3,$B$1:$B$4)", c.getCellFormula());
  1316. Cell eval = evaluator.evaluateInCell(c);
  1317. assertEquals(0.0, eval.getNumericCellValue(), 0.0001);
  1318. }
  1319. private void bug53798Work(Workbook wb, File xlsOutput) throws IOException {
  1320. Sheet testSheet = wb.getSheetAt(0);
  1321. testSheet.shiftRows(2, 2, 1);
  1322. saveAndReloadReport(wb, xlsOutput);
  1323. // 1) corrupted xlsx (unreadable data in the first row of a shifted group) already comes about
  1324. // when shifted by less than -1 negative amount (try -2)
  1325. testSheet.shiftRows(3, 3, -1);
  1326. saveAndReloadReport(wb, xlsOutput);
  1327. testSheet.shiftRows(2, 2, 1);
  1328. saveAndReloadReport(wb, xlsOutput);
  1329. Row newRow = null;
  1330. Cell newCell = null;
  1331. // 2) attempt to create a new row IN PLACE of a removed row by a negative shift causes corrupted
  1332. // xlsx file with unreadable data in the negative shifted row.
  1333. // NOTE it's ok to create any other row.
  1334. newRow = testSheet.createRow(3);
  1335. saveAndReloadReport(wb, xlsOutput);
  1336. newCell = newRow.createCell(0);
  1337. saveAndReloadReport(wb, xlsOutput);
  1338. newCell.setCellValue("new Cell in row "+newRow.getRowNum());
  1339. saveAndReloadReport(wb, xlsOutput);
  1340. // 3) once a negative shift has been made any attempt to shift another group of rows
  1341. // (note: outside of previously negative shifted rows) by a POSITIVE amount causes POI exception:
  1342. // org.apache.xmlbeans.impl.values.XmlValueDisconnectedException.
  1343. // NOTE: another negative shift on another group of rows is successful, provided no new rows in
  1344. // place of previously shifted rows were attempted to be created as explained above.
  1345. testSheet.shiftRows(6, 7, 1); // -- CHANGE the shift to positive once the behaviour of
  1346. // the above has been tested
  1347. saveAndReloadReport(wb, xlsOutput);
  1348. }
  1349. /**
  1350. * XSSFCell.typeMismatch on certain blank cells when formatting
  1351. * with DataFormatter
  1352. */
  1353. @Test
  1354. public void bug56702() throws Exception {
  1355. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56702.xlsx");
  1356. Sheet sheet = wb.getSheetAt(0);
  1357. // Get wrong cell by row 8 & column 7
  1358. Cell cell = sheet.getRow(8).getCell(7);
  1359. assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
  1360. // Check the value - will be zero as it is <c><v/></c>
  1361. assertEquals(0.0, cell.getNumericCellValue(), 0.001);
  1362. // Try to format
  1363. DataFormatter formatter = new DataFormatter();
  1364. formatter.formatCellValue(cell);
  1365. // Check the formatting
  1366. assertEquals("0", formatter.formatCellValue(cell));
  1367. }
  1368. /**
  1369. * Formulas which reference named ranges, either in other
  1370. * sheets, or workbook scoped but in other workbooks.
  1371. * Used to fail with with errors like
  1372. * org.apache.poi.ss.formula.FormulaParseException: Cell reference expected after sheet name at index 9
  1373. * org.apache.poi.ss.formula.FormulaParseException: Parse error near char 0 '[' in specified formula '[0]!NR_Global_B2'. Expected number, string, or defined name
  1374. */
  1375. @Test
  1376. public void bug56737() throws IOException {
  1377. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56737.xlsx");
  1378. // Check the named range definitions
  1379. Name nSheetScope = wb.getName("NR_To_A1");
  1380. Name nWBScope = wb.getName("NR_Global_B2");
  1381. assertNotNull(nSheetScope);
  1382. assertNotNull(nWBScope);
  1383. assertEquals("Defines!$A$1", nSheetScope.getRefersToFormula());
  1384. assertEquals("Defines!$B$2", nWBScope.getRefersToFormula());
  1385. // Check the different kinds of formulas
  1386. Sheet s = wb.getSheetAt(0);
  1387. Cell cRefSName = s.getRow(1).getCell(3);
  1388. Cell cRefWName = s.getRow(2).getCell(3);
  1389. assertEquals("Defines!NR_To_A1", cRefSName.getCellFormula());
  1390. // Note the formula, as stored in the file, has the external name index not filename
  1391. // TODO Provide a way to get the one with the filename
  1392. assertEquals("[0]!NR_Global_B2", cRefWName.getCellFormula());
  1393. // Try to evaluate them
  1394. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  1395. assertEquals("Test A1", eval.evaluate(cRefSName).getStringValue());
  1396. assertEquals(142, (int)eval.evaluate(cRefWName).getNumberValue());
  1397. // Try to evaluate everything
  1398. eval.evaluateAll();
  1399. }
  1400. private void saveAndReloadReport(Workbook wb, File outFile) throws IOException {
  1401. // run some method on the font to verify if it is "disconnected" already
  1402. //for(short i = 0;i < 256;i++)
  1403. {
  1404. Font font = wb.getFontAt((short)0);
  1405. if(font instanceof XSSFFont) {
  1406. XSSFFont xfont = (XSSFFont) wb.getFontAt((short)0);
  1407. CTFontImpl ctFont = (CTFontImpl) xfont.getCTFont();
  1408. assertEquals(0, ctFont.sizeOfBArray());
  1409. }
  1410. }
  1411. FileOutputStream fileOutStream = new FileOutputStream(outFile);
  1412. wb.write(fileOutStream);
  1413. fileOutStream.close();
  1414. //System.out.println("File \""+outFile.getName()+"\" has been saved successfully");
  1415. FileInputStream is = new FileInputStream(outFile);
  1416. try {
  1417. Workbook newWB = null;
  1418. try {
  1419. if(wb instanceof XSSFWorkbook) {
  1420. newWB = new XSSFWorkbook(is);
  1421. } else if(wb instanceof HSSFWorkbook) {
  1422. newWB = new HSSFWorkbook(is);
  1423. } else if(wb instanceof SXSSFWorkbook) {
  1424. newWB = new SXSSFWorkbook(new XSSFWorkbook(is));
  1425. } else {
  1426. throw new IllegalStateException("Unknown workbook: " + wb);
  1427. }
  1428. assertNotNull(newWB.getSheet("test"));
  1429. } finally {
  1430. newWB.close();
  1431. }
  1432. } finally {
  1433. is.close();
  1434. }
  1435. }
  1436. @Test
  1437. public void testBug56688_1() {
  1438. XSSFWorkbook excel = XSSFTestDataSamples.openSampleWorkbook("56688_1.xlsx");
  1439. checkValue(excel, "-1.0"); /* Not 0.0 because POI sees date "0" minus one month as invalid date, which is -1! */
  1440. }
  1441. @Test
  1442. public void testBug56688_2() {
  1443. XSSFWorkbook excel = XSSFTestDataSamples.openSampleWorkbook("56688_2.xlsx");
  1444. checkValue(excel, "#VALUE!");
  1445. }
  1446. @Test
  1447. public void testBug56688_3() {
  1448. XSSFWorkbook excel = XSSFTestDataSamples.openSampleWorkbook("56688_3.xlsx");
  1449. checkValue(excel, "#VALUE!");
  1450. }
  1451. @Test
  1452. public void testBug56688_4() {
  1453. XSSFWorkbook excel = XSSFTestDataSamples.openSampleWorkbook("56688_4.xlsx");
  1454. Calendar calendar = Calendar.getInstance();
  1455. calendar.add(Calendar.MONTH, 2);
  1456. double excelDate = DateUtil.getExcelDate(calendar.getTime());
  1457. NumberEval eval = new NumberEval(Math.floor(excelDate));
  1458. checkValue(excel, eval.getStringValue() + ".0");
  1459. }
  1460. /**
  1461. * New hyperlink with no initial cell reference, still need
  1462. * to be able to change it
  1463. * @throws IOException
  1464. */
  1465. @Test
  1466. public void testBug56527() throws IOException {
  1467. XSSFWorkbook wb = new XSSFWorkbook();
  1468. XSSFSheet sheet = wb.createSheet();
  1469. XSSFCreationHelper creationHelper = wb.getCreationHelper();
  1470. XSSFHyperlink hyperlink;
  1471. // Try with a cell reference
  1472. hyperlink = creationHelper.createHyperlink(Hyperlink.LINK_URL);
  1473. sheet.addHyperlink(hyperlink);
  1474. hyperlink.setAddress("http://myurl");
  1475. hyperlink.setCellReference("B4");
  1476. assertEquals(3, hyperlink.getFirstRow());
  1477. assertEquals(1, hyperlink.getFirstColumn());
  1478. assertEquals(3, hyperlink.getLastRow());
  1479. assertEquals(1, hyperlink.getLastColumn());
  1480. // Try with explicit rows / columns
  1481. hyperlink = creationHelper.createHyperlink(Hyperlink.LINK_URL);
  1482. sheet.addHyperlink(hyperlink);
  1483. hyperlink.setAddress("http://myurl");
  1484. hyperlink.setFirstRow(5);
  1485. hyperlink.setFirstColumn(3);
  1486. assertEquals(5, hyperlink.getFirstRow());
  1487. assertEquals(3, hyperlink.getFirstColumn());
  1488. assertEquals(5, hyperlink.getLastRow());
  1489. assertEquals(3, hyperlink.getLastColumn());
  1490. wb.close();
  1491. }
  1492. /**
  1493. * Shifting rows with a formula that references a
  1494. * function in another file
  1495. */
  1496. @Test
  1497. public void bug56502() throws Exception {
  1498. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56502.xlsx");
  1499. Sheet sheet = wb.getSheetAt(0);
  1500. Cell cFunc = sheet.getRow(3).getCell(0);
  1501. assertEquals("[1]!LUCANET(\"Ist\")", cFunc.getCellFormula());
  1502. Cell cRef = sheet.getRow(3).createCell(1);
  1503. cRef.setCellFormula("A3");
  1504. // Shift it down one row
  1505. sheet.shiftRows(1, sheet.getLastRowNum(), 1);
  1506. // Check the new formulas: Function won't change, Reference will
  1507. cFunc = sheet.getRow(4).getCell(0);
  1508. assertEquals("[1]!LUCANET(\"Ist\")", cFunc.getCellFormula());
  1509. cRef = sheet.getRow(4).getCell(1);
  1510. assertEquals("A4", cRef.getCellFormula());
  1511. }
  1512. @Test
  1513. public void bug54764() throws Exception {
  1514. OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("54764.xlsx");
  1515. // Check the core properties - will be found but empty, due
  1516. // to the expansion being too much to be considered valid
  1517. POIXMLProperties props = new POIXMLProperties(pkg);
  1518. assertEquals(null, props.getCoreProperties().getTitle());
  1519. assertEquals(null, props.getCoreProperties().getSubject());
  1520. assertEquals(null, props.getCoreProperties().getDescription());
  1521. // Now check the spreadsheet itself
  1522. try {
  1523. new XSSFWorkbook(pkg).close();
  1524. fail("Should fail as too much expansion occurs");
  1525. } catch(POIXMLException e) {
  1526. // Expected
  1527. }
  1528. // Try with one with the entities in the Content Types
  1529. try {
  1530. XSSFTestDataSamples.openSamplePackage("54764-2.xlsx");
  1531. fail("Should fail as too much expansion occurs");
  1532. } catch(Exception e) {
  1533. // Expected
  1534. }
  1535. // Check we can still parse valid files after all that
  1536. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx");
  1537. assertEquals(3, wb.getNumberOfSheets());
  1538. }
  1539. /**
  1540. * CTDefinedNamesImpl should be included in the smaller
  1541. * poi-ooxml-schemas jar
  1542. */
  1543. @Test
  1544. public void bug57176() throws Exception {
  1545. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57176.xlsx");
  1546. CTDefinedNames definedNames = wb.getCTWorkbook().getDefinedNames();
  1547. List<CTDefinedName> definedNameList = definedNames.getDefinedNameList();
  1548. for (CTDefinedName defName : definedNameList) {
  1549. assertNotNull(defName.getName());
  1550. assertNotNull(defName.getStringValue());
  1551. }
  1552. assertEquals("TestDefinedName", definedNameList.get(0).getName());
  1553. }
  1554. /**
  1555. * .xlsb files are not supported, but we should generate a helpful
  1556. * error message if given one
  1557. */
  1558. @Test
  1559. public void bug56800_xlsb() throws Exception {
  1560. // Can be opened at the OPC level
  1561. OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("Simple.xlsb");
  1562. // XSSF Workbook gives helpful error
  1563. try {
  1564. new XSSFWorkbook(pkg).close();
  1565. fail(".xlsb files not supported");
  1566. } catch (XLSBUnsupportedException e) {
  1567. // Good, detected and warned
  1568. }
  1569. // Workbook Factory gives helpful error on package
  1570. try {
  1571. WorkbookFactory.create(pkg);
  1572. fail(".xlsb files not supported");
  1573. } catch (XLSBUnsupportedException e) {
  1574. // Good, detected and warned
  1575. }
  1576. // Workbook Factory gives helpful error on file
  1577. File xlsbFile = HSSFTestDataSamples.getSampleFile("Simple.xlsb");
  1578. try {
  1579. WorkbookFactory.create(xlsbFile);
  1580. fail(".xlsb files not supported");
  1581. } catch (XLSBUnsupportedException e) {
  1582. // Good, detected and warned
  1583. }
  1584. }
  1585. private void checkValue(XSSFWorkbook excel, String expect) {
  1586. XSSFFormulaEvaluator evaluator = new XSSFFormulaEvaluator(excel);
  1587. evaluator.evaluateAll();
  1588. XSSFCell cell = excel.getSheetAt(0).getRow(1).getCell(1);
  1589. CellValue value = evaluator.evaluate(cell);
  1590. assertEquals(expect, value.formatAsString());
  1591. }
  1592. @Test
  1593. public void testBug57196() throws IOException {
  1594. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57196.xlsx");
  1595. Sheet sheet = wb.getSheet("Feuil1");
  1596. Row mod=sheet.getRow(1);
  1597. mod.getCell(1).setCellValue(3);
  1598. HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  1599. // FileOutputStream fileOutput = new FileOutputStream("/tmp/57196.xlsx");
  1600. // wb.write(fileOutput);
  1601. // fileOutput.close();
  1602. wb.close();
  1603. }
  1604. @Test
  1605. public void test57196_Detail() {
  1606. XSSFWorkbook wb = new XSSFWorkbook();
  1607. XSSFSheet sheet = wb.createSheet("Sheet1");
  1608. XSSFRow row = sheet.createRow(0);
  1609. XSSFCell cell = row.createCell(0);
  1610. cell.setCellFormula("DEC2HEX(HEX2DEC(O8)-O2+D2)");
  1611. XSSFFormulaEvaluator fe = new XSSFFormulaEvaluator(wb);
  1612. CellValue cv = fe.evaluate(cell);
  1613. assertNotNull(cv);
  1614. }
  1615. @Test
  1616. public void test57196_Detail2() {
  1617. XSSFWorkbook wb = new XSSFWorkbook();
  1618. XSSFSheet sheet = wb.createSheet("Sheet1");
  1619. XSSFRow row = sheet.createRow(0);
  1620. XSSFCell cell = row.createCell(0);
  1621. cell.setCellFormula("DEC2HEX(O2+D2)");
  1622. XSSFFormulaEvaluator fe = new XSSFFormulaEvaluator(wb);
  1623. CellValue cv = fe.evaluate(cell);
  1624. assertNotNull(cv);
  1625. }
  1626. @Test
  1627. public void test57196_WorkbookEvaluator() {
  1628. String previousLogger = System.getProperty("org.apache.poi.util.POILogger");
  1629. //System.setProperty("org.apache.poi.util.POILogger", "org.apache.poi.util.SystemOutLogger");
  1630. //System.setProperty("poi.log.level", "3");
  1631. try {
  1632. XSSFWorkbook wb = new XSSFWorkbook();
  1633. XSSFSheet sheet = wb.createSheet("Sheet1");
  1634. XSSFRow row = sheet.createRow(0);
  1635. XSSFCell cell = row.createCell(0);
  1636. cell.setCellValue("0");
  1637. cell = row.createCell(1);
  1638. cell.setCellValue(0);
  1639. cell = row.createCell(2);
  1640. cell.setCellValue(0);
  1641. // simple formula worked
  1642. cell.setCellFormula("DEC2HEX(O2+D2)");
  1643. WorkbookEvaluator workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null);
  1644. workbookEvaluator.setDebugEvaluationOutputForNextEval(true);
  1645. workbookEvaluator.evaluate(new XSSFEvaluationCell(cell));
  1646. // this already failed! Hex2Dec did not correctly handle RefEval
  1647. cell.setCellFormula("HEX2DEC(O8)");
  1648. workbookEvaluator.clearAllCachedResultValues();
  1649. workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null);
  1650. workbookEvaluator.setDebugEvaluationOutputForNextEval(true);
  1651. workbookEvaluator.evaluate(new XSSFEvaluationCell(cell));
  1652. // slightly more complex one failed
  1653. cell.setCellFormula("HEX2DEC(O8)-O2+D2");
  1654. workbookEvaluator.clearAllCachedResultValues();
  1655. workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null);
  1656. workbookEvaluator.setDebugEvaluationOutputForNextEval(true);
  1657. workbookEvaluator.evaluate(new XSSFEvaluationCell(cell));
  1658. // more complicated failed
  1659. cell.setCellFormula("DEC2HEX(HEX2DEC(O8)-O2+D2)");
  1660. workbookEvaluator.clearAllCachedResultValues();
  1661. workbookEvaluator.setDebugEvaluationOutputForNextEval(true);
  1662. workbookEvaluator.evaluate(new XSSFEvaluationCell(cell));
  1663. // what other similar functions
  1664. cell.setCellFormula("DEC2BIN(O8)-O2+D2");
  1665. workbookEvaluator.clearAllCachedResultValues();
  1666. workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null);
  1667. workbookEvaluator.setDebugEvaluationOutputForNextEval(true);
  1668. workbookEvaluator.evaluate(new XSSFEvaluationCell(cell));
  1669. // what other similar functions
  1670. cell.setCellFormula("DEC2BIN(A1)");
  1671. workbookEvaluator.clearAllCachedResultValues();
  1672. workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null);
  1673. workbookEvaluator.setDebugEvaluationOutputForNextEval(true);
  1674. workbookEvaluator.evaluate(new XSSFEvaluationCell(cell));
  1675. // what other similar functions
  1676. cell.setCellFormula("BIN2DEC(B1)");
  1677. workbookEvaluator.clearAllCachedResultValues();
  1678. workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null);
  1679. workbookEvaluator.setDebugEvaluationOutputForNextEval(true);
  1680. workbookEvaluator.evaluate(new XSSFEvaluationCell(cell));
  1681. } finally {
  1682. if(previousLogger == null) {
  1683. System.clearProperty("org.apache.poi.util.POILogger");
  1684. } else {
  1685. System.setProperty("org.apache.poi.util.POILogger", previousLogger);
  1686. }
  1687. System.clearProperty("poi.log.level");
  1688. }
  1689. }
  1690. /**
  1691. * A .xlsx file with no Shared Strings table should open fine
  1692. * in read-only mode
  1693. */
  1694. @SuppressWarnings("resource")
  1695. @Test
  1696. public void bug57482() throws Exception {
  1697. for (PackageAccess access : new PackageAccess[] {
  1698. PackageAccess.READ_WRITE, PackageAccess.READ
  1699. }) {
  1700. File file = HSSFTestDataSamples.getSampleFile("57482-OnlyNumeric.xlsx");
  1701. OPCPackage pkg = OPCPackage.open(file, access);
  1702. try {
  1703. // Try to open it and read the contents
  1704. XSSFWorkbook wb = new XSSFWorkbook(pkg);
  1705. assertNotNull(wb.getSharedStringSource());
  1706. assertEquals(0, wb.getSharedStringSource().getCount());
  1707. DataFormatter fmt = new DataFormatter();
  1708. XSSFSheet s = wb.getSheetAt(0);
  1709. assertEquals("1", fmt.formatCellValue(s.getRow(0).getCell(0)));
  1710. assertEquals("11", fmt.formatCellValue(s.getRow(0).getCell(1)));
  1711. assertEquals("5", fmt.formatCellValue(s.getRow(4).getCell(0)));
  1712. // Add a text cell
  1713. s.getRow(0).createCell(3).setCellValue("Testing");
  1714. assertEquals("Testing", fmt.formatCellValue(s.getRow(0).getCell(3)));
  1715. // Try to write-out and read again, should only work
  1716. // in read-write mode, not read-only mode
  1717. try {
  1718. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  1719. if (access == PackageAccess.READ)
  1720. fail("Shouln't be able to write from read-only mode");
  1721. } catch (InvalidOperationException e) {
  1722. if (access == PackageAccess.READ) {
  1723. // Expected
  1724. } else {
  1725. // Shouldn't occur in write-mode
  1726. throw e;
  1727. }
  1728. }
  1729. // Check again
  1730. s = wb.getSheetAt(0);
  1731. assertEquals("1", fmt.formatCellValue(s.getRow(0).getCell(0)));
  1732. assertEquals("11", fmt.formatCellValue(s.getRow(0).getCell(1)));
  1733. assertEquals("5", fmt.formatCellValue(s.getRow(4).getCell(0)));
  1734. assertEquals("Testing", fmt.formatCellValue(s.getRow(0).getCell(3)));
  1735. } finally {
  1736. pkg.revert();
  1737. }
  1738. }
  1739. }
  1740. /**
  1741. * "Unknown error type: -60" fetching formula error value
  1742. */
  1743. @Test
  1744. public void bug57535() throws Exception {
  1745. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57535.xlsx");
  1746. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  1747. evaluator.clearAllCachedResultValues();
  1748. Sheet sheet = wb.getSheet("Sheet1");
  1749. Cell cell = sheet.getRow(5).getCell(4);
  1750. assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType());
  1751. assertEquals("E4+E5", cell.getCellFormula());
  1752. CellValue value = evaluator.evaluate(cell);
  1753. assertEquals(Cell.CELL_TYPE_ERROR, value.getCellType());
  1754. assertEquals(-60, value.getErrorValue());
  1755. assertEquals("~CIRCULAR~REF~", FormulaError.forInt(value.getErrorValue()).getString());
  1756. assertEquals("CIRCULAR_REF", FormulaError.forInt(value.getErrorValue()).toString());
  1757. }
  1758. @Test
  1759. public void test57165() throws IOException {
  1760. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
  1761. try {
  1762. removeAllSheetsBut(3, wb);
  1763. wb.cloneSheet(0); // Throws exception here
  1764. wb.setSheetName(1, "New Sheet");
  1765. //saveWorkbook(wb, fileName);
  1766. XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
  1767. try {
  1768. } finally {
  1769. wbBack.close();
  1770. }
  1771. } finally {
  1772. wb.close();
  1773. }
  1774. }
  1775. @Test
  1776. public void test57165_create() throws IOException {
  1777. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
  1778. try {
  1779. removeAllSheetsBut(3, wb);
  1780. wb.createSheet("newsheet"); // Throws exception here
  1781. wb.setSheetName(1, "New Sheet");
  1782. //saveWorkbook(wb, fileName);
  1783. XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
  1784. try {
  1785. } finally {
  1786. wbBack.close();
  1787. }
  1788. } finally {
  1789. wb.close();
  1790. }
  1791. }
  1792. private static void removeAllSheetsBut(int sheetIndex, Workbook wb)
  1793. {
  1794. int sheetNb = wb.getNumberOfSheets();
  1795. // Move this sheet at the first position
  1796. wb.setSheetOrder(wb.getSheetName(sheetIndex), 0);
  1797. for (int sn = sheetNb - 1; sn > 0; sn--)
  1798. {
  1799. wb.removeSheetAt(sn);
  1800. }
  1801. }
  1802. /**
  1803. * Sums 2 plus the cell at the left, indirectly to avoid reference
  1804. * problems when deleting columns, conditionally to stop recursion
  1805. */
  1806. private static final String FORMULA1 =
  1807. "IF( INDIRECT( ADDRESS( ROW(), COLUMN()-1 ) ) = 0, 0,"
  1808. + "INDIRECT( ADDRESS( ROW(), COLUMN()-1 ) ) ) + 2";
  1809. /**
  1810. * Sums 2 plus the upper cell, indirectly to avoid reference
  1811. * problems when deleting rows, conditionally to stop recursion
  1812. */
  1813. private static final String FORMULA2 =
  1814. "IF( INDIRECT( ADDRESS( ROW()-1, COLUMN() ) ) = 0, 0,"
  1815. + "INDIRECT( ADDRESS( ROW()-1, COLUMN() ) ) ) + 2";
  1816. /**
  1817. * Expected:
  1818. * [ 0][ 2][ 4]
  1819. * @throws IOException
  1820. */
  1821. @Test
  1822. public void testBug56820_Formula1() throws IOException {
  1823. Workbook wb = new XSSFWorkbook();
  1824. try {
  1825. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  1826. Sheet sh = wb.createSheet();
  1827. sh.createRow(0).createCell(0).setCellValue(0.0d);
  1828. Cell formulaCell1 = sh.getRow(0).createCell(1);
  1829. Cell formulaCell2 = sh.getRow(0).createCell(2);
  1830. formulaCell1.setCellFormula(FORMULA1);
  1831. formulaCell2.setCellFormula(FORMULA1);
  1832. double A1 = evaluator.evaluate(formulaCell1).getNumberValue();
  1833. double A2 = evaluator.evaluate(formulaCell2).getNumberValue();
  1834. assertEquals(2, A1, 0);
  1835. assertEquals(4, A2, 0); //<-- FAILS EXPECTATIONS
  1836. } finally {
  1837. wb.close();
  1838. }
  1839. }
  1840. /**
  1841. * Expected:
  1842. * [ 0] <- number
  1843. * [ 2] <- formula
  1844. * [ 4] <- formula
  1845. * @throws IOException
  1846. */
  1847. @Test
  1848. public void testBug56820_Formula2() throws IOException {
  1849. Workbook wb = new XSSFWorkbook();
  1850. try {
  1851. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  1852. Sheet sh = wb.createSheet();
  1853. sh.createRow(0).createCell(0).setCellValue(0.0d);
  1854. Cell formulaCell1 = sh.createRow(1).createCell(0);
  1855. Cell formulaCell2 = sh.createRow(2).createCell(0);
  1856. formulaCell1.setCellFormula(FORMULA2);
  1857. formulaCell2.setCellFormula(FORMULA2);
  1858. double A1 = evaluator.evaluate(formulaCell1).getNumberValue();
  1859. double A2 = evaluator.evaluate(formulaCell2).getNumberValue(); //<-- FAILS EVALUATION
  1860. assertEquals(2, A1, 0);
  1861. assertEquals(4, A2, 0);
  1862. } finally {
  1863. wb.close();
  1864. }
  1865. }
  1866. @Test
  1867. public void test56467() throws IOException {
  1868. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("picture.xlsx");
  1869. try {
  1870. Sheet orig = wb.getSheetAt(0);
  1871. assertNotNull(orig);
  1872. Sheet sheet = wb.cloneSheet(0);
  1873. Drawing drawing = sheet.createDrawingPatriarch();
  1874. for (XSSFShape shape : ((XSSFDrawing) drawing).getShapes()) {
  1875. if (shape instanceof XSSFPicture) {
  1876. XSSFPictureData pictureData = ((XSSFPicture) shape).getPictureData();
  1877. assertNotNull(pictureData);
  1878. }
  1879. }
  1880. // OutputStream out = new FileOutputStream("/tmp/56467.xls");
  1881. // try {
  1882. // wb.write(out);
  1883. // } finally {
  1884. // out.close();
  1885. // }
  1886. } finally {
  1887. wb.close();
  1888. }
  1889. }
  1890. /**
  1891. * OOXML-Strict files
  1892. * Not currently working - namespace mis-match from XMLBeans
  1893. */
  1894. @Test
  1895. @Ignore("XMLBeans namespace mis-match on ooxml-strict files")
  1896. public void test57699() throws Exception {
  1897. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("sample.strict.xlsx");
  1898. assertEquals(3, wb.getNumberOfSheets());
  1899. // TODO Check sheet contents
  1900. // TODO Check formula evaluation
  1901. XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
  1902. assertEquals(3, wbBack.getNumberOfSheets());
  1903. // TODO Re-check sheet contents
  1904. // TODO Re-check formula evaluation
  1905. }
  1906. @Test
  1907. public void testBug56295_MergeXlslsWithStyles() throws IOException {
  1908. XSSFWorkbook xlsToAppendWorkbook = XSSFTestDataSamples.openSampleWorkbook("56295.xlsx");
  1909. XSSFSheet sheet = xlsToAppendWorkbook.getSheetAt(0);
  1910. XSSFRow srcRow = sheet.getRow(0);
  1911. XSSFCell oldCell = srcRow.getCell(0);
  1912. XSSFCellStyle cellStyle = oldCell.getCellStyle();
  1913. checkStyle(cellStyle);
  1914. // StylesTable table = xlsToAppendWorkbook.getStylesSource();
  1915. // List<XSSFCellFill> fills = table.getFills();
  1916. // System.out.println("Having " + fills.size() + " fills");
  1917. // for(XSSFCellFill fill : fills) {
  1918. // System.out.println("Fill: " + fill.getFillBackgroundColor() + "/" + fill.getFillForegroundColor());
  1919. // }
  1920. XSSFWorkbook targetWorkbook = new XSSFWorkbook();
  1921. XSSFSheet newSheet = targetWorkbook.createSheet(sheet.getSheetName());
  1922. XSSFRow destRow = newSheet.createRow(0);
  1923. XSSFCell newCell = destRow.createCell(0);
  1924. //newCell.getCellStyle().cloneStyleFrom(cellStyle);
  1925. CellStyle newCellStyle = targetWorkbook.createCellStyle();
  1926. newCellStyle.cloneStyleFrom(cellStyle);
  1927. newCell.setCellStyle(newCellStyle);
  1928. checkStyle(newCell.getCellStyle());
  1929. newCell.setCellValue(oldCell.getStringCellValue());
  1930. // OutputStream os = new FileOutputStream("output.xlsm");
  1931. // try {
  1932. // targetWorkbook.write(os);
  1933. // } finally {
  1934. // os.close();
  1935. // }
  1936. XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(targetWorkbook);
  1937. XSSFCellStyle styleBack = wbBack.getSheetAt(0).getRow(0).getCell(0).getCellStyle();
  1938. checkStyle(styleBack);
  1939. }
  1940. /**
  1941. * Paragraph with property BuFont but none of the properties
  1942. * BuNone, BuChar, and BuAutoNum, used to trigger a NPE
  1943. * Excel treats this as not-bulleted, so now do we
  1944. */
  1945. @Test
  1946. public void testBug57826() {
  1947. XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("57826.xlsx");
  1948. assertTrue("no sheets in workbook", workbook.getNumberOfSheets() >= 1);
  1949. XSSFSheet sheet = workbook.getSheetAt(0);
  1950. XSSFDrawing drawing = sheet.getDrawingPatriarch();
  1951. assertNotNull(drawing);
  1952. List<XSSFShape> shapes = drawing.getShapes();
  1953. assertEquals(1, shapes.size());
  1954. assertTrue(shapes.get(0) instanceof XSSFSimpleShape);
  1955. XSSFSimpleShape shape = (XSSFSimpleShape)shapes.get(0);
  1956. // Used to throw a NPE
  1957. String text = shape.getText();
  1958. // No bulleting info included
  1959. assertEquals("test ok", text);
  1960. }
  1961. private void checkStyle(XSSFCellStyle cellStyle) {
  1962. assertNotNull(cellStyle);
  1963. assertEquals(0, cellStyle.getFillForegroundColor());
  1964. assertNotNull(cellStyle.getFillForegroundXSSFColor());
  1965. XSSFColor fgColor = cellStyle.getFillForegroundColorColor();
  1966. assertNotNull(fgColor);
  1967. assertEquals("FF00FFFF", fgColor.getARGBHex());
  1968. assertEquals(0, cellStyle.getFillBackgroundColor());
  1969. assertNotNull(cellStyle.getFillBackgroundXSSFColor());
  1970. XSSFColor bgColor = cellStyle.getFillBackgroundColorColor();
  1971. assertNotNull(bgColor);
  1972. assertEquals("FF00FFFF", fgColor.getARGBHex());
  1973. }
  1974. @Test
  1975. public void bug57642() throws Exception {
  1976. XSSFWorkbook wb = new XSSFWorkbook();
  1977. XSSFSheet s = wb.createSheet("TestSheet");
  1978. XSSFCell c = s.createRow(0).createCell(0);
  1979. c.setCellFormula("ISERROR(TestSheet!A1)");
  1980. c = s.createRow(1).createCell(1);
  1981. c.setCellFormula("ISERROR(B2)");
  1982. wb.setSheetName(0, "CSN");
  1983. c = s.getRow(0).getCell(0);
  1984. assertEquals("ISERROR(CSN!A1)", c.getCellFormula());
  1985. c = s.getRow(1).getCell(1);
  1986. assertEquals("ISERROR(B2)", c.getCellFormula());
  1987. wb.close();
  1988. }
  1989. /**
  1990. * .xlsx supports 64000 cell styles, the style indexes after
  1991. * 32,767 must not be -32,768, then -32,767, -32,766
  1992. */
  1993. @Test
  1994. public void bug57880() throws Exception {
  1995. int numStyles = 33000;
  1996. XSSFWorkbook wb = new XSSFWorkbook();
  1997. //XSSFSheet s = wb.createSheet("TestSheet");
  1998. XSSFDataFormat fmt = wb.getCreationHelper().createDataFormat();
  1999. for (int i=1; i<numStyles; i++) {
  2000. short df = fmt.getFormat("test"+i);
  2001. // Format indexes will be wrapped beyond 32,676
  2002. assertEquals(164+i, df&0xffff);
  2003. // Create a style and use it
  2004. XSSFCellStyle style = wb.createCellStyle();
  2005. assertEquals(i, style.getUIndex());
  2006. style.setDataFormat(df);
  2007. /*XSSFCell c = s.createRow(i).createCell(0, Cell.CELL_TYPE_NUMERIC);
  2008. c.setCellStyle(style);
  2009. c.setCellValue(i);*/
  2010. }
  2011. // using temp file instead of ByteArrayOutputStream because of OOM in gump run
  2012. File tmp = TempFile.createTempFile("poi-test", ".bug57880");
  2013. FileOutputStream fos = new FileOutputStream(tmp);
  2014. wb.write(fos);
  2015. fos.close();
  2016. wb.close();
  2017. fmt = null; /*s = null;*/ wb = null;
  2018. // System.gc();
  2019. wb = new XSSFWorkbook(tmp);
  2020. fmt = wb.getCreationHelper().createDataFormat();
  2021. // s = wb.getSheetAt(0);
  2022. for (int i=1; i<numStyles; i++) {
  2023. XSSFCellStyle style = wb.getCellStyleAt((short)i);
  2024. assertNotNull(style);
  2025. assertEquals(i, style.getUIndex());
  2026. assertEquals(164+i, style.getDataFormat()&0xffff);
  2027. assertEquals("test"+i, style.getDataFormatString());
  2028. }
  2029. wb.close();
  2030. tmp.delete();
  2031. }
  2032. @Test
  2033. public void test56574() throws IOException {
  2034. runTest56574(false);
  2035. runTest56574(true);
  2036. }
  2037. @SuppressWarnings("deprecation")
  2038. private void runTest56574(boolean createRow) throws IOException {
  2039. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56574.xlsx");
  2040. Sheet sheet = wb.getSheet("Func");
  2041. assertNotNull(sheet);
  2042. Map<String, Object[]> data;
  2043. data = new TreeMap<String, Object[]>();
  2044. data.put("1", new Object[] {"ID", "NAME", "LASTNAME"});
  2045. data.put("2", new Object[] {2, "Amit", "Shukla"});
  2046. data.put("3", new Object[] {1, "Lokesh", "Gupta"});
  2047. data.put("4", new Object[] {4, "John", "Adwards"});
  2048. data.put("5", new Object[] {2, "Brian", "Schultz"});
  2049. Set<String> keyset = data.keySet();
  2050. int rownum = 1;
  2051. for (String key : keyset)
  2052. {
  2053. final Row row;
  2054. if(createRow) {
  2055. row = sheet.createRow(rownum++);
  2056. } else {
  2057. row = sheet.getRow(rownum++);
  2058. }
  2059. assertNotNull(row);
  2060. Object [] objArr = data.get(key);
  2061. int cellnum = 0;
  2062. for (Object obj : objArr)
  2063. {
  2064. Cell cell = row.getCell(cellnum);
  2065. if(cell == null){
  2066. cell = row.createCell(cellnum);
  2067. } else {
  2068. if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
  2069. cell.setCellFormula(null);
  2070. cell.getCellStyle().setDataFormat((short) 0);
  2071. }
  2072. }
  2073. if(obj instanceof String) {
  2074. cell.setCellValue((String)obj);
  2075. } else if(obj instanceof Integer) {
  2076. cell.setCellValue((Integer)obj);
  2077. }
  2078. cellnum++;
  2079. }
  2080. }
  2081. XSSFFormulaEvaluator.evaluateAllFormulaCells((XSSFWorkbook) wb);
  2082. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  2083. CalculationChain chain = ((XSSFWorkbook)wb).getCalculationChain();
  2084. CTCalcCell[] cArray = chain.getCTCalcChain().getCArray();
  2085. for(CTCalcCell calc : cArray) {
  2086. // A2 to A6 should be gone
  2087. assertFalse(calc.getR().equals("A2"));
  2088. assertFalse(calc.getR().equals("A3"));
  2089. assertFalse(calc.getR().equals("A4"));
  2090. assertFalse(calc.getR().equals("A5"));
  2091. assertFalse(calc.getR().equals("A6"));
  2092. }
  2093. /*FileOutputStream out = new FileOutputStream(new File("C:\\temp\\56574.xlsx"));
  2094. try {
  2095. wb.write(out);
  2096. } finally {
  2097. out.close();
  2098. }*/
  2099. Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
  2100. Sheet sheetBack = wbBack.getSheet("Func");
  2101. assertNotNull(sheetBack);
  2102. chain = ((XSSFWorkbook)wbBack).getCalculationChain();
  2103. cArray = chain.getCTCalcChain().getCArray();
  2104. for(CTCalcCell calc : cArray) {
  2105. // A2 to A6 should be gone
  2106. assertFalse(calc.getR().equals("A2"));
  2107. assertFalse(calc.getR().equals("A3"));
  2108. assertFalse(calc.getR().equals("A4"));
  2109. assertFalse(calc.getR().equals("A5"));
  2110. assertFalse(calc.getR().equals("A6"));
  2111. }
  2112. wb.close();
  2113. }
  2114. /**
  2115. * Excel 2007 generated Macro-Enabled .xlsm file
  2116. */
  2117. @Test
  2118. public void bug57181() throws Exception {
  2119. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57181.xlsm");
  2120. assertEquals(9, wb.getNumberOfSheets());
  2121. }
  2122. @Test
  2123. public void bug52111() throws Exception {
  2124. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("Intersection-52111-xssf.xlsx");
  2125. Sheet s = wb.getSheetAt(0);
  2126. assertFormula(wb, s.getRow(2).getCell(0), "(C2:D3 D3:E4)", "4.0");
  2127. assertFormula(wb, s.getRow(6).getCell(0), "Tabelle2!E:E Tabelle2!11:11", "5.0");
  2128. assertFormula(wb, s.getRow(8).getCell(0), "Tabelle2!E:F Tabelle2!11:12", null);
  2129. }
  2130. private void assertFormula(Workbook wb, Cell intF, String expectedFormula, String expectedResultOrNull) {
  2131. assertEquals(Cell.CELL_TYPE_FORMULA, intF.getCellType());
  2132. if (null == expectedResultOrNull) {
  2133. assertEquals(Cell.CELL_TYPE_ERROR, intF.getCachedFormulaResultType());
  2134. expectedResultOrNull = "#VALUE!";
  2135. }
  2136. else {
  2137. assertEquals(Cell.CELL_TYPE_NUMERIC, intF.getCachedFormulaResultType());
  2138. }
  2139. assertEquals(expectedFormula, intF.getCellFormula());
  2140. // Check we can evaluate it correctly
  2141. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  2142. assertEquals(expectedResultOrNull, eval.evaluate(intF).formatAsString());
  2143. }
  2144. @Test
  2145. public void test48962() {
  2146. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("48962.xlsx");
  2147. Sheet sh = wb.getSheetAt(0);
  2148. Row row = sh.getRow(1);
  2149. Cell cell = row.getCell(0);
  2150. CellStyle style = cell.getCellStyle();
  2151. assertNotNull(style);
  2152. // color index
  2153. assertEquals(64, style.getFillBackgroundColor());
  2154. XSSFColor color = ((XSSFCellStyle)style).getFillBackgroundXSSFColor();
  2155. assertNotNull(color);
  2156. // indexed color
  2157. assertEquals(64, color.getIndexed());
  2158. assertEquals(64, color.getIndex());
  2159. // not an RGB color
  2160. assertFalse(color.isRGB());
  2161. assertNull(color.getRGB());
  2162. }
  2163. @Test
  2164. public void test50755_workday_formula_example() {
  2165. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("50755_workday_formula_example.xlsx");
  2166. Sheet sheet = wb.getSheet("Sheet1");
  2167. for(Row aRow : sheet) {
  2168. Cell cell = aRow.getCell(1);
  2169. if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
  2170. String formula = cell.getCellFormula();
  2171. //System.out.println("formula: " + formula);
  2172. assertNotNull(formula);
  2173. assertTrue(formula.contains("WORKDAY"));
  2174. } else {
  2175. assertNotNull(cell.toString());
  2176. }
  2177. }
  2178. }
  2179. @Test
  2180. public void test51626() throws Exception {
  2181. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("51626.xlsx");
  2182. assertNotNull(wb);
  2183. wb.close();
  2184. InputStream stream = HSSFTestDataSamples.openSampleFileStream("51626.xlsx");
  2185. wb = WorkbookFactory.create(stream);
  2186. stream.close();
  2187. wb.close();
  2188. wb = XSSFTestDataSamples.openSampleWorkbook("51626_contact.xlsx");
  2189. assertNotNull(wb);
  2190. wb.close();
  2191. stream = HSSFTestDataSamples.openSampleFileStream("51626_contact.xlsx");
  2192. wb = WorkbookFactory.create(stream);
  2193. stream.close();
  2194. wb.close();
  2195. }
  2196. @Test
  2197. public void test51451() throws IOException {
  2198. Workbook wb = new XSSFWorkbook();
  2199. Sheet sh = wb.createSheet();
  2200. Row row = sh.createRow(0);
  2201. Cell cell = row.createCell(0);
  2202. cell.setCellValue(239827342);
  2203. CellStyle style = wb.createCellStyle();
  2204. //style.setHidden(false);
  2205. DataFormat excelFormat = wb.createDataFormat();
  2206. style.setDataFormat(excelFormat.getFormat("#,##0"));
  2207. sh.setDefaultColumnStyle(0, style);
  2208. // FileOutputStream out = new FileOutputStream("/tmp/51451.xlsx");
  2209. // wb.write(out);
  2210. // out.close();
  2211. wb.close();
  2212. }
  2213. @Test
  2214. public void test53105() throws IOException {
  2215. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53105.xlsx");
  2216. assertNotNull(wb);
  2217. // Act
  2218. // evaluate SUM('Skye Lookup Input'!A4:XFD4), cells in range each contain "1"
  2219. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  2220. double numericValue = evaluator.evaluate(wb.getSheetAt(0).getRow(1).getCell(0)).getNumberValue();
  2221. // Assert
  2222. assertEquals(16384.0, numericValue, 0.0);
  2223. wb.close();
  2224. }
  2225. }