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

TestBugs.java 90KB

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