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

TestBugs.java 92KB

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