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

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