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

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