Lines 1-27
Link Here
|
1 |
/* |
1 |
|
2 |
* RangeTestTest.java |
2 |
/* ==================================================================== |
3 |
* NetBeans JUnit based test |
3 |
* The Apache Software License, Version 1.1 |
|
|
4 |
* |
5 |
* Copyright (c) 2002 The Apache Software Foundation. All rights |
6 |
* reserved. |
7 |
* |
8 |
* Redistribution and use in source and binary forms, with or without |
9 |
* modification, are permitted provided that the following conditions |
10 |
* are met: |
11 |
* |
12 |
* 1. Redistributions of source code must retain the above copyright |
13 |
* notice, this list of conditions and the following disclaimer. |
14 |
* |
15 |
* 2. Redistributions in binary form must reproduce the above copyright |
16 |
* notice, this list of conditions and the following disclaimer in |
17 |
* the documentation and/or other materials provided with the |
18 |
* distribution. |
19 |
* |
20 |
* 3. The end-user documentation included with the redistribution, |
21 |
* if any, must include the following acknowledgment: |
22 |
* "This product includes software developed by the |
23 |
* Apache Software Foundation (http://www.apache.org/)." |
24 |
* Alternately, this acknowledgment may appear in the software itself, |
25 |
* if and wherever such third-party acknowledgments normally appear. |
4 |
* |
26 |
* |
5 |
* Created on April 21, 2002, 6:23 PM |
27 |
* 4. The names "Apache" and "Apache Software Foundation" and |
|
|
28 |
* "Apache POI" must not be used to endorse or promote products |
29 |
* derived from this software without prior written permission. For |
30 |
* written permission, please contact apache@apache.org. |
31 |
* |
32 |
* 5. Products derived from this software may not be called "Apache", |
33 |
* "Apache POI", nor may "Apache" appear in their name, without |
34 |
* prior written permission of the Apache Software Foundation. |
35 |
* |
36 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
37 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
38 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
39 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR |
40 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
41 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
42 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
43 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
44 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
45 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
46 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
47 |
* SUCH DAMAGE. |
48 |
* ==================================================================== |
49 |
* |
50 |
* This software consists of voluntary contributions made by many |
51 |
* individuals on behalf of the Apache Software Foundation. For more |
52 |
* information on the Apache Software Foundation, please see |
53 |
* <http://www.apache.org/>. |
6 |
*/ |
54 |
*/ |
7 |
|
55 |
|
8 |
package org.apache.poi.hssf.usermodel; |
56 |
package org.apache.poi.hssf.usermodel; |
9 |
|
57 |
|
10 |
import junit.framework.*; |
|
|
11 |
|
12 |
import org.apache.poi.poifs.filesystem.POIFSFileSystem; |
13 |
|
14 |
import java.io.File; |
58 |
import java.io.File; |
15 |
import java.io.FileInputStream; |
59 |
import java.io.FileInputStream; |
16 |
import java.io.FileNotFoundException; |
|
|
17 |
import java.io.FileOutputStream; |
60 |
import java.io.FileOutputStream; |
18 |
import java.io.IOException; |
61 |
import java.io.IOException; |
19 |
|
62 |
|
|
|
63 |
import junit.framework.TestCase; |
64 |
|
65 |
import org.apache.poi.hssf.util.AreaReference; |
66 |
import org.apache.poi.poifs.filesystem.POIFSFileSystem; |
67 |
|
20 |
|
68 |
|
21 |
/** |
69 |
/** |
22 |
* |
|
|
23 |
* @author ROMANL |
70 |
* @author ROMANL |
24 |
* @author Andrew C. Oliver (acoliver at apache dot org) |
71 |
* @author Andrew C. Oliver (acoliver at apache dot org) |
|
|
72 |
* @author Danny Mui (danny at muibros.com) |
25 |
*/ |
73 |
*/ |
26 |
public class TestNamedRange |
74 |
public class TestNamedRange |
27 |
extends TestCase { |
75 |
extends TestCase { |
Lines 54-59
Link Here
|
54 |
junit.textui.TestRunner.run(TestNamedRange.class); |
102 |
junit.textui.TestRunner.run(TestNamedRange.class); |
55 |
} |
103 |
} |
56 |
|
104 |
|
|
|
105 |
|
106 |
/** |
107 |
* There was bug where the named reference was not being set properly |
108 |
* <p> |
109 |
* i.e. Sheet1!$1$2 did not translate to Sheet1!$A$1:$UV$2 |
110 |
*/ |
111 |
public void testNameRowReference() |
112 |
{ |
113 |
HSSFWorkbook wb = new HSSFWorkbook(); |
114 |
wb.createSheet("Sheet 1"); |
115 |
HSSFName name = wb.createName(); |
116 |
|
117 |
name.setNameName("test"); |
118 |
name.setReference("Sheet 1!$1:$2"); |
119 |
|
120 |
assertEquals("Row references should be converted: ", "Sheet 1!$A$1:$"+AreaReference.MAXIMUM_COLUMN+"$2", name.getReference()); |
121 |
|
122 |
} |
123 |
|
57 |
/** Test of TestCase method, of class test.RangeTest. */ |
124 |
/** Test of TestCase method, of class test.RangeTest. */ |
58 |
public void testNamedRange() |
125 |
public void testNamedRange() |
59 |
throws IOException |
126 |
throws IOException |
Lines 63-76
Link Here
|
63 |
HSSFWorkbook wb = null; |
130 |
HSSFWorkbook wb = null; |
64 |
|
131 |
|
65 |
String filename = System.getProperty("HSSF.testdata.path"); |
132 |
String filename = System.getProperty("HSSF.testdata.path"); |
66 |
|
|
|
67 |
filename = filename + "/Simple.xls"; |
133 |
filename = filename + "/Simple.xls"; |
68 |
|
134 |
|
|
|
135 |
fis = new FileInputStream(filename); |
136 |
fs = new POIFSFileSystem(fis); |
137 |
wb = new HSSFWorkbook(fs); |
69 |
|
138 |
|
70 |
fis = new FileInputStream(filename); |
139 |
//Creating new Named Range |
71 |
fs = new POIFSFileSystem(fis); |
140 |
HSSFName newNamedRange = wb.createName(); |
72 |
wb = new HSSFWorkbook(fs); |
|
|
73 |
|
141 |
|
|
|
142 |
//Getting Sheet Name for the reference |
143 |
String sheetName = wb.getSheetName(0); |
144 |
|
145 |
//Setting its name |
146 |
newNamedRange.setNameName("RangeTest"); |
147 |
//Setting its reference |
148 |
newNamedRange.setReference(sheetName + "!$D$4:$E$8"); |
149 |
|
150 |
//Getting Named Range |
151 |
HSSFName namedRange1 = wb.getNameAt(0); |
152 |
//Getting it sheet name |
153 |
sheetName = namedRange1.getSheetName(); |
154 |
//Getting its reference |
155 |
String referece = namedRange1.getReference(); |
156 |
|
157 |
File file = File.createTempFile("testNamedRange", |
158 |
".xls"); |
159 |
|
160 |
FileOutputStream fileOut = new FileOutputStream(file); |
161 |
wb.write(fileOut); |
162 |
fis.close(); |
163 |
fileOut.close(); |
164 |
|
165 |
assertTrue("file exists",file.exists()); |
166 |
|
167 |
|
168 |
FileInputStream in = new FileInputStream(file); |
169 |
wb = new HSSFWorkbook(in); |
170 |
HSSFName nm =wb.getNameAt(wb.getNameIndex("RangeTest")); |
171 |
assertTrue("Name is "+nm.getNameName(),"RangeTest".equals(nm.getNameName())); |
172 |
assertEquals("Reference Matches",wb.getSheetName(0)+"!$D$4:$E$8", nm.getReference()); |
173 |
|
174 |
|
175 |
} |
176 |
|
177 |
/** Test that multiple named ranges can be added |
178 |
*/ |
179 |
public void testMultipleNamedRange() |
180 |
throws IOException |
181 |
{ |
182 |
FileInputStream fis = null; |
183 |
POIFSFileSystem fs = null; |
184 |
HSSFWorkbook wb = null; |
185 |
|
186 |
String filename = System.getProperty("HSSF.testdata.path"); |
187 |
filename = filename + "/Simple.xls"; |
188 |
|
189 |
fis = new FileInputStream(filename); |
190 |
fs = new POIFSFileSystem(fis); |
191 |
wb = new HSSFWorkbook(fs); |
74 |
|
192 |
|
75 |
//Creating new Named Range |
193 |
//Creating new Named Range |
76 |
HSSFName newNamedRange = wb.createName(); |
194 |
HSSFName newNamedRange = wb.createName(); |
Lines 81-88
Link Here
|
81 |
//Setting its name |
199 |
//Setting its name |
82 |
newNamedRange.setNameName("RangeTest"); |
200 |
newNamedRange.setNameName("RangeTest"); |
83 |
//Setting its reference |
201 |
//Setting its reference |
84 |
newNamedRange.setReference(sheetName + ".$D$4:$E$8"); |
202 |
newNamedRange.setReference(sheetName + "!$D$4:$E$8"); |
85 |
|
203 |
|
|
|
204 |
//Creating another new Named Range |
205 |
HSSFName newNamedRange2 = wb.createName(); |
206 |
|
207 |
//Getting Sheet Name for the reference |
208 |
sheetName = wb.getSheetName(1); |
209 |
|
210 |
//Setting its name |
211 |
newNamedRange2.setNameName("AnotherTest"); |
212 |
//Setting its reference |
213 |
newNamedRange2.setReference(sheetName + "!$F$1:$G$6"); |
214 |
|
215 |
|
86 |
//Getting NAmed Range |
216 |
//Getting NAmed Range |
87 |
HSSFName namedRange1 = wb.getNameAt(0); |
217 |
HSSFName namedRange1 = wb.getNameAt(0); |
88 |
//Getting it sheet name |
218 |
//Getting it sheet name |
Lines 90-96
Link Here
|
90 |
//Getting its reference |
220 |
//Getting its reference |
91 |
String referece = namedRange1.getReference(); |
221 |
String referece = namedRange1.getReference(); |
92 |
|
222 |
|
93 |
File file = File.createTempFile("testNamedRange", |
223 |
File file = File.createTempFile("testMultiNamedRange", |
94 |
".xls"); |
224 |
".xls"); |
95 |
|
225 |
|
96 |
FileOutputStream fileOut = new FileOutputStream(file); |
226 |
FileOutputStream fileOut = new FileOutputStream(file); |
Lines 100-113
Link Here
|
100 |
|
230 |
|
101 |
assertTrue("file exists",file.exists()); |
231 |
assertTrue("file exists",file.exists()); |
102 |
|
232 |
|
|
|
233 |
|
103 |
FileInputStream in = new FileInputStream(file); |
234 |
FileInputStream in = new FileInputStream(file); |
104 |
wb = new HSSFWorkbook(in); |
235 |
wb = new HSSFWorkbook(in); |
105 |
HSSFName nm =wb.getNameAt(wb.getNameIndex("RangeTest")); |
236 |
HSSFName nm =wb.getNameAt(wb.getNameIndex("RangeTest")); |
106 |
assertTrue("Name is "+nm.getNameName(),"RangeTest".equals(nm.getNameName())); |
237 |
assertTrue("Name is "+nm.getNameName(),"RangeTest".equals(nm.getNameName())); |
107 |
assertTrue("Reference is "+nm.getReference(),(wb.getSheetName(0)+"!$D$4:$E$8").equals(nm.getReference())); |
238 |
assertTrue("Reference is "+nm.getReference(),(wb.getSheetName(0)+"!$D$4:$E$8").equals(nm.getReference())); |
108 |
|
239 |
|
|
|
240 |
nm = wb.getNameAt(wb.getNameIndex("AnotherTest")); |
241 |
assertTrue("Name is "+nm.getNameName(),"AnotherTest".equals(nm.getNameName())); |
242 |
assertTrue("Reference is "+nm.getReference(),newNamedRange2.getReference().equals(nm.getReference())); |
243 |
|
244 |
|
245 |
} |
246 |
|
247 |
/** |
248 |
* Addresses Bug #16411 |
249 |
*/ |
250 |
public void testNamedRead() throws IOException |
251 |
{ |
252 |
FileInputStream fis = null; |
253 |
POIFSFileSystem fs = null; |
254 |
HSSFWorkbook wb = null; |
255 |
|
256 |
String filename = System.getProperty("HSSF.testdata.path"); |
257 |
filename = filename + "/SimpleNamed.xls"; |
258 |
|
259 |
fis = new FileInputStream(filename); |
260 |
fs = new POIFSFileSystem(fis); |
261 |
wb = new HSSFWorkbook(fs); |
262 |
|
263 |
HSSFName name = wb.getNameAt(0); |
264 |
String sheetName = wb.getSheetName(0); |
265 |
|
266 |
assertEquals("Retrieved name", "Test", name.getNameName()); |
267 |
assertEquals("Reference",sheetName+"!$C$8", name.getReference()); |
268 |
|
269 |
name.setReference(sheetName+"!$A$1:$C$36"); |
270 |
assertEquals("Reference",sheetName+"!$A$1:$C$36", name.getReference()); |
271 |
|
272 |
fis.close(); |
273 |
} |
274 |
|
275 |
/**Test to see if the print areas can be retrieved/created in memory |
276 |
*/ |
277 |
public void testSinglePrintArea() |
278 |
{ |
279 |
HSSFWorkbook workbook = new HSSFWorkbook(); |
280 |
HSSFSheet sheet = workbook.createSheet("Test Print Area"); |
281 |
String sheetName = workbook.getSheetName(0); |
282 |
//HSSFName printArea = workbook.createPrintArea(0); |
283 |
//assertNotNull("Print Area is Null", printArea); |
284 |
|
285 |
//printArea.setReference(); |
286 |
String reference = sheetName+"!$A$1:$B$1"; |
287 |
workbook.setPrintArea(0, reference); |
288 |
|
289 |
String retrievedPrintArea = workbook.getPrintArea(0); |
290 |
|
291 |
assertNotNull("Print Area not defined for first sheet", retrievedPrintArea); |
292 |
assertEquals("References Match", reference, retrievedPrintArea); |
293 |
|
294 |
} |
295 |
|
296 |
/** |
297 |
* Test to see if the print area made it to the file |
298 |
*/ |
299 |
public void testPrintAreaFile() |
300 |
throws IOException |
301 |
{ |
302 |
HSSFWorkbook workbook = new HSSFWorkbook(); |
303 |
HSSFSheet sheet = workbook.createSheet("Test Print Area"); |
304 |
String sheetName = workbook.getSheetName(0); |
305 |
//HSSFName printArea = workbook.createPrintArea(0); |
306 |
//assertNotNull("Print Area is Null", printArea); |
307 |
|
308 |
//printArea.setReference(); |
309 |
String reference = sheetName+"!$A$1:$B$1"; |
310 |
workbook.setPrintArea(0, reference); |
311 |
|
312 |
File file = File.createTempFile("testPrintArea",".xls"); |
313 |
|
314 |
FileOutputStream fileOut = new FileOutputStream(file); |
315 |
workbook.write(fileOut); |
316 |
fileOut.close(); |
317 |
|
318 |
assertTrue("file exists",file.exists()); |
319 |
|
320 |
FileInputStream in = new FileInputStream(file); |
321 |
workbook = new HSSFWorkbook(in); |
322 |
|
323 |
String retrievedPrintArea = workbook.getPrintArea(0); |
324 |
assertNotNull("Print Area not defined for first sheet", retrievedPrintArea); |
325 |
assertEquals("References Match", reference, retrievedPrintArea); |
109 |
|
326 |
|
110 |
} |
327 |
} |
|
|
328 |
|
329 |
/**Test to see if the multiple print areas can be retrieved/created in memory |
330 |
*/ |
331 |
public void testMultiplePrintArea() |
332 |
{ |
333 |
HSSFWorkbook workbook = new HSSFWorkbook(); |
334 |
HSSFSheet sheet = workbook.createSheet("Sheet 1"); |
335 |
sheet = workbook.createSheet("Sheet 2"); |
336 |
sheet = workbook.createSheet("Sheet 3"); |
111 |
|
337 |
|
|
|
338 |
String sheetName = workbook.getSheetName(0); |
339 |
String reference = null; |
340 |
|
341 |
|
342 |
reference = sheetName+"!$A$1:$B$1"; |
343 |
workbook.setPrintArea(0, reference); |
344 |
|
345 |
sheetName = workbook.getSheetName(1); |
346 |
String reference2 = sheetName+"!$B$2:$D$5"; |
347 |
workbook.setPrintArea(1, reference2); |
348 |
|
349 |
sheetName = workbook.getSheetName(2); |
350 |
String reference3 = sheetName+"!$D$2:$F$5"; |
351 |
workbook.setPrintArea(2, reference3); |
352 |
|
353 |
String retrievedPrintArea = workbook.getPrintArea(0); |
354 |
assertNotNull("Print Area Not Found (Sheet 1)", retrievedPrintArea); |
355 |
assertEquals("References Do Not Match (Sheet 1)", reference, retrievedPrintArea); |
356 |
|
357 |
String retrievedPrintArea2 = workbook.getPrintArea(1); |
358 |
assertNotNull("Print Area Not Found (Sheet 2)", retrievedPrintArea2); |
359 |
assertEquals("References Do Not Match (Sheet 2)", reference2, retrievedPrintArea2); |
360 |
|
361 |
String retrievedPrintArea3 = workbook.getPrintArea(2); |
362 |
assertNotNull("Print Area Not Found (Sheet 3)", retrievedPrintArea3); |
363 |
assertEquals("References Do Not Match (Sheet 3)", reference3, retrievedPrintArea3); |
364 |
|
365 |
|
366 |
|
367 |
} |
368 |
|
369 |
/** |
370 |
* Test to see if multiple print areas made it to the file |
371 |
*/ |
372 |
public void testMultiplePrintAreaFile() |
373 |
throws IOException |
374 |
{ |
375 |
HSSFWorkbook workbook = new HSSFWorkbook(); |
376 |
|
377 |
HSSFSheet sheet = workbook.createSheet("Sheet 1"); |
378 |
sheet = workbook.createSheet("Sheet 2"); |
379 |
sheet = workbook.createSheet("Sheet 3"); |
380 |
|
381 |
String sheetName = workbook.getSheetName(0); |
382 |
String reference = null; |
383 |
|
384 |
|
385 |
reference = sheetName+"!$A$1:$B$1"; |
386 |
workbook.setPrintArea(0, reference); |
387 |
|
388 |
sheetName = workbook.getSheetName(1); |
389 |
String reference2 = sheetName+"!$B$2:$D$5"; |
390 |
workbook.setPrintArea(1, reference2); |
391 |
|
392 |
sheetName = workbook.getSheetName(2); |
393 |
String reference3 = sheetName+"!$D$2:$F$5"; |
394 |
workbook.setPrintArea(2, reference3); |
395 |
|
396 |
File file = File.createTempFile("testMultiPrintArea",".xls"); |
397 |
|
398 |
FileOutputStream fileOut = new FileOutputStream(file); |
399 |
workbook.write(fileOut); |
400 |
fileOut.close(); |
401 |
|
402 |
assertTrue("file exists",file.exists()); |
403 |
|
404 |
FileInputStream in = new FileInputStream(file); |
405 |
workbook = new HSSFWorkbook(in); |
406 |
|
407 |
String retrievedPrintArea = workbook.getPrintArea(0); |
408 |
assertNotNull("Print Area Not Found (Sheet 1)", retrievedPrintArea); |
409 |
assertEquals("References Do Not Match (Sheet 1)", reference, retrievedPrintArea); |
410 |
|
411 |
String retrievedPrintArea2 = workbook.getPrintArea(1); |
412 |
assertNotNull("Print Area Not Found (Sheet 2)", retrievedPrintArea2); |
413 |
assertEquals("References Do Not Match (Sheet 2)", reference2, retrievedPrintArea2); |
414 |
|
415 |
String retrievedPrintArea3 = workbook.getPrintArea(2); |
416 |
assertNotNull("Print Area Not Found (Sheet 3)", retrievedPrintArea3); |
417 |
assertEquals("References Do Not Match (Sheet 3)", reference3, retrievedPrintArea3); |
418 |
|
419 |
|
420 |
} |
421 |
|
422 |
/**Test to see if the printTitles can be retrieved/created in memory |
423 |
*/ |
424 |
public void testSinglePrintTitles() |
425 |
{ |
426 |
HSSFWorkbook workbook = new HSSFWorkbook(); |
427 |
HSSFSheet sheet = workbook.createSheet("Test Print Titles"); |
428 |
String sheetName = workbook.getSheetName(0); |
429 |
HSSFPrintTitles titles = workbook.createPrintTitles(0); |
430 |
assertNotNull("Print Title is Null", titles); |
431 |
|
432 |
titles.setRowReference(sheetName+"!$1:$2"); |
433 |
assertEquals("Sheetnames did not carry over.", titles.getSheetName(), sheetName); |
434 |
assertEquals("PrintTitles properly set.", titles.getNameName(), "Print_Titles"); |
435 |
|
436 |
|
437 |
HSSFName retrievedName = workbook.getPrintTitles(0); |
438 |
assertEquals("PrintTitles properly set.", retrievedName.getNameName(), "Print_Titles"); |
439 |
|
440 |
assertEquals("References Match", titles.getReference(), retrievedName.getReference()); |
441 |
|
442 |
} |
443 |
|
444 |
/** |
445 |
* Test to see if the print_titles made it to the file |
446 |
*/ |
447 |
public void testPrintTitlesFile() |
448 |
throws IOException |
449 |
{ |
450 |
HSSFWorkbook workbook = new HSSFWorkbook(); |
451 |
HSSFSheet sheet = workbook.createSheet("Test Print Titles"); |
452 |
String sheetName = workbook.getSheetName(0); |
453 |
HSSFPrintTitles titles = workbook.createPrintTitles(0); |
454 |
assertNotNull("Print Title is Null", titles); |
455 |
|
456 |
titles.setRowReference(sheetName+"!$1:$2"); |
457 |
assertEquals("Sheetnames did not carry over.", titles.getSheetName(), sheetName); |
458 |
assertEquals("PrintTitles properly set.", titles.getNameName(), "Print_Titles"); |
459 |
|
460 |
File file = File.createTempFile("testPrintTitles",".xls"); |
461 |
|
462 |
FileOutputStream fileOut = new FileOutputStream(file); |
463 |
workbook.write(fileOut); |
464 |
fileOut.close(); |
465 |
|
466 |
assertTrue("file exists",file.exists()); |
467 |
|
468 |
FileInputStream in = new FileInputStream(file); |
469 |
workbook = new HSSFWorkbook(in); |
470 |
HSSFPrintTitles retrievedTitles = workbook.getPrintTitles(0); |
471 |
assertNotNull("Print Area Not Found", retrievedTitles); |
472 |
assertEquals("References Do Not Match", titles.getReference(), retrievedTitles.getReference()); |
473 |
assertEquals("Row References Do Not Match", titles.getRowReference(), retrievedTitles.getRowReference()); |
474 |
assertEquals("Column References Do Not Match", titles.getColReference(), retrievedTitles.getColReference()); |
475 |
|
476 |
} |
477 |
|
478 |
/**Test to see if the multiple print_titles can be retrieved/created in memory |
479 |
*/ |
480 |
public void testMultiplePrintTitles() |
481 |
{ |
482 |
HSSFWorkbook workbook = new HSSFWorkbook(); |
483 |
HSSFSheet sheet = workbook.createSheet("Sheet 1"); |
484 |
sheet = workbook.createSheet("Sheet 2"); |
485 |
sheet = workbook.createSheet("Sheet 3"); |
486 |
|
487 |
String sheetName = workbook.getSheetName(0); |
488 |
|
489 |
HSSFPrintTitles titles = workbook.createPrintTitles(0); |
490 |
assertNotNull("Print Titles (Sheet 1) is Null", titles); |
491 |
|
492 |
titles.setRowReference(sheetName+"!$1:$2"); |
493 |
assertEquals("Sheetnames did not carry over.", titles.getSheetName(), sheetName); |
494 |
|
495 |
|
496 |
sheetName = workbook.getSheetName(1); |
497 |
HSSFPrintTitles titles2 = workbook.createPrintTitles(1); |
498 |
assertNotNull("Print Titles (Sheet 2) is Null", titles2); |
499 |
|
500 |
titles2.setColReference(sheetName+"!$A:$A"); |
501 |
assertNotNull("Print Titles (Sheet 2) is Null", titles2); |
502 |
|
503 |
sheetName = workbook.getSheetName(2); |
504 |
HSSFPrintTitles titles3 = workbook.createPrintTitles(2); |
505 |
assertNotNull("Print Titles (Sheet 3) is Null", titles3); |
506 |
|
507 |
titles3.setColReference(sheetName+"!$D:$E"); |
508 |
assertNotNull("Print Titles (Sheet 3) is Null", titles3); |
509 |
|
510 |
|
511 |
HSSFName retrievedTitles = workbook.getPrintTitles(0); |
512 |
assertEquals("References Match", titles.getReference(), retrievedTitles.getReference()); |
513 |
|
514 |
HSSFName retrievedTitles2 = workbook.getPrintTitles(1); |
515 |
assertEquals("References Match (Sheet 2)", titles2.getReference(), retrievedTitles2.getReference()); |
516 |
|
517 |
HSSFName retrievedTitles3 = workbook.getPrintTitles(2); |
518 |
assertEquals("References Match (Sheet 3)", titles3.getReference(), retrievedTitles3.getReference()); |
519 |
|
520 |
} |
521 |
|
522 |
/** |
523 |
* Test to see if multiple print titles made it to the file |
524 |
*/ |
525 |
public void testMultiplePrintTitlesFile() |
526 |
throws IOException |
527 |
{ |
528 |
|
529 |
HSSFWorkbook workbook = new HSSFWorkbook(); |
530 |
HSSFSheet sheet = workbook.createSheet("Sheet 1"); |
531 |
sheet = workbook.createSheet("Sheet 2"); |
532 |
sheet = workbook.createSheet("Sheet 3"); |
533 |
|
534 |
String sheetName = workbook.getSheetName(0); |
535 |
|
536 |
HSSFPrintTitles titles = workbook.createPrintTitles(0); |
537 |
assertNotNull("Print Titles (Sheet 1) is Null", titles); |
538 |
|
539 |
titles.setRowReference(sheetName+"!$1:$2"); |
540 |
assertEquals("Sheetnames did not carry over.", titles.getSheetName(), sheetName); |
541 |
|
542 |
|
543 |
sheetName = workbook.getSheetName(1); |
544 |
HSSFPrintTitles titles2 = workbook.createPrintTitles(1); |
545 |
assertNotNull("Print Titles (Sheet 2) is Null", titles2); |
546 |
|
547 |
titles2.setColReference(sheetName+"!$A:$A"); |
548 |
assertNotNull("Print Titles (Sheet 2) is Null", titles2); |
549 |
|
550 |
sheetName = workbook.getSheetName(2); |
551 |
HSSFPrintTitles titles3 = workbook.createPrintTitles(2); |
552 |
assertNotNull("Print Titles (Sheet 3) is Null", titles3); |
553 |
|
554 |
titles3.setColReference(sheetName+"!$D:$E"); |
555 |
assertNotNull("Print Titles (Sheet 3) is Null", titles3); |
556 |
|
557 |
File file = File.createTempFile("testMultiPrintTitles",".xls"); |
558 |
FileOutputStream fileOut = new FileOutputStream(file); |
559 |
workbook.write(fileOut); |
560 |
fileOut.close(); |
561 |
|
562 |
assertTrue("file exists",file.exists()); |
563 |
|
564 |
FileInputStream in = new FileInputStream(file); |
565 |
workbook = new HSSFWorkbook(in); |
566 |
|
567 |
|
568 |
HSSFName retrievedTitles = workbook.getPrintTitles(0); |
569 |
assertEquals("References Match", titles.getReference(), retrievedTitles.getReference()); |
570 |
|
571 |
HSSFName retrievedTitles2 = workbook.getPrintTitles(1); |
572 |
assertEquals("References Match (Sheet 2)", titles2.getReference(), retrievedTitles2.getReference()); |
573 |
|
574 |
HSSFName retrievedTitles3 = workbook.getPrintTitles(2); |
575 |
assertEquals("References Match (Sheet 3)", titles3.getReference(), retrievedTitles3.getReference()); |
576 |
|
577 |
} |
578 |
|
112 |
} |
579 |
} |
113 |
|
580 |
|