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.

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