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

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