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

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