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

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