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

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