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

TestBugs.java 104KB

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