You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestBugs.java 105KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143
  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.hssf.usermodel;
  16. import static org.apache.poi.POITestCase.assertContains;
  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.assertNotSame;
  21. import static org.junit.Assert.assertNull;
  22. import static org.junit.Assert.assertSame;
  23. import static org.junit.Assert.assertTrue;
  24. import static org.junit.Assert.fail;
  25. import javax.imageio.ImageIO;
  26. import java.awt.image.BufferedImage;
  27. import java.io.ByteArrayInputStream;
  28. import java.io.ByteArrayOutputStream;
  29. import java.io.File;
  30. import java.io.FileInputStream;
  31. import java.io.IOException;
  32. import java.io.InputStream;
  33. import java.net.URL;
  34. import java.util.ArrayList;
  35. import java.util.Arrays;
  36. import java.util.Calendar;
  37. import java.util.Iterator;
  38. import java.util.List;
  39. import java.util.Locale;
  40. import java.util.TimeZone;
  41. import org.apache.poi.EncryptedDocumentException;
  42. import org.apache.poi.hpsf.PropertySet;
  43. import org.apache.poi.hpsf.SummaryInformation;
  44. import org.apache.poi.hssf.HSSFITestDataProvider;
  45. import org.apache.poi.hssf.HSSFTestDataSamples;
  46. import org.apache.poi.hssf.OldExcelFormatException;
  47. import org.apache.poi.hssf.extractor.ExcelExtractor;
  48. import org.apache.poi.hssf.model.InternalSheet;
  49. import org.apache.poi.hssf.model.InternalWorkbook;
  50. import org.apache.poi.hssf.record.CellValueRecordInterface;
  51. import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
  52. import org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord;
  53. import org.apache.poi.hssf.record.NameRecord;
  54. import org.apache.poi.hssf.record.Record;
  55. import org.apache.poi.hssf.record.TabIdRecord;
  56. import org.apache.poi.hssf.record.UnknownRecord;
  57. import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
  58. import org.apache.poi.hssf.record.aggregates.PageSettingsBlock;
  59. import org.apache.poi.hssf.record.common.UnicodeString;
  60. import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
  61. import org.apache.poi.poifs.filesystem.DocumentEntry;
  62. import org.apache.poi.poifs.filesystem.DocumentInputStream;
  63. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  64. import org.apache.poi.ss.formula.ptg.Area3DPtg;
  65. import org.apache.poi.ss.formula.ptg.DeletedArea3DPtg;
  66. import org.apache.poi.ss.formula.ptg.Ptg;
  67. import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
  68. import org.apache.poi.ss.usermodel.BorderStyle;
  69. import org.apache.poi.ss.usermodel.Cell;
  70. import org.apache.poi.ss.usermodel.CellStyle;
  71. import org.apache.poi.ss.usermodel.CellType;
  72. import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
  73. import org.apache.poi.ss.usermodel.ComparisonOperator;
  74. import org.apache.poi.ss.usermodel.DataFormatter;
  75. import org.apache.poi.ss.usermodel.FormulaError;
  76. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  77. import org.apache.poi.ss.usermodel.Name;
  78. import org.apache.poi.ss.usermodel.Row;
  79. import org.apache.poi.ss.usermodel.Sheet;
  80. import org.apache.poi.ss.usermodel.SheetVisibility;
  81. import org.apache.poi.ss.usermodel.Workbook;
  82. import org.apache.poi.ss.util.CellRangeAddress;
  83. import org.apache.poi.util.LocaleUtil;
  84. import org.junit.Assume;
  85. import org.junit.Ignore;
  86. import org.junit.Test;
  87. /**
  88. * Testcases for bugs entered in bugzilla
  89. * the Test name contains the bugzilla bug id
  90. * <p>
  91. * <b>YK: If a bug can be tested in terms of common ss interfaces,
  92. * define the test in the base class {@link BaseTestBugzillaIssues}</b>
  93. */
  94. public final class TestBugs extends BaseTestBugzillaIssues {
  95. public TestBugs() {
  96. super(HSSFITestDataProvider.instance);
  97. }
  98. private static HSSFWorkbook openSample(String sampleFileName) {
  99. return HSSFITestDataProvider.instance.openSampleWorkbook(sampleFileName);
  100. }
  101. private static HSSFWorkbook writeOutAndReadBack(HSSFWorkbook original) {
  102. return HSSFITestDataProvider.instance.writeOutAndReadBack(original);
  103. }
  104. /**
  105. * Test reading AND writing a complicated workbook
  106. * Test opening resulting sheet in excel
  107. */
  108. @Test
  109. public void bug15228() throws Exception {
  110. HSSFWorkbook wb = openSample("15228.xls");
  111. HSSFSheet s = wb.getSheetAt(0);
  112. HSSFRow r = s.createRow(0);
  113. HSSFCell c = r.createCell(0);
  114. c.setCellValue(10);
  115. writeOutAndReadBack(wb).close();
  116. wb.close();
  117. }
  118. @Test
  119. public void bug13796() throws Exception {
  120. HSSFWorkbook wb = openSample("13796.xls");
  121. HSSFSheet s = wb.getSheetAt(0);
  122. HSSFRow r = s.createRow(0);
  123. HSSFCell c = r.createCell(0);
  124. c.setCellValue(10);
  125. writeOutAndReadBack(wb).close();
  126. wb.close();
  127. }
  128. /**
  129. * test reading of a formula with a name and a cell ref in one
  130. **/
  131. @Test
  132. public void bug14460() throws Exception {
  133. HSSFWorkbook wb = openSample("14460.xls");
  134. wb.getSheetAt(0);
  135. wb.close();
  136. }
  137. @Test
  138. public void bug14330() throws Exception {
  139. HSSFWorkbook wb1 = openSample("14330-1.xls");
  140. wb1.getSheetAt(0);
  141. wb1.close();
  142. HSSFWorkbook wb2 = openSample("14330-2.xls");
  143. wb2.getSheetAt(0);
  144. wb2.close();
  145. }
  146. private static void setCellText(HSSFCell cell, String text) {
  147. cell.setCellValue(new HSSFRichTextString(text));
  148. }
  149. /**
  150. * test rewriting a file with large number of unique strings
  151. * open resulting file in Excel to check results!
  152. */
  153. @Test
  154. public void bug15375() throws Exception {
  155. HSSFWorkbook wb = openSample("15375.xls");
  156. HSSFSheet sheet = wb.getSheetAt(0);
  157. HSSFRow row = sheet.getRow(5);
  158. HSSFCell cell = row.getCell(3);
  159. if (cell == null)
  160. cell = row.createCell(3);
  161. // Write test
  162. setCellText(cell, "a test");
  163. // change existing numeric cell value
  164. HSSFRow oRow = sheet.getRow(14);
  165. HSSFCell oCell = oRow.getCell(4);
  166. oCell.setCellValue(75);
  167. oCell = oRow.getCell(5);
  168. setCellText(oCell, "0.3");
  169. writeOutAndReadBack(wb).close();
  170. wb.close();
  171. }
  172. /**
  173. * test writing a file with large number of unique strings,
  174. * open resulting file in Excel to check results!
  175. */
  176. @Test
  177. public void bug15375_2() throws Exception {
  178. bug15375(6000);
  179. }
  180. /**
  181. * Double byte strings
  182. */
  183. @Test
  184. public void bug15556() throws Exception {
  185. HSSFWorkbook wb = openSample("15556.xls");
  186. HSSFSheet sheet = wb.getSheetAt(0);
  187. HSSFRow row = sheet.getRow(45);
  188. assertNotNull("Read row fine!", row);
  189. wb.close();
  190. }
  191. /**
  192. * Double byte strings
  193. */
  194. @Test
  195. public void bug22742() {
  196. openSample("22742.xls");
  197. }
  198. /**
  199. * Double byte strings
  200. */
  201. @Test
  202. public void bug12561_1() {
  203. openSample("12561-1.xls");
  204. }
  205. /**
  206. * Double byte strings
  207. */
  208. @Test
  209. public void bug12561_2() {
  210. openSample("12561-2.xls");
  211. }
  212. /**
  213. * Double byte strings
  214. * File supplied by jubeson
  215. */
  216. @Test
  217. public void bug12843_1() {
  218. openSample("12843-1.xls");
  219. }
  220. /**
  221. * Double byte strings
  222. * File supplied by Paul Chung
  223. */
  224. @Test
  225. public void bug12843_2() {
  226. openSample("12843-2.xls");
  227. }
  228. /**
  229. * Reference to Name
  230. */
  231. @Test
  232. public void bug13224() {
  233. openSample("13224.xls");
  234. }
  235. /**
  236. * Illegal argument exception - cannot store duplicate value in Map
  237. */
  238. @Test
  239. public void bug19599() {
  240. openSample("19599-1.xls");
  241. openSample("19599-2.xls");
  242. }
  243. @Test
  244. public void bug24215() throws Exception {
  245. HSSFWorkbook wb = openSample("24215.xls");
  246. for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
  247. HSSFSheet sheet = wb.getSheetAt(sheetIndex);
  248. int rows = sheet.getLastRowNum();
  249. for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
  250. HSSFRow row = sheet.getRow(rowIndex);
  251. int cells = row.getLastCellNum();
  252. for (int cellIndex = 0; cellIndex < cells; cellIndex++) {
  253. row.getCell(cellIndex);
  254. }
  255. }
  256. }
  257. wb.close();
  258. }
  259. /**
  260. * Tests read and write of Unicode strings in formula results
  261. * bug and testcase submitted by Sompop Kumnoonsate
  262. * The file contains THAI unicode characters.
  263. */
  264. @Test
  265. public void bugUnicodeStringFormulaRead() throws Exception {
  266. HSSFWorkbook w = openSample("25695.xls");
  267. HSSFCell a1 = w.getSheetAt(0).getRow(0).getCell(0);
  268. HSSFCell a2 = w.getSheetAt(0).getRow(0).getCell(1);
  269. HSSFCell b1 = w.getSheetAt(0).getRow(1).getCell(0);
  270. HSSFCell b2 = w.getSheetAt(0).getRow(1).getCell(1);
  271. HSSFCell c1 = w.getSheetAt(0).getRow(2).getCell(0);
  272. HSSFCell c2 = w.getSheetAt(0).getRow(2).getCell(1);
  273. HSSFCell d1 = w.getSheetAt(0).getRow(3).getCell(0);
  274. HSSFCell d2 = w.getSheetAt(0).getRow(3).getCell(1);
  275. /*
  276. // THAI code page
  277. System.out.println("a1="+unicodeString(a1));
  278. System.out.println("a2="+unicodeString(a2));
  279. // US code page
  280. System.out.println("b1="+unicodeString(b1));
  281. System.out.println("b2="+unicodeString(b2));
  282. // THAI+US
  283. System.out.println("c1="+unicodeString(c1));
  284. System.out.println("c2="+unicodeString(c2));
  285. // US+THAI
  286. System.out.println("d1="+unicodeString(d1));
  287. System.out.println("d2="+unicodeString(d2));
  288. */
  289. confirmSameCellText(a1, a2);
  290. confirmSameCellText(b1, b2);
  291. confirmSameCellText(c1, c2);
  292. confirmSameCellText(d1, d2);
  293. HSSFWorkbook rw = writeOutAndReadBack(w);
  294. w.close();
  295. HSSFCell ra1 = rw.getSheetAt(0).getRow(0).getCell(0);
  296. HSSFCell ra2 = rw.getSheetAt(0).getRow(0).getCell(1);
  297. HSSFCell rb1 = rw.getSheetAt(0).getRow(1).getCell(0);
  298. HSSFCell rb2 = rw.getSheetAt(0).getRow(1).getCell(1);
  299. HSSFCell rc1 = rw.getSheetAt(0).getRow(2).getCell(0);
  300. HSSFCell rc2 = rw.getSheetAt(0).getRow(2).getCell(1);
  301. HSSFCell rd1 = rw.getSheetAt(0).getRow(3).getCell(0);
  302. HSSFCell rd2 = rw.getSheetAt(0).getRow(3).getCell(1);
  303. confirmSameCellText(a1, ra1);
  304. confirmSameCellText(b1, rb1);
  305. confirmSameCellText(c1, rc1);
  306. confirmSameCellText(d1, rd1);
  307. confirmSameCellText(a1, ra2);
  308. confirmSameCellText(b1, rb2);
  309. confirmSameCellText(c1, rc2);
  310. confirmSameCellText(d1, rd2);
  311. rw.close();
  312. }
  313. private static void confirmSameCellText(HSSFCell a, HSSFCell b) {
  314. assertEquals(a.getRichStringCellValue().getString(), b.getRichStringCellValue().getString());
  315. }
  316. /**
  317. * Error in opening wb
  318. */
  319. @Test
  320. public void bug32822() {
  321. openSample("32822.xls");
  322. }
  323. /**
  324. * fail to read wb with chart
  325. */
  326. @Test
  327. public void bug15573() {
  328. openSample("15573.xls");
  329. }
  330. /**
  331. * names and macros
  332. */
  333. @Test
  334. public void bug27852() throws Exception {
  335. HSSFWorkbook wb = openSample("27852.xls");
  336. for (int i = 0; i < wb.getNumberOfNames(); i++) {
  337. HSSFName name = wb.getNameAt(i);
  338. name.getNameName();
  339. if (name.isFunctionName()) {
  340. continue;
  341. }
  342. name.getRefersToFormula();
  343. }
  344. wb.close();
  345. }
  346. @Test
  347. public void bug33082() {
  348. openSample("33082.xls");
  349. }
  350. @Test
  351. public void bug34775() {
  352. try {
  353. openSample("34775.xls");
  354. } catch (NullPointerException e) {
  355. fail("identified bug 34775");
  356. }
  357. }
  358. /**
  359. * Error when reading then writing ArrayValues in NameRecord's
  360. */
  361. @Test
  362. public void bug37630() throws Exception {
  363. HSSFWorkbook wb = openSample("37630.xls");
  364. writeOutAndReadBack(wb);
  365. wb.close();
  366. }
  367. /**
  368. * Bug 25183: org.apache.poi.hssf.usermodel.HSSFSheet.setPropertiesFromSheet
  369. */
  370. @Test
  371. public void bug25183() throws Exception {
  372. HSSFWorkbook wb = openSample("25183.xls");
  373. writeOutAndReadBack(wb);
  374. wb.close();
  375. }
  376. /**
  377. * Bug 26100: 128-character message in IF statement cell causes HSSFWorkbook open failure
  378. */
  379. @Test
  380. public void bug26100() throws Exception {
  381. HSSFWorkbook wb = openSample("26100.xls");
  382. writeOutAndReadBack(wb);
  383. wb.close();
  384. }
  385. /**
  386. * Bug 27933: Unable to use a template (xls) file containing a wmf graphic
  387. */
  388. @Test
  389. public void bug27933() throws Exception {
  390. HSSFWorkbook wb = openSample("27933.xls");
  391. writeOutAndReadBack(wb);
  392. wb.close();
  393. }
  394. /**
  395. * Bug 29206: NPE on HSSFSheet.getRow for blank rows
  396. */
  397. @Test
  398. public void bug29206() throws Exception {
  399. //the first check with blank workbook
  400. HSSFWorkbook wb = openSample("Simple.xls");
  401. HSSFSheet sheet = wb.createSheet();
  402. for (int i = 1; i < 400; i++) {
  403. HSSFRow row = sheet.getRow(i);
  404. if (row != null) {
  405. row.getCell(0);
  406. }
  407. }
  408. wb.close();
  409. }
  410. /**
  411. * Bug 29675: POI 2.5 final corrupts output when starting workbook has a graphic
  412. */
  413. @Test
  414. public void bug29675() throws Exception {
  415. HSSFWorkbook wb = openSample("29675.xls");
  416. writeOutAndReadBack(wb);
  417. wb.close();
  418. }
  419. /**
  420. * Bug 29942: Importing Excel files that have been created by Open Office on Linux
  421. */
  422. @Test
  423. public void bug29942() throws Exception {
  424. HSSFWorkbook wb = openSample("29942.xls");
  425. HSSFSheet sheet = wb.getSheetAt(0);
  426. int count = 0;
  427. for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
  428. HSSFRow row = sheet.getRow(i);
  429. if (row != null) {
  430. HSSFCell cell = row.getCell(0);
  431. assertEquals(CellType.STRING, cell.getCellType());
  432. count++;
  433. }
  434. }
  435. assertEquals(85, count); //should read 85 rows
  436. writeOutAndReadBack(wb).close();
  437. wb.close();
  438. }
  439. /**
  440. * Bug 29982: Unable to read spreadsheet when dropdown list cell is selected -
  441. * Unable to construct record instance
  442. */
  443. @Test
  444. public void bug29982() throws Exception {
  445. HSSFWorkbook wb = openSample("29982.xls");
  446. writeOutAndReadBack(wb).close();
  447. wb.close();
  448. }
  449. /**
  450. * Bug 30540: HSSFSheet.setRowBreak throws NullPointerException
  451. */
  452. @Test
  453. public void bug30540() throws Exception {
  454. HSSFWorkbook wb = openSample("30540.xls");
  455. HSSFSheet s = wb.getSheetAt(0);
  456. s.setRowBreak(1);
  457. writeOutAndReadBack(wb).close();
  458. wb.close();
  459. }
  460. /**
  461. * Bug 31749: {Need help urgently}[This is critical] workbook.write() corrupts the file......?
  462. */
  463. @Test
  464. public void bug31749() throws Exception {
  465. HSSFWorkbook wb = openSample("31749.xls");
  466. writeOutAndReadBack(wb).close();
  467. wb.close();
  468. }
  469. /**
  470. * Bug 31979: {urgent help needed .....}poi library does not support form objects properly.
  471. */
  472. @Test
  473. public void bug31979() throws Exception {
  474. HSSFWorkbook wb = openSample("31979.xls");
  475. writeOutAndReadBack(wb).close();
  476. wb.close();
  477. }
  478. /**
  479. * Bug 35564: HSSFCell.java: NullPtrExc in isGridsPrinted() and getProtect()
  480. * when HSSFWorkbook is created from file
  481. */
  482. @Test
  483. public void bug35564() throws Exception {
  484. HSSFWorkbook wb = openSample("35564.xls");
  485. HSSFSheet sheet = wb.getSheetAt(0);
  486. assertFalse(sheet.isGridsPrinted());
  487. assertFalse(sheet.getProtect());
  488. writeOutAndReadBack(wb).close();
  489. wb.close();
  490. }
  491. /**
  492. * Bug 35565: HSSFCell.java: NullPtrExc in getColumnBreaks() when HSSFWorkbook is created from file
  493. */
  494. @Test
  495. public void bug35565() throws Exception {
  496. HSSFWorkbook wb = openSample("35565.xls");
  497. HSSFSheet sheet = wb.getSheetAt(0);
  498. assertNotNull(sheet);
  499. writeOutAndReadBack(wb).close();
  500. wb.close();
  501. }
  502. /**
  503. * Bug 37376: Cannot open the saved Excel file if checkbox controls exceed certain limit
  504. */
  505. @Test
  506. public void bug37376() throws Exception {
  507. HSSFWorkbook wb = openSample("37376.xls");
  508. writeOutAndReadBack(wb).close();
  509. wb.close();
  510. }
  511. /**
  512. * Bug 40285: CellIterator Skips First Column
  513. */
  514. @Test
  515. public void bug40285() throws Exception {
  516. HSSFWorkbook wb = openSample("40285.xls");
  517. HSSFSheet sheet = wb.getSheetAt(0);
  518. int rownum = 0;
  519. for (Iterator<Row> it = sheet.rowIterator(); it.hasNext(); rownum++) {
  520. Row row = it.next();
  521. assertEquals(rownum, row.getRowNum());
  522. int cellNum = 0;
  523. for (Iterator<Cell> it2 = row.cellIterator(); it2.hasNext(); cellNum++) {
  524. Cell cell = it2.next();
  525. assertEquals(cellNum, cell.getColumnIndex());
  526. }
  527. }
  528. wb.close();
  529. }
  530. /**
  531. * Test bug 38266: NPE when adding a row break
  532. * <p>
  533. * User's diagnosis:
  534. * 1. Manually (i.e., not using POI) create an Excel Workbook, making sure it
  535. * contains a sheet that doesn't have any row breaks
  536. * 2. Using POI, create a new HSSFWorkbook from the template in step #1
  537. * 3. Try adding a row break (via sheet.setRowBreak()) to the sheet mentioned in step #1
  538. * 4. Get a NullPointerException
  539. */
  540. @Test
  541. public void bug38266() throws Exception {
  542. String[] files = {"Simple.xls", "SimpleMultiCell.xls", "duprich1.xls"};
  543. for (String file : files) {
  544. HSSFWorkbook wb = openSample(file);
  545. HSSFSheet sheet = wb.getSheetAt(0);
  546. int[] breaks = sheet.getRowBreaks();
  547. assertEquals(0, breaks.length);
  548. //add 3 row breaks
  549. for (int j = 1; j <= 3; j++) {
  550. sheet.setRowBreak(j * 20);
  551. }
  552. wb.close();
  553. }
  554. }
  555. @Test
  556. public void bug40738() throws Exception {
  557. HSSFWorkbook wb = openSample("SimpleWithAutofilter.xls");
  558. writeOutAndReadBack(wb).close();
  559. wb.close();
  560. }
  561. /**
  562. * Bug 44200: Sheet not cloneable when Note added to excel cell
  563. */
  564. @Test
  565. public void bug44200() throws Exception {
  566. HSSFWorkbook wb = openSample("44200.xls");
  567. wb.cloneSheet(0);
  568. writeOutAndReadBack(wb).close();
  569. wb.close();
  570. }
  571. /**
  572. * Bug 44201: Sheet not cloneable when validation added to excel cell
  573. */
  574. @Test
  575. public void bug44201() throws Exception {
  576. HSSFWorkbook wb = openSample("44201.xls");
  577. writeOutAndReadBack(wb).close();
  578. wb.close();
  579. }
  580. /**
  581. * Bug 37684 : Unhandled Continue Record Error
  582. */
  583. @Test
  584. public void bug37684() throws Exception {
  585. HSSFWorkbook wb1 = openSample("37684-1.xls");
  586. writeOutAndReadBack(wb1).close();
  587. wb1.close();
  588. HSSFWorkbook wb2 = openSample("37684-2.xls");
  589. writeOutAndReadBack(wb2).close();
  590. wb2.close();
  591. }
  592. /**
  593. * Bug 41139: Constructing HSSFWorkbook is failed,threw threw ArrayIndexOutOfBoundsException for creating UnknownRecord
  594. */
  595. @Test
  596. public void bug41139() throws Exception {
  597. HSSFWorkbook wb = openSample("41139.xls");
  598. writeOutAndReadBack(wb).close();
  599. wb.close();
  600. }
  601. /**
  602. * Bug 41546: Constructing HSSFWorkbook is failed,
  603. * Unknown Ptg in Formula: 0x1a (26)
  604. */
  605. @Test
  606. public void bug41546() throws Exception {
  607. HSSFWorkbook wb1 = openSample("41546.xls");
  608. assertEquals(1, wb1.getNumberOfSheets());
  609. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  610. assertEquals(1, wb2.getNumberOfSheets());
  611. wb2.close();
  612. wb1.close();
  613. }
  614. /**
  615. * Bug 42564: Some files from Access were giving a RecordFormatException
  616. * when reading the BOFRecord
  617. */
  618. @Test
  619. public void bug42564() throws Exception {
  620. HSSFWorkbook wb = openSample("ex42564-21435.xls");
  621. writeOutAndReadBack(wb).close();
  622. wb.close();
  623. }
  624. /**
  625. * Bug 42564: Some files from Access also have issues
  626. * with the NameRecord, once you get past the BOFRecord
  627. * issue.
  628. */
  629. @Test
  630. public void bug42564Alt() throws Exception {
  631. HSSFWorkbook wb = openSample("ex42564-21503.xls");
  632. writeOutAndReadBack(wb).close();
  633. wb.close();
  634. }
  635. /**
  636. * Bug 42618: RecordFormatException reading a file containing
  637. * =CHOOSE(2,A2,A3,A4)
  638. */
  639. @Test
  640. public void bug42618() throws Exception {
  641. HSSFWorkbook wb1 = openSample("SimpleWithChoose.xls");
  642. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  643. wb1.close();
  644. // Check we detect the string properly too
  645. HSSFSheet s = wb2.getSheetAt(0);
  646. // Textual value
  647. HSSFRow r1 = s.getRow(0);
  648. HSSFCell c1 = r1.getCell(1);
  649. assertEquals("=CHOOSE(2,A2,A3,A4)", c1.getRichStringCellValue().toString());
  650. // Formula Value
  651. HSSFRow r2 = s.getRow(1);
  652. HSSFCell c2 = r2.getCell(1);
  653. assertEquals(25, (int) c2.getNumericCellValue());
  654. try {
  655. assertEquals("CHOOSE(2,A2,A3,A4)", c2.getCellFormula());
  656. } catch (IllegalStateException e) {
  657. if (e.getMessage().startsWith("Too few arguments")
  658. && e.getMessage().indexOf("ConcatPtg") > 0) {
  659. fail("identified bug 44306");
  660. }
  661. }
  662. wb2.close();
  663. }
  664. /**
  665. * Something up with the FileSharingRecord
  666. */
  667. @Test
  668. public void bug43251() throws Exception {
  669. // Used to blow up with an IllegalArgumentException
  670. // when creating a FileSharingRecord
  671. HSSFWorkbook wb = openSample("43251.xls");
  672. assertEquals(1, wb.getNumberOfSheets());
  673. wb.close();
  674. }
  675. /**
  676. * Crystal reports generates files with short
  677. * StyleRecords, which is against the spec
  678. */
  679. @Test
  680. public void bug44471() throws Exception {
  681. // Used to blow up with an ArrayIndexOutOfBounds
  682. // when creating a StyleRecord
  683. HSSFWorkbook wb = openSample("OddStyleRecord.xls");
  684. assertEquals(1, wb.getNumberOfSheets());
  685. wb.close();
  686. }
  687. /**
  688. * Files with "read only recommended" were giving
  689. * grief on the FileSharingRecord
  690. */
  691. @Test
  692. public void bug44536() throws Exception {
  693. // Used to blow up with an IllegalArgumentException
  694. // when creating a FileSharingRecord
  695. HSSFWorkbook wb1 = openSample("ReadOnlyRecommended.xls");
  696. // Check read only advised
  697. assertEquals(3, wb1.getNumberOfSheets());
  698. assertTrue(wb1.isWriteProtected());
  699. wb1.close();
  700. // But also check that another wb isn't
  701. HSSFWorkbook wb2 = openSample("SimpleWithChoose.xls");
  702. assertFalse(wb2.isWriteProtected());
  703. wb2.close();
  704. }
  705. /**
  706. * Some files were having problems with the DVRecord,
  707. * probably due to dropdowns
  708. */
  709. @Test
  710. public void bug44593() throws Exception {
  711. // Used to blow up with an IllegalArgumentException
  712. // when creating a DVRecord
  713. // Now won't, but no idea if this means we have
  714. // rubbish in the DVRecord or not...
  715. HSSFWorkbook wb = openSample("44593.xls");
  716. assertEquals(2, wb.getNumberOfSheets());
  717. wb.close();
  718. }
  719. /**
  720. * Used to give problems due to trying to read a zero
  721. * length string, but that's now properly handled
  722. */
  723. @Test
  724. public void bug44643() throws Exception {
  725. // Used to blow up with an IllegalArgumentException
  726. HSSFWorkbook wb = openSample("44643.xls");
  727. assertEquals(1, wb.getNumberOfSheets());
  728. wb.close();
  729. }
  730. /**
  731. * User reported the wrong number of rows from the
  732. * iterator, but we can't replicate that
  733. */
  734. @Test
  735. public void bug44693() throws Exception {
  736. HSSFWorkbook wb = openSample("44693.xls");
  737. HSSFSheet s = wb.getSheetAt(0);
  738. // Rows are 1 to 713
  739. assertEquals(0, s.getFirstRowNum());
  740. assertEquals(712, s.getLastRowNum());
  741. assertEquals(713, s.getPhysicalNumberOfRows());
  742. // Now check the iterator
  743. int rowsSeen = 0;
  744. for (Iterator<Row> i = s.rowIterator(); i.hasNext(); ) {
  745. Row r = i.next();
  746. assertNotNull(r);
  747. rowsSeen++;
  748. }
  749. assertEquals(713, rowsSeen);
  750. wb.close();
  751. }
  752. /**
  753. * Bug 28774: Excel will crash when opening xls-files with images.
  754. */
  755. @Test
  756. public void bug28774() throws Exception {
  757. HSSFWorkbook wb = openSample("28774.xls");
  758. assertTrue("no errors reading sample xls", true);
  759. writeOutAndReadBack(wb).close();
  760. assertTrue("no errors writing sample xls", true);
  761. wb.close();
  762. }
  763. /**
  764. * Had a problem apparently, not sure what as it
  765. * works just fine...
  766. */
  767. @Test
  768. public void bug44891() throws Exception {
  769. HSSFWorkbook wb = openSample("44891.xls");
  770. assertTrue("no errors reading sample xls", true);
  771. writeOutAndReadBack(wb).close();
  772. assertTrue("no errors writing sample xls", true);
  773. wb.close();
  774. }
  775. /**
  776. * Bug 44235: Ms Excel can't open save as excel file
  777. * <p>
  778. * Works fine with poi-3.1-beta1.
  779. */
  780. @Test
  781. public void bug44235() throws Exception {
  782. HSSFWorkbook wb = openSample("44235.xls");
  783. assertTrue("no errors reading sample xls", true);
  784. writeOutAndReadBack(wb).close();
  785. assertTrue("no errors writing sample xls", true);
  786. wb.close();
  787. }
  788. @Test
  789. public void bug36947() throws Exception {
  790. HSSFWorkbook wb = openSample("36947.xls");
  791. assertTrue("no errors reading sample xls", true);
  792. writeOutAndReadBack(wb).close();
  793. assertTrue("no errors writing sample xls", true);
  794. wb.close();
  795. }
  796. @Test
  797. public void bug39634() throws Exception {
  798. HSSFWorkbook wb = openSample("39634.xls");
  799. assertTrue("no errors reading sample xls", true);
  800. writeOutAndReadBack(wb).close();
  801. assertTrue("no errors writing sample xls", true);
  802. wb.close();
  803. }
  804. /**
  805. * Problems with extracting check boxes from
  806. * HSSFObjectData
  807. */
  808. @Test(expected = IllegalArgumentException.class)
  809. public void bug44840() throws Exception {
  810. HSSFWorkbook wb = openSample("WithCheckBoxes.xls");
  811. // Take a look at the embedded objects
  812. List<HSSFObjectData> objects = wb.getAllEmbeddedObjects();
  813. assertEquals(1, objects.size());
  814. HSSFObjectData obj = objects.get(0);
  815. assertNotNull(obj);
  816. // Peek inside the underlying record
  817. EmbeddedObjectRefSubRecord rec = obj.findObjectRecord();
  818. assertNotNull(rec);
  819. // assertEquals(32, rec.field_1_stream_id_offset);
  820. assertEquals(0, rec.getStreamId().intValue()); // WRONG!
  821. assertEquals("Forms.CheckBox.1", rec.getOLEClassName());
  822. assertEquals(12, rec.getObjectData().length);
  823. // Doesn't have a directory
  824. assertFalse(obj.hasDirectoryEntry());
  825. assertNotNull(obj.getObjectData());
  826. assertEquals(12, obj.getObjectData().length);
  827. assertEquals("Forms.CheckBox.1", obj.getOLE2ClassName());
  828. try {
  829. obj.getDirectory();
  830. } finally {
  831. wb.close();
  832. }
  833. }
  834. /**
  835. * Test that we can delete sheets without
  836. * breaking the build in named ranges
  837. * used for printing stuff.
  838. */
  839. @Test
  840. public void bug30978() throws Exception {
  841. HSSFWorkbook wb1 = openSample("30978-alt.xls");
  842. assertEquals(1, wb1.getNumberOfNames());
  843. assertEquals(3, wb1.getNumberOfSheets());
  844. // Check all names fit within range, and use
  845. // DeletedArea3DPtg
  846. InternalWorkbook w = wb1.getWorkbook();
  847. assertNames(wb1, w);
  848. // Delete the 2nd sheet
  849. wb1.removeSheetAt(1);
  850. // Re-check
  851. assertEquals(1, wb1.getNumberOfNames());
  852. assertEquals(2, wb1.getNumberOfSheets());
  853. assertNames(wb1, w);
  854. // Save and re-load
  855. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  856. wb1.close();
  857. w = wb2.getWorkbook();
  858. assertEquals(1, wb2.getNumberOfNames());
  859. assertEquals(2, wb2.getNumberOfSheets());
  860. assertNames(wb2, w);
  861. wb2.close();
  862. }
  863. private void assertNames(HSSFWorkbook wb1, InternalWorkbook w) {
  864. for (int i = 0; i < w.getNumNames(); i++) {
  865. NameRecord r = w.getNameRecord(i);
  866. assertTrue(r.getSheetNumber() <= wb1.getNumberOfSheets());
  867. Ptg[] nd = r.getNameDefinition();
  868. assertEquals(1, nd.length);
  869. assertTrue(nd[0] instanceof DeletedArea3DPtg);
  870. }
  871. }
  872. /**
  873. * Test that fonts get added properly
  874. */
  875. @Test
  876. public void bug45338() throws IOException {
  877. HSSFWorkbook wb = new HSSFWorkbook();
  878. assertEquals(4, wb.getNumberOfFontsAsInt());
  879. HSSFSheet s = wb.createSheet();
  880. s.createRow(0);
  881. s.createRow(1);
  882. s.getRow(0).createCell(0);
  883. s.getRow(1).createCell(0);
  884. assertEquals(4, wb.getNumberOfFontsAsInt());
  885. HSSFFont f1 = wb.getFontAt(0);
  886. assertFalse(f1.getBold());
  887. // Check that asking for the same font
  888. // multiple times gives you the same thing.
  889. // Otherwise, our tests wouldn't work!
  890. assertSame(wb.getFontAt(0), wb.getFontAt(0));
  891. assertEquals(
  892. wb.getFontAt(0),
  893. wb.getFontAt(0)
  894. );
  895. assertEquals(
  896. wb.getFontAt(2),
  897. wb.getFontAt(2)
  898. );
  899. assertNotSame(wb.getFontAt(0), wb.getFontAt(2));
  900. // Look for a new font we have
  901. // yet to add
  902. assertNull(
  903. wb.findFont(
  904. false, (short) 123, (short) 22,
  905. "Thingy", false, true, (short) 2, (byte) 2
  906. )
  907. );
  908. HSSFFont nf = wb.createFont();
  909. assertEquals(5, wb.getNumberOfFontsAsInt());
  910. assertEquals(5, nf.getIndex());
  911. assertEquals(nf, wb.getFontAt(5));
  912. nf.setBold(false);
  913. nf.setColor((short) 123);
  914. nf.setFontHeight((short) 22);
  915. nf.setFontName("Thingy");
  916. nf.setItalic(false);
  917. nf.setStrikeout(true);
  918. nf.setTypeOffset((short) 2);
  919. nf.setUnderline((byte) 2);
  920. assertEquals(5, wb.getNumberOfFontsAsInt());
  921. assertEquals(nf, wb.getFontAt(5));
  922. // Find it now
  923. assertNotNull(
  924. wb.findFont(
  925. false, (short) 123, (short) 22,
  926. "Thingy", false, true, (short) 2, (byte) 2
  927. )
  928. );
  929. HSSFFont font = wb.findFont(
  930. false, (short) 123, (short) 22,
  931. "Thingy", false, true, (short) 2, (byte) 2
  932. );
  933. assertNotNull(font);
  934. assertEquals(
  935. 5,
  936. font.getIndex()
  937. );
  938. assertEquals(nf,
  939. wb.findFont(
  940. false, (short) 123, (short) 22,
  941. "Thingy", false, true, (short) 2, (byte) 2
  942. )
  943. );
  944. wb.close();
  945. }
  946. /**
  947. * From the mailing list - ensure we can handle a formula
  948. * containing a zip code, eg ="70164"
  949. */
  950. @Test
  951. public void bugZipCodeFormulas() throws Exception {
  952. HSSFWorkbook wb1 = new HSSFWorkbook();
  953. HSSFSheet s = wb1.createSheet();
  954. s.createRow(0);
  955. HSSFCell c1 = s.getRow(0).createCell(0);
  956. HSSFCell c2 = s.getRow(0).createCell(1);
  957. HSSFCell c3 = s.getRow(0).createCell(2);
  958. // As number and string
  959. c1.setCellFormula("70164");
  960. c2.setCellFormula("\"70164\"");
  961. c3.setCellFormula("\"90210\"");
  962. // Check the formulas
  963. assertEquals("70164", c1.getCellFormula());
  964. assertEquals("\"70164\"", c2.getCellFormula());
  965. // And check the values - blank
  966. confirmCachedValue(0.0, c1);
  967. confirmCachedValue(0.0, c2);
  968. confirmCachedValue(0.0, c3);
  969. // Try changing the cached value on one of the string
  970. // formula cells, so we can see it updates properly
  971. c3.setCellValue(new HSSFRichTextString("test"));
  972. confirmCachedValue("test", c3);
  973. try {
  974. c3.getNumericCellValue();
  975. fail("exception should have been thrown");
  976. } catch (IllegalStateException e) {
  977. assertEquals("Cannot get a NUMERIC value from a STRING formula cell", e.getMessage());
  978. }
  979. // Now evaluate, they should all be changed
  980. HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb1);
  981. eval.evaluateFormulaCell(c1);
  982. eval.evaluateFormulaCell(c2);
  983. eval.evaluateFormulaCell(c3);
  984. // Check that the cells now contain
  985. // the correct values
  986. confirmCachedValue(70164.0, c1);
  987. confirmCachedValue("70164", c2);
  988. confirmCachedValue("90210", c3);
  989. // Write and read
  990. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  991. wb1.close();
  992. HSSFSheet ns = wb2.getSheetAt(0);
  993. HSSFCell nc1 = ns.getRow(0).getCell(0);
  994. HSSFCell nc2 = ns.getRow(0).getCell(1);
  995. HSSFCell nc3 = ns.getRow(0).getCell(2);
  996. // Re-check
  997. confirmCachedValue(70164.0, nc1);
  998. confirmCachedValue("70164", nc2);
  999. confirmCachedValue("90210", nc3);
  1000. int i = 0;
  1001. for (Iterator<CellValueRecordInterface> it = ns.getSheet().getCellValueIterator(); it.hasNext(); i++) {
  1002. CellValueRecordInterface cvr = it.next();
  1003. if (cvr instanceof FormulaRecordAggregate) {
  1004. FormulaRecordAggregate fr = (FormulaRecordAggregate) cvr;
  1005. if (i == 0) {
  1006. assertEquals(70164.0, fr.getFormulaRecord().getValue(), 0.0001);
  1007. assertNull(fr.getStringRecord());
  1008. } else if (i == 1) {
  1009. assertEquals(0.0, fr.getFormulaRecord().getValue(), 0.0001);
  1010. assertNotNull(fr.getStringRecord());
  1011. assertEquals("70164", fr.getStringRecord().getString());
  1012. } else {
  1013. assertEquals(0.0, fr.getFormulaRecord().getValue(), 0.0001);
  1014. assertNotNull(fr.getStringRecord());
  1015. assertEquals("90210", fr.getStringRecord().getString());
  1016. }
  1017. }
  1018. }
  1019. assertEquals(3, i);
  1020. wb2.close();
  1021. }
  1022. private static void confirmCachedValue(double expectedValue, HSSFCell cell) {
  1023. assertEquals(CellType.FORMULA, cell.getCellType());
  1024. assertEquals(CellType.NUMERIC, cell.getCachedFormulaResultType());
  1025. assertEquals(expectedValue, cell.getNumericCellValue(), 0.0);
  1026. }
  1027. private static void confirmCachedValue(String expectedValue, HSSFCell cell) {
  1028. assertEquals(CellType.FORMULA, cell.getCellType());
  1029. assertEquals(CellType.STRING, cell.getCachedFormulaResultType());
  1030. assertEquals(expectedValue, cell.getRichStringCellValue().getString());
  1031. }
  1032. /**
  1033. * Problem with "Vector Rows", eg a whole
  1034. * column which is set to the result of
  1035. * {=sin(B1:B9)}(9,1), so that each cell is
  1036. * shown to have the contents
  1037. * {=sin(B1:B9){9,1)[rownum][0]
  1038. * In this sample file, the vector column
  1039. * is C, and the data column is B.
  1040. * <p>
  1041. * Expected ExpPtg to be converted from Shared to Non-Shared...
  1042. */
  1043. @Ignore("For now, blows up with an exception from ExtPtg")
  1044. @Test
  1045. public void test43623() throws Exception {
  1046. HSSFWorkbook wb1 = openSample("43623.xls");
  1047. assertEquals(1, wb1.getNumberOfSheets());
  1048. HSSFSheet s1 = wb1.getSheetAt(0);
  1049. HSSFCell c1 = s1.getRow(0).getCell(2);
  1050. HSSFCell c2 = s1.getRow(1).getCell(2);
  1051. HSSFCell c3 = s1.getRow(2).getCell(2);
  1052. // These formula contents are a guess...
  1053. assertEquals("{=sin(B1:B9){9,1)[0][0]", c1.getCellFormula());
  1054. assertEquals("{=sin(B1:B9){9,1)[1][0]", c2.getCellFormula());
  1055. assertEquals("{=sin(B1:B9){9,1)[2][0]", c3.getCellFormula());
  1056. // Save and re-open, ensure it still works
  1057. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  1058. wb1.close();
  1059. HSSFSheet ns1 = wb2.getSheetAt(0);
  1060. HSSFCell nc1 = ns1.getRow(0).getCell(2);
  1061. HSSFCell nc2 = ns1.getRow(1).getCell(2);
  1062. HSSFCell nc3 = ns1.getRow(2).getCell(2);
  1063. assertEquals("{=sin(B1:B9){9,1)[0][0]", nc1.getCellFormula());
  1064. assertEquals("{=sin(B1:B9){9,1)[1][0]", nc2.getCellFormula());
  1065. assertEquals("{=sin(B1:B9){9,1)[2][0]", nc3.getCellFormula());
  1066. wb2.close();
  1067. }
  1068. /**
  1069. * People are all getting confused about the last
  1070. * row and cell number
  1071. */
  1072. @Test
  1073. public void bug30635() throws IOException {
  1074. HSSFWorkbook wb = new HSSFWorkbook();
  1075. HSSFSheet s = wb.createSheet();
  1076. // No rows, first/last return -1
  1077. assertEquals(-1, s.getFirstRowNum());
  1078. assertEquals(-1, s.getLastRowNum());
  1079. assertEquals(0, s.getPhysicalNumberOfRows());
  1080. // One row, most things are 0, physical is 1
  1081. s.createRow(0);
  1082. assertEquals(0, s.getFirstRowNum());
  1083. assertEquals(0, s.getLastRowNum());
  1084. assertEquals(1, s.getPhysicalNumberOfRows());
  1085. // And another, things change
  1086. s.createRow(4);
  1087. assertEquals(0, s.getFirstRowNum());
  1088. assertEquals(4, s.getLastRowNum());
  1089. assertEquals(2, s.getPhysicalNumberOfRows());
  1090. // Now start on cells
  1091. HSSFRow r = s.getRow(0);
  1092. assertEquals(-1, r.getFirstCellNum());
  1093. assertEquals(-1, r.getLastCellNum());
  1094. assertEquals(0, r.getPhysicalNumberOfCells());
  1095. // Add a cell, things move off -1
  1096. r.createCell(0);
  1097. assertEquals(0, r.getFirstCellNum());
  1098. assertEquals(1, r.getLastCellNum()); // last cell # + 1
  1099. assertEquals(1, r.getPhysicalNumberOfCells());
  1100. r.createCell(1);
  1101. assertEquals(0, r.getFirstCellNum());
  1102. assertEquals(2, r.getLastCellNum()); // last cell # + 1
  1103. assertEquals(2, r.getPhysicalNumberOfCells());
  1104. r.createCell(4);
  1105. assertEquals(0, r.getFirstCellNum());
  1106. assertEquals(5, r.getLastCellNum()); // last cell # + 1
  1107. assertEquals(3, r.getPhysicalNumberOfCells());
  1108. wb.close();
  1109. }
  1110. /**
  1111. * Data Tables - ptg 0x2
  1112. */
  1113. @Test
  1114. public void bug44958() throws Exception {
  1115. HSSFWorkbook wb = openSample("44958.xls");
  1116. HSSFSheet s;
  1117. HSSFRow r;
  1118. HSSFCell c;
  1119. // Check the contents of the formulas
  1120. // E4 to G9 of sheet 4 make up the table
  1121. s = wb.getSheet("OneVariable Table Completed");
  1122. r = s.getRow(3);
  1123. c = r.getCell(4);
  1124. assertEquals(CellType.FORMULA, c.getCellType());
  1125. // TODO - check the formula once tables and
  1126. // arrays are properly supported
  1127. // E4 to H9 of sheet 5 make up the table
  1128. s = wb.getSheet("TwoVariable Table Example");
  1129. r = s.getRow(3);
  1130. c = r.getCell(4);
  1131. assertEquals(CellType.FORMULA, c.getCellType());
  1132. // TODO - check the formula once tables and
  1133. // arrays are properly supported
  1134. wb.close();
  1135. }
  1136. /**
  1137. * 45322: HSSFSheet.autoSizeColumn fails when style.getDataFormat() returns -1
  1138. */
  1139. @Test
  1140. public void bug45322() throws Exception {
  1141. HSSFWorkbook wb = openSample("44958.xls");
  1142. HSSFSheet sh = wb.getSheetAt(0);
  1143. for (short i = 0; i < 30; i++) sh.autoSizeColumn(i);
  1144. wb.close();
  1145. }
  1146. /**
  1147. * We used to add too many UncalcRecords to sheets
  1148. * with diagrams on. Don't any more
  1149. */
  1150. @Test
  1151. public void bug45414() throws Exception {
  1152. HSSFWorkbook wb1 = openSample("WithThreeCharts.xls");
  1153. wb1.getSheetAt(0).setForceFormulaRecalculation(true);
  1154. wb1.getSheetAt(1).setForceFormulaRecalculation(false);
  1155. wb1.getSheetAt(2).setForceFormulaRecalculation(true);
  1156. // Write out and back in again
  1157. // This used to break
  1158. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  1159. wb1.close();
  1160. // Check now set as it should be
  1161. assertTrue(wb2.getSheetAt(0).getForceFormulaRecalculation());
  1162. assertFalse(wb2.getSheetAt(1).getForceFormulaRecalculation());
  1163. assertTrue(wb2.getSheetAt(2).getForceFormulaRecalculation());
  1164. wb2.close();
  1165. }
  1166. /**
  1167. * Very hidden sheets not displaying as such
  1168. */
  1169. @Test
  1170. public void bug45761() throws Exception {
  1171. HSSFWorkbook wb1 = openSample("45761.xls");
  1172. assertEquals(3, wb1.getNumberOfSheets());
  1173. assertFalse(wb1.isSheetHidden(0));
  1174. assertFalse(wb1.isSheetVeryHidden(0));
  1175. assertTrue(wb1.isSheetHidden(1));
  1176. assertFalse(wb1.isSheetVeryHidden(1));
  1177. assertFalse(wb1.isSheetHidden(2));
  1178. assertTrue(wb1.isSheetVeryHidden(2));
  1179. // Change sheet 0 to be very hidden, and re-load
  1180. wb1.setSheetVisibility(0, SheetVisibility.VERY_HIDDEN);
  1181. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  1182. wb1.close();
  1183. assertFalse(wb2.isSheetHidden(0));
  1184. assertTrue(wb2.isSheetVeryHidden(0));
  1185. assertTrue(wb2.isSheetHidden(1));
  1186. assertFalse(wb2.isSheetVeryHidden(1));
  1187. assertFalse(wb2.isSheetHidden(2));
  1188. assertTrue(wb2.isSheetVeryHidden(2));
  1189. wb2.close();
  1190. }
  1191. /**
  1192. * The resolution for bug 45777 assumed that the maximum text length in a header / footer
  1193. * record was 256 bytes. This assumption appears to be wrong. Since the fix for bug 47244,
  1194. * POI now supports header / footer text lengths beyond 256 bytes.
  1195. */
  1196. @Test
  1197. public void bug45777() throws Exception {
  1198. HSSFWorkbook wb = new HSSFWorkbook();
  1199. HSSFSheet s = wb.createSheet();
  1200. char[] cc248 = new char[248];
  1201. Arrays.fill(cc248, 'x');
  1202. String s248 = new String(cc248);
  1203. String s249 = s248 + "1";
  1204. String s250 = s248 + "12";
  1205. String s251 = s248 + "123";
  1206. assertEquals(248, s248.length());
  1207. assertEquals(249, s249.length());
  1208. assertEquals(250, s250.length());
  1209. assertEquals(251, s251.length());
  1210. // Try on headers
  1211. s.getHeader().setCenter(s248);
  1212. assertEquals(254, s.getHeader().getRawText().length());
  1213. writeOutAndReadBack(wb).close();
  1214. s.getHeader().setCenter(s251);
  1215. assertEquals(257, s.getHeader().getRawText().length());
  1216. writeOutAndReadBack(wb).close();
  1217. // header can be more than 256 bytes
  1218. s.getHeader().setCenter(s250); // 256 bytes required
  1219. s.getHeader().setCenter(s251); // 257 bytes required
  1220. // Now try on footers
  1221. s.getFooter().setCenter(s248);
  1222. assertEquals(254, s.getFooter().getRawText().length());
  1223. writeOutAndReadBack(wb).close();
  1224. s.getFooter().setCenter(s251);
  1225. assertEquals(257, s.getFooter().getRawText().length());
  1226. writeOutAndReadBack(wb).close();
  1227. // footer can be more than 256 bytes
  1228. s.getFooter().setCenter(s250); // 256 bytes required
  1229. s.getFooter().setCenter(s251); // 257 bytes required
  1230. wb.close();
  1231. }
  1232. /**
  1233. * Charts with long titles
  1234. */
  1235. @Test
  1236. public void bug45784() throws Exception {
  1237. // This used to break
  1238. HSSFWorkbook wb = openSample("45784.xls");
  1239. assertEquals(1, wb.getNumberOfSheets());
  1240. writeOutAndReadBack(wb).close();
  1241. wb.close();
  1242. }
  1243. /**
  1244. * Cell background colours
  1245. */
  1246. @Test
  1247. public void bug45492() throws Exception {
  1248. HSSFWorkbook wb = openSample("45492.xls");
  1249. HSSFSheet s = wb.getSheetAt(0);
  1250. HSSFRow r = s.getRow(0);
  1251. HSSFPalette p = wb.getCustomPalette();
  1252. HSSFCell auto = r.getCell(0);
  1253. HSSFCell grey = r.getCell(1);
  1254. HSSFCell red = r.getCell(2);
  1255. HSSFCell blue = r.getCell(3);
  1256. HSSFCell green = r.getCell(4);
  1257. assertEquals(64, auto.getCellStyle().getFillForegroundColor());
  1258. assertEquals(64, auto.getCellStyle().getFillBackgroundColor());
  1259. assertEquals("0:0:0", p.getColor(64).getHexString());
  1260. assertEquals(22, grey.getCellStyle().getFillForegroundColor());
  1261. assertEquals(64, grey.getCellStyle().getFillBackgroundColor());
  1262. assertEquals("C0C0:C0C0:C0C0", p.getColor(22).getHexString());
  1263. assertEquals(10, red.getCellStyle().getFillForegroundColor());
  1264. assertEquals(64, red.getCellStyle().getFillBackgroundColor());
  1265. assertEquals("FFFF:0:0", p.getColor(10).getHexString());
  1266. assertEquals(12, blue.getCellStyle().getFillForegroundColor());
  1267. assertEquals(64, blue.getCellStyle().getFillBackgroundColor());
  1268. assertEquals("0:0:FFFF", p.getColor(12).getHexString());
  1269. assertEquals(11, green.getCellStyle().getFillForegroundColor());
  1270. assertEquals(64, green.getCellStyle().getFillBackgroundColor());
  1271. assertEquals("0:FFFF:0", p.getColor(11).getHexString());
  1272. wb.close();
  1273. }
  1274. /**
  1275. * ContinueRecord after EOF
  1276. */
  1277. @Test
  1278. public void bug46137() throws Exception {
  1279. // This used to break
  1280. HSSFWorkbook wb = openSample("46137.xls");
  1281. assertEquals(7, wb.getNumberOfSheets());
  1282. writeOutAndReadBack(wb).close();
  1283. wb.close();
  1284. }
  1285. /**
  1286. * Odd POIFS blocks issue:
  1287. * block[ 44 ] already removed from org.apache.poi.poifs.storage.BlockListImpl.remove
  1288. */
  1289. @Test
  1290. public void bug45290() throws Exception {
  1291. HSSFWorkbook wb = openSample("45290.xls");
  1292. assertEquals(1, wb.getNumberOfSheets());
  1293. wb.close();
  1294. }
  1295. /**
  1296. * In POI-2.5 user reported exception when parsing a name with a custom VBA function:
  1297. * =MY_VBA_FUNCTION("lskdjflsk")
  1298. */
  1299. @Test
  1300. public void bug30070() throws Exception {
  1301. HSSFWorkbook wb = openSample("30070.xls"); //contains custom VBA function 'Commission'
  1302. HSSFSheet sh = wb.getSheetAt(0);
  1303. HSSFCell cell = sh.getRow(0).getCell(1);
  1304. //B1 uses VBA in the formula
  1305. assertEquals("Commission(A1)", cell.getCellFormula());
  1306. //name sales_1 refers to Commission(Sheet0!$A$1)
  1307. int idx = wb.getNameIndex("sales_1");
  1308. assertTrue(idx != -1);
  1309. HSSFName name = wb.getNameAt(idx);
  1310. assertEquals("Commission(Sheet0!$A$1)", name.getRefersToFormula());
  1311. wb.close();
  1312. }
  1313. /**
  1314. * The link formulas which is referring to other books cannot be taken (the bug existed prior to POI-3.2)
  1315. * Expected:
  1316. * <p>
  1317. * [link_sub.xls]Sheet1!$A$1
  1318. * [link_sub.xls]Sheet1!$A$2
  1319. * [link_sub.xls]Sheet1!$A$3
  1320. * <p>
  1321. * POI-3.1 output:
  1322. * <p>
  1323. * Sheet1!$A$1
  1324. * Sheet1!$A$2
  1325. * Sheet1!$A$3
  1326. */
  1327. @Test
  1328. public void bug27364() throws Exception {
  1329. HSSFWorkbook wb = openSample("27364.xls");
  1330. HSSFSheet sheet = wb.getSheetAt(0);
  1331. assertEquals("[link_sub.xls]Sheet1!$A$1", sheet.getRow(0).getCell(0).getCellFormula());
  1332. assertEquals("[link_sub.xls]Sheet1!$A$2", sheet.getRow(1).getCell(0).getCellFormula());
  1333. assertEquals("[link_sub.xls]Sheet1!$A$3", sheet.getRow(2).getCell(0).getCellFormula());
  1334. wb.close();
  1335. }
  1336. /**
  1337. * Similar to bug#27364:
  1338. * HSSFCell.getCellFormula() fails with references to external workbooks
  1339. */
  1340. @Test
  1341. public void bug31661() throws Exception {
  1342. HSSFWorkbook wb = openSample("31661.xls");
  1343. HSSFSheet sheet = wb.getSheetAt(0);
  1344. HSSFCell cell = sheet.getRow(11).getCell(10); //K11
  1345. assertEquals("+'[GM Budget.xls]8085.4450'!$B$2", cell.getCellFormula());
  1346. wb.close();
  1347. }
  1348. /**
  1349. * Incorrect handling of non-ISO 8859-1 characters in Windows ANSII Code Page 1252
  1350. */
  1351. @Test
  1352. public void bug27394() throws Exception {
  1353. HSSFWorkbook wb = openSample("27394.xls");
  1354. assertEquals("\u0161\u017E", wb.getSheetName(0));
  1355. assertEquals("\u0161\u017E\u010D\u0148\u0159", wb.getSheetName(1));
  1356. HSSFSheet sheet = wb.getSheetAt(0);
  1357. assertEquals("\u0161\u017E", sheet.getRow(0).getCell(0).getStringCellValue());
  1358. assertEquals("\u0161\u017E\u010D\u0148\u0159", sheet.getRow(1).getCell(0).getStringCellValue());
  1359. wb.close();
  1360. }
  1361. /**
  1362. * Multiple calls of HSSFWorkbook.write result in corrupted xls
  1363. */
  1364. @Test
  1365. public void bug32191() throws IOException {
  1366. HSSFWorkbook wb = openSample("27394.xls");
  1367. ByteArrayOutputStream out = new ByteArrayOutputStream();
  1368. wb.write(out);
  1369. out.close();
  1370. int size1 = out.size();
  1371. out = new ByteArrayOutputStream();
  1372. wb.write(out);
  1373. out.close();
  1374. int size2 = out.size();
  1375. assertEquals(size1, size2);
  1376. out = new ByteArrayOutputStream();
  1377. wb.write(out);
  1378. out.close();
  1379. int size3 = out.size();
  1380. assertEquals(size2, size3);
  1381. wb.close();
  1382. }
  1383. /**
  1384. * java.io.IOException: block[ 0 ] already removed
  1385. * (is an excel 95 file though)
  1386. */
  1387. @Test
  1388. public void bug46904() throws Exception {
  1389. try {
  1390. POIFSFileSystem fs = new POIFSFileSystem(
  1391. HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"));
  1392. new HSSFWorkbook(fs.getRoot(), false).close();
  1393. fail("Should catch exception here");
  1394. } catch (OldExcelFormatException e) {
  1395. assertTrue(e.getMessage().startsWith(
  1396. "The supplied spreadsheet seems to be Excel"
  1397. ));
  1398. }
  1399. try {
  1400. try (POIFSFileSystem fs = new POIFSFileSystem(
  1401. HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"))) {
  1402. new HSSFWorkbook(fs.getRoot(), false).close();
  1403. fail("Should catch exception here");
  1404. }
  1405. } catch (OldExcelFormatException e) {
  1406. assertTrue(e.getMessage().startsWith(
  1407. "The supplied spreadsheet seems to be Excel"
  1408. ));
  1409. }
  1410. }
  1411. /**
  1412. * java.lang.NegativeArraySizeException reading long
  1413. * non-unicode data for a name record
  1414. */
  1415. @Test
  1416. public void bug47034() throws Exception {
  1417. HSSFWorkbook wb = openSample("47034.xls");
  1418. assertEquals(893, wb.getNumberOfNames());
  1419. assertEquals("Matthew\\Matthew11_1\\Matthew2331_1\\Matthew2351_1\\Matthew2361_1___lab", wb.getNameName(300));
  1420. wb.close();
  1421. }
  1422. /**
  1423. * HSSFRichTextString.length() returns negative for really long strings.
  1424. * The test file was created in OpenOffice 3.0 as Excel does not allow cell text longer than 32,767 characters
  1425. */
  1426. @Test
  1427. public void bug46368() throws Exception {
  1428. HSSFWorkbook wb = openSample("46368.xls");
  1429. HSSFSheet s = wb.getSheetAt(0);
  1430. HSSFCell cell1 = s.getRow(0).getCell(0);
  1431. assertEquals(32770, cell1.getStringCellValue().length());
  1432. HSSFCell cell2 = s.getRow(2).getCell(0);
  1433. assertEquals(32766, cell2.getStringCellValue().length());
  1434. wb.close();
  1435. }
  1436. /**
  1437. * Short records on certain sheets with charts in them
  1438. */
  1439. @Test
  1440. public void bug48180() throws Exception {
  1441. HSSFWorkbook wb = openSample("48180.xls");
  1442. HSSFSheet s = wb.getSheetAt(0);
  1443. HSSFCell cell1 = s.getRow(0).getCell(0);
  1444. assertEquals("test ", cell1.getStringCellValue());
  1445. HSSFCell cell2 = s.getRow(0).getCell(1);
  1446. assertEquals(1.0, cell2.getNumericCellValue(), 0.0);
  1447. wb.close();
  1448. }
  1449. /**
  1450. * POI 3.5 beta 7 can not read excel file contain list box (Form Control)
  1451. */
  1452. @Test
  1453. public void bug47701() {
  1454. openSample("47701.xls");
  1455. }
  1456. @Test
  1457. public void bug48026() {
  1458. openSample("48026.xls");
  1459. }
  1460. @Test
  1461. public void bug47251() {
  1462. // Firstly, try with one that triggers on InterfaceHdrRecord
  1463. openSample("47251.xls");
  1464. // Now with one that triggers on NoteRecord
  1465. openSample("47251_1.xls");
  1466. }
  1467. /**
  1468. * Round trip a file with an unusual UnicodeString/ExtRst record parts
  1469. */
  1470. @Test
  1471. public void bug47847() throws Exception {
  1472. HSSFWorkbook wb1 = openSample("47847.xls");
  1473. assertEquals(3, wb1.getNumberOfSheets());
  1474. // Find the SST record
  1475. UnicodeString withExt = wb1.getWorkbook().getSSTString(0);
  1476. UnicodeString withoutExt = wb1.getWorkbook().getSSTString(31);
  1477. assertEquals("O:Alloc:Qty", withExt.getString());
  1478. assertEquals(0x0004, (withExt.getOptionFlags() & 0x0004));
  1479. assertEquals("RT", withoutExt.getString());
  1480. assertEquals(0x0000, (withoutExt.getOptionFlags() & 0x0004));
  1481. // Something about continues...
  1482. // Write out and re-read
  1483. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  1484. wb1.close();
  1485. assertEquals(3, wb2.getNumberOfSheets());
  1486. // Check it's the same now
  1487. withExt = wb2.getWorkbook().getSSTString(0);
  1488. withoutExt = wb2.getWorkbook().getSSTString(31);
  1489. assertEquals("O:Alloc:Qty", withExt.getString());
  1490. assertEquals(0x0004, (withExt.getOptionFlags() & 0x0004));
  1491. assertEquals("RT", withoutExt.getString());
  1492. assertEquals(0x0000, (withoutExt.getOptionFlags() & 0x0004));
  1493. wb2.close();
  1494. }
  1495. /**
  1496. * Problem with cloning a sheet with a chart
  1497. * contained in it.
  1498. */
  1499. @Test
  1500. public void bug49096() throws Exception {
  1501. HSSFWorkbook wb1 = openSample("49096.xls");
  1502. assertEquals(1, wb1.getNumberOfSheets());
  1503. assertNotNull(wb1.getSheetAt(0));
  1504. wb1.cloneSheet(0);
  1505. assertEquals(2, wb1.getNumberOfSheets());
  1506. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  1507. wb1.close();
  1508. assertEquals(2, wb2.getNumberOfSheets());
  1509. wb2.close();
  1510. }
  1511. /**
  1512. * Newly created sheets need to get a
  1513. * proper TabID, otherwise print setup
  1514. * gets confused on them.
  1515. * Also ensure that print setup refs are
  1516. * by reference not value
  1517. */
  1518. @Test
  1519. public void bug46664() throws Exception {
  1520. HSSFWorkbook wb1 = new HSSFWorkbook();
  1521. HSSFSheet sheet = wb1.createSheet("new_sheet");
  1522. HSSFRow row = sheet.createRow((short) 0);
  1523. row.createCell(0).setCellValue(new HSSFRichTextString("Column A"));
  1524. row.createCell(1).setCellValue(new HSSFRichTextString("Column B"));
  1525. row.createCell(2).setCellValue(new HSSFRichTextString("Column C"));
  1526. row.createCell(3).setCellValue(new HSSFRichTextString("Column D"));
  1527. row.createCell(4).setCellValue(new HSSFRichTextString("Column E"));
  1528. row.createCell(5).setCellValue(new HSSFRichTextString("Column F"));
  1529. //set print area from column a to column c (on first row)
  1530. wb1.setPrintArea(
  1531. 0, //sheet index
  1532. 0, //start column
  1533. 2, //end column
  1534. 0, //start row
  1535. 0 //end row
  1536. );
  1537. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  1538. wb1.close();
  1539. // Ensure the tab index
  1540. TabIdRecord tr = null;
  1541. for (Record r : wb2.getWorkbook().getRecords()) {
  1542. if (r instanceof TabIdRecord) {
  1543. tr = (TabIdRecord) r;
  1544. }
  1545. }
  1546. assertNotNull(tr);
  1547. assertEquals(1, tr._tabids.length);
  1548. assertEquals(0, tr._tabids[0]);
  1549. // Ensure the print setup
  1550. assertEquals("new_sheet!$A$1:$C$1", wb2.getPrintArea(0));
  1551. HSSFName printArea = wb2.getName("Print_Area");
  1552. assertNotNull(printArea);
  1553. assertEquals("new_sheet!$A$1:$C$1", printArea.getRefersToFormula());
  1554. // Needs reference not value
  1555. NameRecord nr = wb2.getWorkbook().getNameRecord(
  1556. wb2.getNameIndex("Print_Area")
  1557. );
  1558. assertEquals("Print_Area", nr.getNameText());
  1559. assertEquals(1, nr.getNameDefinition().length);
  1560. assertEquals(
  1561. "new_sheet!$A$1:$C$1",
  1562. ((Area3DPtg) nr.getNameDefinition()[0]).toFormulaString(HSSFEvaluationWorkbook.create(wb2))
  1563. );
  1564. assertEquals('R', nr.getNameDefinition()[0].getRVAType());
  1565. wb2.close();
  1566. }
  1567. /**
  1568. * Problems with formula references to
  1569. * sheets via URLs
  1570. */
  1571. @Test
  1572. public void bug45970() throws Exception {
  1573. HSSFWorkbook wb1 = openSample("FormulaRefs.xls");
  1574. assertEquals(3, wb1.getNumberOfSheets());
  1575. HSSFSheet s = wb1.getSheetAt(0);
  1576. HSSFRow row;
  1577. row = s.getRow(0);
  1578. assertEquals(CellType.NUMERIC, row.getCell(1).getCellType());
  1579. assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
  1580. row = s.getRow(1);
  1581. assertEquals(CellType.FORMULA, row.getCell(1).getCellType());
  1582. assertEquals("B1", row.getCell(1).getCellFormula());
  1583. assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
  1584. row = s.getRow(2);
  1585. assertEquals(CellType.FORMULA, row.getCell(1).getCellType());
  1586. assertEquals("Sheet1!B1", row.getCell(1).getCellFormula());
  1587. assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
  1588. row = s.getRow(3);
  1589. assertEquals(CellType.FORMULA, row.getCell(1).getCellType());
  1590. assertEquals("[Formulas2.xls]Sheet1!B2", row.getCell(1).getCellFormula());
  1591. assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
  1592. row = s.getRow(4);
  1593. assertEquals(CellType.FORMULA, row.getCell(1).getCellType());
  1594. assertEquals("'[$http://gagravarr.org/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula());
  1595. assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
  1596. // Link our new workbook
  1597. Workbook externalWb1 = new HSSFWorkbook();
  1598. externalWb1.createSheet("Sheet1");
  1599. assertEquals(4, wb1.linkExternalWorkbook("$http://gagravarr.org/FormulaRefs2.xls", externalWb1));
  1600. // Change 4
  1601. row.getCell(1).setCellFormula("'[$http://gagravarr.org/FormulaRefs2.xls]Sheet1'!B2");
  1602. row.getCell(1).setCellValue(123.0);
  1603. // Link our new workbook
  1604. Workbook externalWb2 = new HSSFWorkbook();
  1605. externalWb2.createSheet("Sheet1");
  1606. assertEquals(5, wb1.linkExternalWorkbook("$http://example.com/FormulaRefs.xls", externalWb2));
  1607. // Add 5
  1608. row = s.createRow(5);
  1609. row.createCell(1, CellType.FORMULA);
  1610. row.getCell(1).setCellFormula("'[$http://example.com/FormulaRefs.xls]Sheet1'!B1");
  1611. row.getCell(1).setCellValue(234.0);
  1612. // Re-test
  1613. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  1614. wb1.close();
  1615. s = wb2.getSheetAt(0);
  1616. row = s.getRow(0);
  1617. assertEquals(CellType.NUMERIC, row.getCell(1).getCellType());
  1618. assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
  1619. row = s.getRow(1);
  1620. assertEquals(CellType.FORMULA, row.getCell(1).getCellType());
  1621. assertEquals("B1", row.getCell(1).getCellFormula());
  1622. assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
  1623. row = s.getRow(2);
  1624. assertEquals(CellType.FORMULA, row.getCell(1).getCellType());
  1625. assertEquals("Sheet1!B1", row.getCell(1).getCellFormula());
  1626. assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
  1627. row = s.getRow(3);
  1628. assertEquals(CellType.FORMULA, row.getCell(1).getCellType());
  1629. assertEquals("[Formulas2.xls]Sheet1!B2", row.getCell(1).getCellFormula());
  1630. assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
  1631. row = s.getRow(4);
  1632. assertEquals(CellType.FORMULA, row.getCell(1).getCellType());
  1633. assertEquals("'[$http://gagravarr.org/FormulaRefs2.xls]Sheet1'!B2", row.getCell(1).getCellFormula());
  1634. assertEquals(123.0, row.getCell(1).getNumericCellValue(), 0);
  1635. row = s.getRow(5);
  1636. assertEquals(CellType.FORMULA, row.getCell(1).getCellType());
  1637. assertEquals("'[$http://example.com/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula());
  1638. assertEquals(234.0, row.getCell(1).getNumericCellValue(), 0);
  1639. wb2.close();
  1640. }
  1641. /**
  1642. * Test for a file with NameRecord with NameCommentRecord comments
  1643. */
  1644. @Test
  1645. public void bug49185() throws Exception {
  1646. HSSFWorkbook wb1 = openSample("49185.xls");
  1647. Name name = wb1.getName("foobarName");
  1648. assertNotNull(name);
  1649. assertEquals("This is a comment", name.getComment());
  1650. // Rename the name, comment comes with it
  1651. name.setNameName("ChangedName");
  1652. assertEquals("This is a comment", name.getComment());
  1653. // Save and re-check
  1654. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  1655. wb1.close();
  1656. name = wb2.getName("ChangedName");
  1657. assertNotNull(name);
  1658. assertEquals("This is a comment", name.getComment());
  1659. // Now try to change it
  1660. name.setComment("Changed Comment");
  1661. assertEquals("Changed Comment", name.getComment());
  1662. // Save and re-check
  1663. HSSFWorkbook wb3 = writeOutAndReadBack(wb2);
  1664. wb2.close();
  1665. name = wb3.getName("ChangedName");
  1666. assertNotNull(name);
  1667. assertEquals("Changed Comment", name.getComment());
  1668. wb3.close();
  1669. }
  1670. /**
  1671. * Vertically aligned text
  1672. */
  1673. @Test
  1674. public void bug49524() throws Exception {
  1675. HSSFWorkbook wb1 = openSample("49524.xls");
  1676. Sheet s = wb1.getSheetAt(0);
  1677. Row r = s.getRow(0);
  1678. Cell rotated = r.getCell(0);
  1679. Cell normal = r.getCell(1);
  1680. // Check the current ones
  1681. assertEquals(0, normal.getCellStyle().getRotation());
  1682. assertEquals(0xff, rotated.getCellStyle().getRotation());
  1683. // Add a new style, also rotated
  1684. CellStyle cs = wb1.createCellStyle();
  1685. cs.setRotation((short) 0xff);
  1686. Cell nc = r.createCell(2);
  1687. nc.setCellValue("New Rotated Text");
  1688. nc.setCellStyle(cs);
  1689. assertEquals(0xff, nc.getCellStyle().getRotation());
  1690. // Write out and read back
  1691. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  1692. wb1.close();
  1693. // Re-check
  1694. s = wb2.getSheetAt(0);
  1695. r = s.getRow(0);
  1696. rotated = r.getCell(0);
  1697. normal = r.getCell(1);
  1698. nc = r.getCell(2);
  1699. assertEquals(0, normal.getCellStyle().getRotation());
  1700. assertEquals(0xff, rotated.getCellStyle().getRotation());
  1701. assertEquals(0xff, nc.getCellStyle().getRotation());
  1702. wb2.close();
  1703. }
  1704. /**
  1705. * Setting the user style name on custom styles
  1706. */
  1707. @Test
  1708. public void bug49689() throws Exception {
  1709. HSSFWorkbook wb1 = new HSSFWorkbook();
  1710. HSSFSheet s = wb1.createSheet("Test");
  1711. HSSFRow r = s.createRow(0);
  1712. HSSFCell c = r.createCell(0);
  1713. HSSFCellStyle cs1 = wb1.createCellStyle();
  1714. HSSFCellStyle cs2 = wb1.createCellStyle();
  1715. HSSFCellStyle cs3 = wb1.createCellStyle();
  1716. assertEquals(21, cs1.getIndex());
  1717. cs1.setUserStyleName("Testing");
  1718. assertEquals(22, cs2.getIndex());
  1719. cs2.setUserStyleName("Testing 2");
  1720. assertEquals(23, cs3.getIndex());
  1721. cs3.setUserStyleName("Testing 3");
  1722. // Set one
  1723. c.setCellStyle(cs1);
  1724. // Write out and read back
  1725. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  1726. wb1.close();
  1727. // Re-check
  1728. assertEquals("Testing", wb2.getCellStyleAt((short) 21).getUserStyleName());
  1729. assertEquals("Testing 2", wb2.getCellStyleAt((short) 22).getUserStyleName());
  1730. assertEquals("Testing 3", wb2.getCellStyleAt((short) 23).getUserStyleName());
  1731. wb2.close();
  1732. }
  1733. @Test
  1734. public void bug49751() throws Exception {
  1735. HSSFWorkbook wb = openSample("49751.xls");
  1736. int numCellStyles = wb.getNumCellStyles();
  1737. List<String> namedStyles = Arrays.asList(
  1738. "20% - Accent1", "20% - Accent2", "20% - Accent3", "20% - Accent4", "20% - Accent5",
  1739. "20% - Accent6", "40% - Accent1", "40% - Accent2", "40% - Accent3", "40% - Accent4",
  1740. "40% - Accent5", "40% - Accent6", "60% - Accent1", "60% - Accent2", "60% - Accent3",
  1741. "60% - Accent4", "60% - Accent5", "60% - Accent6", "Accent1", "Accent2", "Accent3",
  1742. "Accent4", "Accent5", "Accent6", "Bad", "Calculation", "Check Cell", "Explanatory Text",
  1743. "Good", "Heading 1", "Heading 2", "Heading 3", "Heading 4", "Input", "Linked Cell",
  1744. "Neutral", "Note", "Output", "Title", "Total", "Warning Text");
  1745. List<String> collecteddStyles = new ArrayList<>();
  1746. for (int i = 0; i < numCellStyles; i++) {
  1747. HSSFCellStyle cellStyle = wb.getCellStyleAt(i);
  1748. String styleName = cellStyle.getUserStyleName();
  1749. if (styleName != null) {
  1750. collecteddStyles.add(styleName);
  1751. }
  1752. }
  1753. assertTrue(namedStyles.containsAll(collecteddStyles));
  1754. wb.close();
  1755. }
  1756. /**
  1757. * Regression with the PageSettingsBlock
  1758. */
  1759. @Test
  1760. public void bug49931() throws Exception {
  1761. HSSFWorkbook wb = openSample("49931.xls");
  1762. assertEquals(1, wb.getNumberOfSheets());
  1763. assertEquals("Foo", wb.getSheetAt(0).getRow(0).getCell(0).getRichStringCellValue().toString());
  1764. wb.close();
  1765. }
  1766. /**
  1767. * Missing left/right/centre options on a footer
  1768. */
  1769. @Test
  1770. public void bug48325() throws Exception {
  1771. HSSFWorkbook wb = openSample("48325.xls");
  1772. HSSFSheet sh = wb.getSheetAt(0);
  1773. HSSFFooter f = sh.getFooter();
  1774. // Will show as the center, as that is what excel does
  1775. // with an invalid footer lacking left/right/center details
  1776. assertEquals("Left text should be empty", "", f.getLeft());
  1777. assertEquals("Right text should be empty", "", f.getRight());
  1778. assertEquals(
  1779. "Center text should contain the illegal value",
  1780. "BlahBlah blah blah ", f.getCenter()
  1781. );
  1782. wb.close();
  1783. }
  1784. /**
  1785. * IllegalStateException received when creating Data validation in sheet with macro
  1786. */
  1787. @Test
  1788. public void bug50020() throws Exception {
  1789. HSSFWorkbook wb = openSample("50020.xls");
  1790. writeOutAndReadBack(wb).close();
  1791. wb.close();
  1792. }
  1793. @Test
  1794. public void bug50426() throws Exception {
  1795. HSSFWorkbook wb = openSample("50426.xls");
  1796. writeOutAndReadBack(wb).close();
  1797. wb.close();
  1798. }
  1799. /**
  1800. * Last row number when shifting rows
  1801. */
  1802. @Test
  1803. public void bug50416LastRowNumber() throws IOException {
  1804. // Create the workbook with 1 sheet which contains 3 rows
  1805. HSSFWorkbook workbook = new HSSFWorkbook();
  1806. Sheet sheet = workbook.createSheet("Bug50416");
  1807. Row row1 = sheet.createRow(0);
  1808. Cell cellA_1 = row1.createCell(0, CellType.STRING);
  1809. cellA_1.setCellValue("Cell A,1");
  1810. Row row2 = sheet.createRow(1);
  1811. Cell cellA_2 = row2.createCell(0, CellType.STRING);
  1812. cellA_2.setCellValue("Cell A,2");
  1813. Row row3 = sheet.createRow(2);
  1814. Cell cellA_3 = row3.createCell(0, CellType.STRING);
  1815. cellA_3.setCellValue("Cell A,3");
  1816. // Test the last Row number it currently correct
  1817. assertEquals(2, sheet.getLastRowNum());
  1818. // Shift the first row to the end
  1819. sheet.shiftRows(0, 0, 3);
  1820. assertEquals(3, sheet.getLastRowNum());
  1821. assertEquals(-1, sheet.getRow(0).getLastCellNum());
  1822. assertEquals("Cell A,2", sheet.getRow(1).getCell(0).getStringCellValue());
  1823. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1824. assertEquals("Cell A,1", sheet.getRow(3).getCell(0).getStringCellValue());
  1825. // Shift the 2nd row up to the first one
  1826. sheet.shiftRows(1, 1, -1);
  1827. assertEquals(3, sheet.getLastRowNum());
  1828. assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
  1829. assertEquals(-1, sheet.getRow(1).getLastCellNum());
  1830. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1831. assertEquals("Cell A,1", sheet.getRow(3).getCell(0).getStringCellValue());
  1832. // Shift the 4th row up into the gap in the 3rd row
  1833. sheet.shiftRows(3, 3, -2);
  1834. assertEquals(2, sheet.getLastRowNum());
  1835. assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
  1836. assertEquals("Cell A,1", sheet.getRow(1).getCell(0).getStringCellValue());
  1837. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1838. assertEquals(-1, sheet.getRow(3).getLastCellNum());
  1839. // Now zap the empty 4th row - won't do anything
  1840. sheet.removeRow(sheet.getRow(3));
  1841. // Test again the last row number which should be 2
  1842. assertEquals(2, sheet.getLastRowNum());
  1843. assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
  1844. assertEquals("Cell A,1", sheet.getRow(1).getCell(0).getStringCellValue());
  1845. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1846. workbook.close();
  1847. }
  1848. /**
  1849. * If you send a file between Excel and OpenOffice enough, something
  1850. * will turn the "General" format into "GENERAL"
  1851. */
  1852. @Test
  1853. public void bug50756() throws Exception {
  1854. HSSFWorkbook wb = openSample("50756.xls");
  1855. HSSFSheet s = wb.getSheetAt(0);
  1856. HSSFRow r17 = s.getRow(16);
  1857. HSSFRow r18 = s.getRow(17);
  1858. HSSFDataFormatter df = new HSSFDataFormatter();
  1859. assertEquals(10.0, r17.getCell(1).getNumericCellValue(), 0);
  1860. assertEquals(20.0, r17.getCell(2).getNumericCellValue(), 0);
  1861. assertEquals(20.0, r17.getCell(3).getNumericCellValue(), 0);
  1862. assertEquals("GENERAL", r17.getCell(1).getCellStyle().getDataFormatString());
  1863. assertEquals("GENERAL", r17.getCell(2).getCellStyle().getDataFormatString());
  1864. assertEquals("GENERAL", r17.getCell(3).getCellStyle().getDataFormatString());
  1865. assertEquals("10", df.formatCellValue(r17.getCell(1)));
  1866. assertEquals("20", df.formatCellValue(r17.getCell(2)));
  1867. assertEquals("20", df.formatCellValue(r17.getCell(3)));
  1868. assertEquals(16.0, r18.getCell(1).getNumericCellValue(), 0);
  1869. assertEquals(35.0, r18.getCell(2).getNumericCellValue(), 0);
  1870. assertEquals(123.0, r18.getCell(3).getNumericCellValue(), 0);
  1871. assertEquals("GENERAL", r18.getCell(1).getCellStyle().getDataFormatString());
  1872. assertEquals("GENERAL", r18.getCell(2).getCellStyle().getDataFormatString());
  1873. assertEquals("GENERAL", r18.getCell(3).getCellStyle().getDataFormatString());
  1874. assertEquals("16", df.formatCellValue(r18.getCell(1)));
  1875. assertEquals("35", df.formatCellValue(r18.getCell(2)));
  1876. assertEquals("123", df.formatCellValue(r18.getCell(3)));
  1877. wb.close();
  1878. }
  1879. /**
  1880. * A protected sheet with comments, when written out by
  1881. * POI, ends up upsetting excel.
  1882. * TODO Identify the cause and add extra asserts for
  1883. * the bit excel cares about
  1884. */
  1885. @Test
  1886. public void bug50833() throws Exception {
  1887. HSSFWorkbook wb1 = openSample("50833.xls");
  1888. HSSFSheet s = wb1.getSheetAt(0);
  1889. assertEquals("Sheet1", s.getSheetName());
  1890. assertFalse(s.getProtect());
  1891. HSSFCell c = s.getRow(0).getCell(0);
  1892. assertEquals("test cell value", c.getRichStringCellValue().getString());
  1893. HSSFComment cmt = c.getCellComment();
  1894. assertNotNull(cmt);
  1895. assertEquals("Robert Lawrence", cmt.getAuthor());
  1896. assertEquals("Robert Lawrence:\ntest comment", cmt.getString().getString());
  1897. // Reload
  1898. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  1899. wb1.close();
  1900. s = wb2.getSheetAt(0);
  1901. c = s.getRow(0).getCell(0);
  1902. // Re-check the comment
  1903. cmt = c.getCellComment();
  1904. assertNotNull(cmt);
  1905. assertEquals("Robert Lawrence", cmt.getAuthor());
  1906. assertEquals("Robert Lawrence:\ntest comment", cmt.getString().getString());
  1907. // TODO Identify what excel doesn't like, and check for that
  1908. wb2.close();
  1909. }
  1910. @Test
  1911. public void bug50779() throws Exception {
  1912. HSSFWorkbook wb1 = openSample("50779_1.xls");
  1913. writeOutAndReadBack(wb1).close();
  1914. wb1.close();
  1915. HSSFWorkbook wb2 = openSample("50779_2.xls");
  1916. writeOutAndReadBack(wb2).close();
  1917. wb2.close();
  1918. }
  1919. /**
  1920. * The spec says that ChartEndObjectRecord has 6 reserved
  1921. * bytes on the end, but we sometimes find files without...
  1922. */
  1923. @Test
  1924. public void bug50939() throws Exception {
  1925. HSSFWorkbook wb = openSample("50939.xls");
  1926. assertEquals(2, wb.getNumberOfSheets());
  1927. wb.close();
  1928. }
  1929. @Test
  1930. public void bug49219() throws Exception {
  1931. HSSFWorkbook wb = openSample("49219.xls");
  1932. assertEquals(1, wb.getNumberOfSheets());
  1933. assertEquals("DGATE", wb.getSheetAt(0).getRow(1).getCell(0).getStringCellValue());
  1934. wb.close();
  1935. }
  1936. @Test
  1937. public void bug48968() throws Exception {
  1938. TimeZone userTimeZone = LocaleUtil.getUserTimeZone();
  1939. LocaleUtil.setUserTimeZone(TimeZone.getTimeZone("CET"));
  1940. try {
  1941. HSSFWorkbook wb = openSample("48968.xls");
  1942. assertEquals(1, wb.getNumberOfSheets());
  1943. DataFormatter fmt = new DataFormatter();
  1944. // Check the dates
  1945. HSSFSheet s = wb.getSheetAt(0);
  1946. Cell cell_d20110325 = s.getRow(0).getCell(0);
  1947. Cell cell_d19000102 = s.getRow(11).getCell(0);
  1948. Cell cell_d19000100 = s.getRow(21).getCell(0);
  1949. assertEquals(s.getRow(0).getCell(3).getStringCellValue(), fmt.formatCellValue(cell_d20110325));
  1950. assertEquals(s.getRow(11).getCell(3).getStringCellValue(), fmt.formatCellValue(cell_d19000102));
  1951. // There is no such thing as 00/01/1900...
  1952. assertEquals("00/01/1900 06:14:24", s.getRow(21).getCell(3).getStringCellValue());
  1953. assertEquals("31/12/1899 06:14:24", fmt.formatCellValue(cell_d19000100));
  1954. // Check the cached values
  1955. assertEquals("HOUR(A1)", s.getRow(5).getCell(0).getCellFormula());
  1956. assertEquals(11.0, s.getRow(5).getCell(0).getNumericCellValue(), 0);
  1957. assertEquals("MINUTE(A1)", s.getRow(6).getCell(0).getCellFormula());
  1958. assertEquals(39.0, s.getRow(6).getCell(0).getNumericCellValue(), 0);
  1959. assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
  1960. assertEquals(54.0, s.getRow(7).getCell(0).getNumericCellValue(), 0);
  1961. // Re-evaluate and check
  1962. HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  1963. assertEquals("HOUR(A1)", s.getRow(5).getCell(0).getCellFormula());
  1964. assertEquals(11.0, s.getRow(5).getCell(0).getNumericCellValue(), 0);
  1965. assertEquals("MINUTE(A1)", s.getRow(6).getCell(0).getCellFormula());
  1966. assertEquals(39.0, s.getRow(6).getCell(0).getNumericCellValue(), 0);
  1967. assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
  1968. assertEquals(54.0, s.getRow(7).getCell(0).getNumericCellValue(), 0);
  1969. // Push the time forward a bit and check
  1970. double date = s.getRow(0).getCell(0).getNumericCellValue();
  1971. s.getRow(0).getCell(0).setCellValue(date + 1.26);
  1972. HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  1973. assertEquals("HOUR(A1)", s.getRow(5).getCell(0).getCellFormula());
  1974. assertEquals(11.0 + 6.0, s.getRow(5).getCell(0).getNumericCellValue(), 0);
  1975. assertEquals("MINUTE(A1)", s.getRow(6).getCell(0).getCellFormula());
  1976. assertEquals(39.0 + 14.0 + 1, s.getRow(6).getCell(0).getNumericCellValue(), 0);
  1977. assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
  1978. assertEquals(54.0 + 24.0 - 60, s.getRow(7).getCell(0).getNumericCellValue(), 0);
  1979. wb.close();
  1980. } finally {
  1981. LocaleUtil.setUserTimeZone(userTimeZone);
  1982. }
  1983. }
  1984. /**
  1985. * Mixture of Ascii and Unicode strings in a
  1986. * NameComment record
  1987. */
  1988. @Test
  1989. public void bug51143() throws Exception {
  1990. HSSFWorkbook wb1 = openSample("51143.xls");
  1991. assertEquals(1, wb1.getNumberOfSheets());
  1992. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  1993. wb1.close();
  1994. assertEquals(1, wb2.getNumberOfSheets());
  1995. wb2.close();
  1996. }
  1997. /**
  1998. * File with exactly 256 data blocks (+header block)
  1999. * shouldn't break on POIFS loading
  2000. */
  2001. @Test
  2002. public void bug51461() throws Exception {
  2003. byte[] data = HSSFITestDataProvider.instance.getTestDataFileContent("51461.xls");
  2004. HSSFWorkbook wbPOIFS = new HSSFWorkbook(new POIFSFileSystem(
  2005. new ByteArrayInputStream(data)).getRoot(), false);
  2006. HSSFWorkbook wbPOIFS2 = new HSSFWorkbook(new POIFSFileSystem(
  2007. new ByteArrayInputStream(data)).getRoot(), false);
  2008. assertEquals(2, wbPOIFS.getNumberOfSheets());
  2009. assertEquals(2, wbPOIFS2.getNumberOfSheets());
  2010. }
  2011. @Test
  2012. public void bug51535() throws Exception {
  2013. byte[] data = HSSFITestDataProvider.instance.getTestDataFileContent("51535.xls");
  2014. HSSFWorkbook wbPOIFS = new HSSFWorkbook(new POIFSFileSystem(
  2015. new ByteArrayInputStream(data)).getRoot(), false);
  2016. HSSFWorkbook wbPOIFS2 = new HSSFWorkbook(new POIFSFileSystem(
  2017. new ByteArrayInputStream(data)).getRoot(), false);
  2018. for (HSSFWorkbook wb : new HSSFWorkbook[]{wbPOIFS, wbPOIFS2}) {
  2019. assertEquals(3, wb.getNumberOfSheets());
  2020. // Check directly
  2021. HSSFSheet s = wb.getSheetAt(0);
  2022. assertEquals("Top Left Cell", s.getRow(0).getCell(0).getStringCellValue());
  2023. assertEquals("Top Right Cell", s.getRow(0).getCell(255).getStringCellValue());
  2024. assertEquals("Bottom Left Cell", s.getRow(65535).getCell(0).getStringCellValue());
  2025. assertEquals("Bottom Right Cell", s.getRow(65535).getCell(255).getStringCellValue());
  2026. // Extract and check
  2027. ExcelExtractor ex = new ExcelExtractor(wb);
  2028. String text = ex.getText();
  2029. assertContains(text, "Top Left Cell");
  2030. assertContains(text, "Top Right Cell");
  2031. assertContains(text, "Bottom Left Cell");
  2032. assertContains(text, "Bottom Right Cell");
  2033. ex.close();
  2034. }
  2035. }
  2036. @Test
  2037. public void bug51670() throws Exception {
  2038. HSSFWorkbook wb = openSample("51670.xls");
  2039. writeOutAndReadBack(wb).close();
  2040. wb.close();
  2041. }
  2042. /**
  2043. * Sum across multiple workbooks
  2044. * eg =SUM($Sheet2.A1:$Sheet3.A1)
  2045. */
  2046. @Test
  2047. public void test48703() throws Exception {
  2048. HSSFWorkbook wb = openSample("48703.xls");
  2049. assertEquals(3, wb.getNumberOfSheets());
  2050. // Check reading the formula
  2051. Sheet sheet = wb.getSheetAt(0);
  2052. Row r = sheet.getRow(0);
  2053. Cell c = r.getCell(0);
  2054. assertEquals("SUM(Sheet2:Sheet3!A1)", c.getCellFormula());
  2055. assertEquals(4.0, c.getNumericCellValue(), 0);
  2056. // Check the evaluated result
  2057. HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb);
  2058. eval.evaluateFormulaCell(c);
  2059. assertEquals(4.0, c.getNumericCellValue(), 0);
  2060. wb.close();
  2061. }
  2062. /**
  2063. * Normally encrypted files have BOF then FILEPASS, but
  2064. * some may squeeze a WRITEPROTECT in the middle
  2065. */
  2066. @Test(expected = EncryptedDocumentException.class)
  2067. public void bug51832() {
  2068. openSample("51832.xls");
  2069. }
  2070. @Test
  2071. public void bug49896() throws Exception {
  2072. HSSFWorkbook wb = openSample("49896.xls");
  2073. HSSFCell cell = wb.getSheetAt(0).getRow(1).getCell(1);
  2074. String PATH_SEPARATOR = System.getProperty("file.separator");
  2075. assertEquals("VLOOKUP(A2,'[C:Documents and Settings" + PATH_SEPARATOR + "Yegor" + PATH_SEPARATOR
  2076. + "My Documents" + PATH_SEPARATOR + "csco.xls]Sheet1'!$A$2:$B$3,2,FALSE)",
  2077. cell.getCellFormula());
  2078. wb.close();
  2079. }
  2080. @Test
  2081. public void bug49529() throws Exception {
  2082. // user code reported in Bugzilla #49529
  2083. HSSFWorkbook wb = openSample("49529.xls");
  2084. wb.getSheetAt(0).createDrawingPatriarch();
  2085. // prior to the fix the line below failed with
  2086. // java.lang.IllegalStateException: EOF - next record not available
  2087. wb.cloneSheet(0);
  2088. // make sure we are still readable
  2089. writeOutAndReadBack(wb).close();
  2090. wb.close();
  2091. }
  2092. @Test
  2093. public void bug49612_part() throws IOException {
  2094. HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("49612.xls");
  2095. HSSFSheet sh = wb.getSheetAt(0);
  2096. HSSFRow row = sh.getRow(0);
  2097. HSSFCell c1 = row.getCell(2);
  2098. HSSFCell d1 = row.getCell(3);
  2099. HSSFCell e1 = row.getCell(2);
  2100. assertEquals("SUM(BOB+JIM)", c1.getCellFormula());
  2101. // Problem 1: See TestUnfixedBugs#test49612()
  2102. // Problem 2: TestUnfixedBugs#test49612()
  2103. // Problem 3: These used to fail, now pass
  2104. HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb);
  2105. assertEquals("evaluating c1", 30.0, eval.evaluate(c1).getNumberValue(), 0.001);
  2106. assertEquals("evaluating d1", 30.0, eval.evaluate(d1).getNumberValue(), 0.001);
  2107. assertEquals("evaluating e1", 30.0, eval.evaluate(e1).getNumberValue(), 0.001);
  2108. wb.close();
  2109. }
  2110. @Test
  2111. public void bug51675() throws Exception {
  2112. final List<Short> list = new ArrayList<>();
  2113. HSSFWorkbook wb = openSample("51675.xls");
  2114. HSSFSheet sh = wb.getSheetAt(0);
  2115. InternalSheet ish = HSSFTestHelper.getSheetForTest(sh);
  2116. PageSettingsBlock psb = (PageSettingsBlock) ish.getRecords().get(13);
  2117. psb.visitContainedRecords(r -> list.add(r.getSid()));
  2118. assertEquals(UnknownRecord.BITMAP_00E9, list.get(list.size() - 1).intValue());
  2119. assertEquals(UnknownRecord.HEADER_FOOTER_089C, list.get(list.size() - 2).intValue());
  2120. wb.close();
  2121. }
  2122. @Test
  2123. public void bug52272() throws IOException {
  2124. HSSFWorkbook wb = new HSSFWorkbook();
  2125. HSSFSheet sh = wb.createSheet();
  2126. HSSFPatriarch p = sh.createDrawingPatriarch();
  2127. HSSFSimpleShape s = p.createSimpleShape(new HSSFClientAnchor());
  2128. s.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
  2129. HSSFSheet sh2 = wb.cloneSheet(0);
  2130. assertNotNull(sh2.getDrawingPatriarch());
  2131. wb.close();
  2132. }
  2133. @Test
  2134. public void bug53432() throws IOException {
  2135. HSSFWorkbook wb1 = new HSSFWorkbook(); //or new HSSFWorkbook();
  2136. wb1.addPicture(new byte[]{123, 22}, Workbook.PICTURE_TYPE_JPEG);
  2137. assertEquals(wb1.getAllPictures().size(), 1);
  2138. wb1.close();
  2139. wb1.close();
  2140. wb1 = new HSSFWorkbook();
  2141. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  2142. wb1.close();
  2143. assertEquals(wb2.getAllPictures().size(), 0);
  2144. wb2.addPicture(new byte[]{123, 22}, Workbook.PICTURE_TYPE_JPEG);
  2145. assertEquals(wb2.getAllPictures().size(), 1);
  2146. HSSFWorkbook wb3 = writeOutAndReadBack(wb2);
  2147. wb2.close();
  2148. assertEquals(wb3.getAllPictures().size(), 1);
  2149. wb3.close();
  2150. }
  2151. @Test
  2152. public void bug46250() throws Exception {
  2153. HSSFWorkbook wb = openSample("46250.xls");
  2154. Sheet sh = wb.getSheet("Template");
  2155. Sheet cSh = wb.cloneSheet(wb.getSheetIndex(sh));
  2156. HSSFPatriarch patriarch = (HSSFPatriarch) cSh.createDrawingPatriarch();
  2157. HSSFTextbox tb = (HSSFTextbox) patriarch.getChildren().get(2);
  2158. tb.setString(new HSSFRichTextString("POI test"));
  2159. tb.setAnchor(new HSSFClientAnchor(0, 0, 0, 0, (short) 0, 0, (short) 10, 10));
  2160. writeOutAndReadBack(wb).close();
  2161. wb.close();
  2162. }
  2163. @Test
  2164. public void bug53404() throws Exception {
  2165. HSSFWorkbook wb = openSample("53404.xls");
  2166. Sheet sheet = wb.getSheet("test-sheet");
  2167. int rowCount = sheet.getLastRowNum() + 1;
  2168. int newRows = 5;
  2169. Calendar cal = LocaleUtil.getLocaleCalendar();
  2170. for (int r = rowCount; r < rowCount + newRows; r++) {
  2171. Row row = sheet.createRow((short) r);
  2172. row.createCell(0).setCellValue(1.03 * (r + 7));
  2173. row.createCell(1).setCellValue(cal.getTime());
  2174. row.createCell(2).setCellValue(cal);
  2175. row.createCell(3).setCellValue(String.format(Locale.ROOT, "row:%d/col:%d", r, 3));
  2176. row.createCell(4).setCellValue(true);
  2177. row.createCell(5).setCellErrorValue(FormulaError.NUM.getCode());
  2178. row.createCell(6).setCellValue("added cells.");
  2179. }
  2180. writeOutAndReadBack(wb).close();
  2181. wb.close();
  2182. }
  2183. @Test
  2184. public void bug54016() throws Exception {
  2185. // This used to break
  2186. HSSFWorkbook wb = openSample("54016.xls");
  2187. writeOutAndReadBack(wb).close();
  2188. wb.close();
  2189. }
  2190. /**
  2191. * Row style information is 12 not 16 bits
  2192. */
  2193. @Test
  2194. public void bug49237() throws Exception {
  2195. Workbook wb = openSample("49237.xls");
  2196. Sheet sheet = wb.getSheetAt(0);
  2197. Row row = sheet.getRow(0);
  2198. CellStyle rstyle = row.getRowStyle();
  2199. assertNotNull(rstyle);
  2200. assertEquals(BorderStyle.DOUBLE, rstyle.getBorderBottom());
  2201. wb.close();
  2202. }
  2203. /**
  2204. * POI doesn't currently support the RC4 CryptoAPI encryption header structure
  2205. */
  2206. @Test(expected = EncryptedDocumentException.class)
  2207. public void bug35897() throws Exception {
  2208. // password is abc
  2209. Biff8EncryptionKey.setCurrentUserPassword("abc");
  2210. try {
  2211. openSample("xor-encryption-abc.xls").close();
  2212. } finally {
  2213. Biff8EncryptionKey.setCurrentUserPassword(null);
  2214. }
  2215. // One using the only-recently-documented encryption header type 4,
  2216. // and the RC4 CryptoAPI encryption header structure
  2217. openSample("35897-type4.xls").close();
  2218. }
  2219. @Test
  2220. public void bug56450() throws Exception {
  2221. HSSFWorkbook wb = openSample("56450.xls");
  2222. HSSFSheet sheet = wb.getSheetAt(0);
  2223. int comments = 0;
  2224. for (Row r : sheet) {
  2225. for (Cell c : r) {
  2226. if (c.getCellComment() != null) {
  2227. assertNotNull(c.getCellComment().getString().getString());
  2228. comments++;
  2229. }
  2230. }
  2231. }
  2232. assertEquals(0, comments);
  2233. wb.close();
  2234. }
  2235. /**
  2236. * Files initially created with Excel 2010 can have >3 CF rules
  2237. */
  2238. @Test
  2239. public void bug56482() throws Exception {
  2240. HSSFWorkbook wb = openSample("56482.xls");
  2241. assertEquals(1, wb.getNumberOfSheets());
  2242. HSSFSheet sheet = wb.getSheetAt(0);
  2243. HSSFSheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();
  2244. assertEquals(5, cf.getNumConditionalFormattings());
  2245. wb.close();
  2246. }
  2247. @Test
  2248. public void bug56325() throws IOException {
  2249. HSSFWorkbook wb1;
  2250. POIFSFileSystem fs;
  2251. File file = HSSFTestDataSamples.getSampleFile("56325.xls");
  2252. try (InputStream stream = new FileInputStream(file)) {
  2253. fs = new POIFSFileSystem(stream);
  2254. wb1 = new HSSFWorkbook(fs);
  2255. }
  2256. assertEquals(3, wb1.getNumberOfSheets());
  2257. wb1.removeSheetAt(0);
  2258. assertEquals(2, wb1.getNumberOfSheets());
  2259. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  2260. wb1.close();
  2261. fs.close();
  2262. assertEquals(2, wb2.getNumberOfSheets());
  2263. wb2.removeSheetAt(0);
  2264. assertEquals(1, wb2.getNumberOfSheets());
  2265. wb2.removeSheetAt(0);
  2266. assertEquals(0, wb2.getNumberOfSheets());
  2267. HSSFWorkbook wb3 = writeOutAndReadBack(wb2);
  2268. wb2.close();
  2269. assertEquals(0, wb3.getNumberOfSheets());
  2270. wb3.close();
  2271. }
  2272. @Test
  2273. public void bug56325a() throws IOException {
  2274. HSSFWorkbook wb1 = HSSFTestDataSamples.openSampleWorkbook("56325a.xls");
  2275. HSSFSheet sheet = wb1.cloneSheet(2);
  2276. wb1.setSheetName(3, "Clone 1");
  2277. sheet.setRepeatingRows(CellRangeAddress.valueOf("2:3"));
  2278. wb1.setPrintArea(3, "$A$4:$C$10");
  2279. sheet = wb1.cloneSheet(2);
  2280. wb1.setSheetName(4, "Clone 2");
  2281. sheet.setRepeatingRows(CellRangeAddress.valueOf("2:3"));
  2282. wb1.setPrintArea(4, "$A$4:$C$10");
  2283. wb1.removeSheetAt(2);
  2284. Workbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
  2285. assertEquals(4, wb2.getNumberOfSheets());
  2286. wb2.close();
  2287. wb1.close();
  2288. }
  2289. /**
  2290. * Formulas which reference named ranges, either in other
  2291. * sheets, or workbook scoped but in other workbooks.
  2292. * Used to fail with
  2293. * java.lang.RuntimeException: Unexpected eval class (org.apache.poi.ss.formula.eval.NameXEval)
  2294. */
  2295. @Test
  2296. public void bug56737() throws IOException {
  2297. Workbook wb = openSample("56737.xls");
  2298. // Check the named range definitions
  2299. Name nSheetScope = wb.getName("NR_To_A1");
  2300. Name nWBScope = wb.getName("NR_Global_B2");
  2301. assertNotNull(nSheetScope);
  2302. assertNotNull(nWBScope);
  2303. assertEquals("Defines!$A$1", nSheetScope.getRefersToFormula());
  2304. assertEquals("Defines!$B$2", nWBScope.getRefersToFormula());
  2305. // Check the different kinds of formulas
  2306. Sheet s = wb.getSheetAt(0);
  2307. Cell cRefSName = s.getRow(1).getCell(3);
  2308. Cell cRefWName = s.getRow(2).getCell(3);
  2309. assertEquals("Defines!NR_To_A1", cRefSName.getCellFormula());
  2310. // TODO Correct this, so that the filename is shown too, see bug #56742
  2311. // This is what Excel itself shows
  2312. //assertEquals("'56737.xls'!NR_Global_B2", cRefWName.getCellFormula());
  2313. // TODO This isn't right, but it's what we currently generate....
  2314. assertEquals("NR_Global_B2", cRefWName.getCellFormula());
  2315. // Try to evaluate them
  2316. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  2317. assertEquals("Test A1", eval.evaluate(cRefSName).getStringValue());
  2318. assertEquals(142, (int) eval.evaluate(cRefWName).getNumberValue());
  2319. // Try to evaluate everything
  2320. eval.evaluateAll();
  2321. wb.close();
  2322. }
  2323. /**
  2324. * ClassCastException in HSSFOptimiser - StyleRecord cannot be cast to
  2325. * ExtendedFormatRecord when removing un-used styles
  2326. */
  2327. @Test
  2328. public void bug54443() throws Exception {
  2329. HSSFWorkbook workbook = new HSSFWorkbook();
  2330. HSSFCellStyle style = workbook.createCellStyle();
  2331. HSSFCellStyle newStyle = workbook.createCellStyle();
  2332. HSSFSheet mySheet = workbook.createSheet();
  2333. HSSFRow row = mySheet.createRow(0);
  2334. HSSFCell cell = row.createCell(0);
  2335. // Use style
  2336. cell.setCellStyle(style);
  2337. // Switch to newStyle, style is now un-used
  2338. cell.setCellStyle(newStyle);
  2339. // Optimize
  2340. HSSFOptimiser.optimiseCellStyles(workbook);
  2341. workbook.close();
  2342. }
  2343. /**
  2344. * Intersection formula ranges, eg =(C2:D3 D3:E4)
  2345. */
  2346. @Test
  2347. public void bug52111() throws Exception {
  2348. Workbook wb = openSample("Intersection-52111.xls");
  2349. Sheet s = wb.getSheetAt(0);
  2350. assertFormula(wb, s.getRow(2).getCell(0), "(C2:D3 D3:E4)", "4.0");
  2351. assertFormula(wb, s.getRow(6).getCell(0), "Tabelle2!E:E Tabelle2!$A11:$IV11", "5.0");
  2352. assertFormula(wb, s.getRow(8).getCell(0), "Tabelle2!E:F Tabelle2!$A11:$IV12", null);
  2353. wb.close();
  2354. }
  2355. @Test
  2356. public void bug42016() throws Exception {
  2357. Workbook wb = openSample("42016.xls");
  2358. Sheet s = wb.getSheetAt(0);
  2359. for (int row = 0; row < 7; row++) {
  2360. assertEquals("A$1+B$1", s.getRow(row).getCell(2).getCellFormula());
  2361. }
  2362. wb.close();
  2363. }
  2364. /**
  2365. * Unexpected record type (org.apache.poi.hssf.record.ColumnInfoRecord)
  2366. */
  2367. @Test
  2368. public void bug53984() throws Exception {
  2369. Workbook wb = openSample("53984.xls");
  2370. Sheet s = wb.getSheetAt(0);
  2371. assertEquals("International Communication Services SA", s.getRow(2).getCell(0).getStringCellValue());
  2372. assertEquals("Saudi Arabia-Riyadh", s.getRow(210).getCell(0).getStringCellValue());
  2373. wb.close();
  2374. }
  2375. /**
  2376. * Read, write, read for formulas point to cells in other files.
  2377. * See {@link #bug46670()} for the main test, this just
  2378. * covers reading an existing file and checking it.
  2379. *
  2380. * See base-test-class for some related tests that still fail
  2381. */
  2382. @Test
  2383. public void bug46670_existing() throws Exception {
  2384. // Expected values
  2385. String refLocal = "'[refs" + File.separator + "airport.xls]Sheet1'!$A$2";
  2386. String refHttp = "'[9http://www.principlesofeconometrics.com/excel/airline.xls]Sheet1'!$A$2";
  2387. // Check we can read them correctly
  2388. Workbook wb1 = openSample("46670_local.xls");
  2389. Sheet s = wb1.getSheetAt(0);
  2390. assertEquals(refLocal, s.getRow(0).getCell(0).getCellFormula());
  2391. wb1.close();
  2392. HSSFWorkbook wb2 = openSample("46670_http.xls");
  2393. s = wb2.getSheetAt(0);
  2394. assertEquals(refHttp, s.getRow(0).getCell(0).getCellFormula());
  2395. wb2.close();
  2396. // Now try to set them to the same values, and ensure that
  2397. // they end up as they did before, even with a save and re-load
  2398. HSSFWorkbook wb3 = openSample("46670_local.xls");
  2399. s = wb3.getSheetAt(0);
  2400. Cell c = s.getRow(0).getCell(0);
  2401. c.setCellFormula(refLocal);
  2402. assertEquals(refLocal, c.getCellFormula());
  2403. HSSFWorkbook wb4 = HSSFTestDataSamples.writeOutAndReadBack(wb3);
  2404. wb3.close();
  2405. s = wb4.getSheetAt(0);
  2406. assertEquals(refLocal, s.getRow(0).getCell(0).getCellFormula());
  2407. wb4.close();
  2408. HSSFWorkbook wb5 = openSample("46670_http.xls");
  2409. s = wb5.getSheetAt(0);
  2410. c = s.getRow(0).getCell(0);
  2411. c.setCellFormula(refHttp);
  2412. assertEquals(refHttp, c.getCellFormula());
  2413. Workbook wb6 = HSSFTestDataSamples.writeOutAndReadBack(wb5);
  2414. wb5.close();
  2415. s = wb6.getSheetAt(0);
  2416. assertEquals(refHttp, s.getRow(0).getCell(0).getCellFormula());
  2417. wb6.close();
  2418. }
  2419. @Test
  2420. public void test57456() throws IOException {
  2421. Workbook wb = openSample("57456.xls");
  2422. wb.close();
  2423. }
  2424. @Test
  2425. public void test57163() throws IOException {
  2426. Workbook wb = openSample("57163.xls");
  2427. while (wb.getNumberOfSheets() > 1) {
  2428. wb.removeSheetAt(1);
  2429. }
  2430. wb.close();
  2431. }
  2432. @Test
  2433. public void test53109() throws IOException {
  2434. HSSFWorkbook wb1 = openSample("53109.xls");
  2435. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  2436. assertNotNull(wb2);
  2437. wb2.close();
  2438. wb1.close();
  2439. }
  2440. @Test
  2441. public void test53109a() throws IOException {
  2442. HSSFWorkbook wb1 = openSample("com.aida-tour.www_SPO_files_maldives%20august%20october.xls");
  2443. Workbook wb2 = writeOutAndReadBack(wb1);
  2444. assertNotNull(wb2);
  2445. wb2.close();
  2446. wb1.close();
  2447. }
  2448. @Test
  2449. public void test48043() throws IOException {
  2450. HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("56325a.xls");
  2451. wb.removeSheetAt(2);
  2452. wb.removeSheetAt(1);
  2453. //Sheet s = wb.createSheet("sheetname");
  2454. Sheet s = wb.getSheetAt(0);
  2455. Row row = s.createRow(0);
  2456. Cell cell = row.createCell(0);
  2457. cell.setCellFormula(
  2458. "IF(AND(ISBLANK(A10)," +
  2459. "ISBLANK(B10)),\"\"," +
  2460. "CONCATENATE(A10,\"-\",B10))");
  2461. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  2462. eval.evaluateAll();
  2463. /*OutputStream out = new FileOutputStream("C:\\temp\\48043.xls");
  2464. try {
  2465. wb.write(out);
  2466. } finally {
  2467. out.close();
  2468. }*/
  2469. Workbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb);
  2470. assertNotNull(wbBack);
  2471. wbBack.close();
  2472. wb.close();
  2473. }
  2474. @Test
  2475. public void test57925() throws IOException {
  2476. Workbook wb = HSSFTestDataSamples.openSampleWorkbook("57925.xls");
  2477. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  2478. for (int i = 0; i < wb.getNumberOfSheets(); i++) {
  2479. Sheet sheet = wb.getSheetAt(i);
  2480. for (Row row : sheet) {
  2481. for (Cell cell : row) {
  2482. new DataFormatter().formatCellValue(cell);
  2483. }
  2484. }
  2485. }
  2486. wb.close();
  2487. }
  2488. @Test
  2489. public void test46515() throws IOException {
  2490. Workbook wb = HSSFTestDataSamples.openSampleWorkbook("46515.xls");
  2491. // Get structure from webservice
  2492. String urlString = "http://poi.apache.org/components/spreadsheet/images/calendar.jpg";
  2493. URL structURL = new URL(urlString);
  2494. BufferedImage bimage;
  2495. try {
  2496. bimage = ImageIO.read(structURL);
  2497. } catch (IOException e) {
  2498. Assume.assumeNoException("Downloading a jpg from poi.apache.org should work", e);
  2499. return;
  2500. } finally {
  2501. wb.close();
  2502. }
  2503. // Convert BufferedImage to byte[]
  2504. ByteArrayOutputStream imageBAOS = new ByteArrayOutputStream();
  2505. ImageIO.write(bimage, "jpeg", imageBAOS);
  2506. imageBAOS.flush();
  2507. byte[] imageBytes = imageBAOS.toByteArray();
  2508. imageBAOS.close();
  2509. // Pop structure into Structure HSSFSheet
  2510. int pict = wb.addPicture(imageBytes, HSSFWorkbook.PICTURE_TYPE_JPEG);
  2511. Sheet sheet = wb.getSheet("Structure");
  2512. assertNotNull("Did not find sheet", sheet);
  2513. HSSFPatriarch patriarch = (HSSFPatriarch) sheet.createDrawingPatriarch();
  2514. HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 10, 22);
  2515. anchor.setAnchorType(AnchorType.MOVE_DONT_RESIZE);
  2516. patriarch.createPicture(anchor, pict);
  2517. // Write out destination file
  2518. // FileOutputStream fileOut = new FileOutputStream("/tmp/46515.xls");
  2519. // wb.write(fileOut);
  2520. // fileOut.close();
  2521. wb.close();
  2522. }
  2523. @Test
  2524. public void test55668() throws IOException {
  2525. Workbook wb = HSSFTestDataSamples.openSampleWorkbook("55668.xls");
  2526. Sheet sheet = wb.getSheetAt(0);
  2527. Row row = sheet.getRow(0);
  2528. Cell cell = row.getCell(0);
  2529. assertEquals(CellType.FORMULA, cell.getCellType());
  2530. assertEquals("IF(TRUE,\"\",\"\")", cell.getCellFormula());
  2531. assertEquals("", cell.getStringCellValue());
  2532. //noinspection deprecation
  2533. cell.setCellType(CellType.STRING);
  2534. assertEquals(CellType.BLANK, cell.getCellType());
  2535. try {
  2536. assertNull(cell.getCellFormula());
  2537. fail("Should throw an exception here");
  2538. } catch (IllegalStateException e) {
  2539. // expected here
  2540. }
  2541. assertEquals("", cell.getStringCellValue());
  2542. wb.close();
  2543. }
  2544. @Test
  2545. public void test55982() throws IOException {
  2546. Workbook wb = HSSFTestDataSamples.openSampleWorkbook("55982.xls");
  2547. Sheet newSheet = wb.cloneSheet(1);
  2548. assertNotNull(newSheet);
  2549. wb.close();
  2550. }
  2551. /**
  2552. * Test generator of ids for the CommonObjectDataSubRecord record.
  2553. */
  2554. @Test
  2555. public void test51332() {
  2556. HSSFClientAnchor anchor = new HSSFClientAnchor();
  2557. HSSFSimpleShape shape;
  2558. CommonObjectDataSubRecord cmo;
  2559. shape = new HSSFTextbox(null, anchor);
  2560. shape.setShapeId(1025);
  2561. cmo = (CommonObjectDataSubRecord) shape.getObjRecord().getSubRecords().get(0);
  2562. assertEquals(1, cmo.getObjectId());
  2563. shape = new HSSFPicture(null, anchor);
  2564. shape.setShapeId(1026);
  2565. cmo = (CommonObjectDataSubRecord) shape.getObjRecord().getSubRecords().get(0);
  2566. assertEquals(2, cmo.getObjectId());
  2567. shape = new HSSFComment(null, anchor);
  2568. shape.setShapeId(1027);
  2569. cmo = (CommonObjectDataSubRecord) shape.getObjRecord().getSubRecords().get(0);
  2570. assertEquals(1027, cmo.getObjectId());
  2571. }
  2572. // As of POI 3.15 beta 2, LibreOffice does not display the diagonal border while it does display the bottom border
  2573. // I have not checked Excel to know if this is a LibreOffice or a POI problem.
  2574. @Test
  2575. public void test53564() throws IOException {
  2576. HSSFWorkbook wb = new HSSFWorkbook();
  2577. HSSFSheet sheet = wb.createSheet("Page 1");
  2578. final short BLUE = 30;
  2579. HSSFSheetConditionalFormatting scf = sheet.getSheetConditionalFormatting();
  2580. HSSFConditionalFormattingRule rule = scf.createConditionalFormattingRule(ComparisonOperator.GT, "10");
  2581. HSSFBorderFormatting bord = rule.createBorderFormatting();
  2582. bord.setBorderDiagonal(BorderStyle.THICK);
  2583. assertEquals(BorderStyle.THICK, bord.getBorderDiagonal());
  2584. bord.setBackwardDiagonalOn(true);
  2585. assertTrue(bord.isBackwardDiagonalOn());
  2586. bord.setForwardDiagonalOn(true);
  2587. assertTrue(bord.isForwardDiagonalOn());
  2588. bord.setDiagonalBorderColor(BLUE);
  2589. assertEquals(BLUE, bord.getDiagonalBorderColor());
  2590. // Create the bottom border style so we know what a border is supposed to look like
  2591. bord.setBorderBottom(BorderStyle.THICK);
  2592. assertEquals(BorderStyle.THICK, bord.getBorderBottom());
  2593. bord.setBottomBorderColor(BLUE);
  2594. assertEquals(BLUE, bord.getBottomBorderColor());
  2595. CellRangeAddress[] A2_D4 = {new CellRangeAddress(1, 3, 0, 3)};
  2596. scf.addConditionalFormatting(A2_D4, rule);
  2597. // Set a cell value within the conditional formatting range whose rule would resolve to True.
  2598. Cell C3 = sheet.createRow(2).createCell(2);
  2599. C3.setCellValue(30.0);
  2600. // Manually check the output file with Excel to see if the diagonal border is present
  2601. //OutputStream fos = new FileOutputStream("/tmp/53564.xls");
  2602. //wb.write(fos);
  2603. //fos.close();
  2604. wb.close();
  2605. }
  2606. @Test
  2607. public void test45353a() throws IOException {
  2608. final Workbook wb = HSSFTestDataSamples.openSampleWorkbook("named-cell-in-formula-test.xls");
  2609. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  2610. wb.close();
  2611. }
  2612. @Test
  2613. public void test45353b() throws IOException {
  2614. final Workbook wb = HSSFTestDataSamples.openSampleWorkbook("named-cell-test.xls");
  2615. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  2616. wb.close();
  2617. }
  2618. @Test
  2619. public void test61287() throws IOException {
  2620. final HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("61287.xls");
  2621. ExcelExtractor ex = new ExcelExtractor(wb);
  2622. String text = ex.getText();
  2623. assertContains(text, "\u8D44\u4EA7\u8D1F\u503A\u8868");
  2624. wb.close();
  2625. }
  2626. @Test(expected = RuntimeException.class)
  2627. public void test61300() throws Exception {
  2628. POIFSFileSystem poifs = new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("61300.xls"));
  2629. DocumentEntry entry =
  2630. (DocumentEntry) poifs.getRoot().getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
  2631. // this will throw an Exception "RuntimeException: Can't read negative number of bytes"
  2632. new PropertySet(new DocumentInputStream(entry));
  2633. }
  2634. @Test
  2635. public void test51262() throws IOException {
  2636. try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("51262.xls")) {
  2637. Sheet sheet = wb.getSheetAt(0);
  2638. Row row = sheet.getRow(2);
  2639. Cell cell = row.getCell(1);
  2640. CellStyle style = cell.getCellStyle();
  2641. assertEquals(26, style.getFontIndexAsInt());
  2642. row = sheet.getRow(3);
  2643. cell = row.getCell(1);
  2644. style = cell.getCellStyle();
  2645. assertEquals(28, style.getFontIndexAsInt());
  2646. // check the two fonts
  2647. HSSFFont font = wb.getFontAt(26);
  2648. assertTrue(font.getBold());
  2649. assertEquals(10, font.getFontHeightInPoints());
  2650. assertEquals("\uFF2D\uFF33 \uFF30\u30B4\u30B7\u30C3\u30AF", font.getFontName());
  2651. font = wb.getFontAt(28);
  2652. assertTrue(font.getBold());
  2653. assertEquals(10, font.getFontHeightInPoints());
  2654. assertEquals("\uFF2D\uFF33 \uFF30\u30B4\u30B7\u30C3\u30AF", font.getFontName());
  2655. }
  2656. }
  2657. @Test
  2658. public void test60460() throws IOException {
  2659. try (final Workbook wb = HSSFTestDataSamples.openSampleWorkbook("60460.xls")) {
  2660. assertEquals(2, wb.getAllNames().size());
  2661. Name rangedName = wb.getAllNames().get(0);
  2662. assertFalse(rangedName.isFunctionName());
  2663. assertEquals("'[\\\\HEPPC3\\gt$\\Teaching\\Syn\\physyn.xls]#REF'!$AK$70:$AL$70",
  2664. // replace '/' to make test work equally on Windows and Linux
  2665. rangedName.getRefersToFormula().replace("/", "\\"));
  2666. rangedName = wb.getAllNames().get(1);
  2667. assertFalse(rangedName.isFunctionName());
  2668. assertEquals("Questionnaire!$A$1:$L$65", rangedName.getRefersToFormula());
  2669. }
  2670. }
  2671. @Test
  2672. public void test63940() throws IOException {
  2673. try (final Workbook wb = HSSFTestDataSamples.openSampleWorkbook("SUBSTITUTE.xls")) {
  2674. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  2675. }
  2676. }
  2677. }