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

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