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

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