Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TestBugs.java 76KB

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