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

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