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

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