1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/*
* RangeTestTest.java
* NetBeans JUnit based test
*
* Created on April 21, 2002, 6:23 PM
*/
package org.apache.poi.hssf.usermodel;
import junit.framework.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
*
* @author ROMANL
* @author Andrew C. Oliver (acoliver at apache dot org)
*/
public class TestNamedRange
extends TestCase {
public TestNamedRange(String testName) {
super(testName);
}
public static void main(java.lang.String[] args) {
String filename = System.getProperty("HSSF.testdata.path");
// assume andy is running this in the debugger
if (filename == null)
{
if (args != null && args.length == 1) {
System.setProperty(
"HSSF.testdata.path",
args[0]);
} else {
System.err.println("Geesh, no HSSF.testdata.path system " +
"property, no command line arg with the path "+
"what do you expect me to do, guess where teh data " +
"files are? Sorry, I give up!");
}
}
junit.textui.TestRunner.run(TestNamedRange.class);
}
/** Test of TestCase method, of class test.RangeTest. */
public void testNamedRange()
throws IOException
{
FileInputStream fis = null;
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
String filename = System.getProperty("HSSF.testdata.path");
filename = filename + "/Simple.xls";
fis = new FileInputStream(filename);
fs = new POIFSFileSystem(fis);
wb = new HSSFWorkbook(fs);
//Creating new Named Range
HSSFName newNamedRange = wb.createName();
//Getting Sheet Name for the reference
String sheetName = wb.getSheetName(0);
//Setting its name
newNamedRange.setNameName("RangeTest");
//Setting its reference
newNamedRange.setReference(sheetName + ".$D$4:$E$8");
//Getting NAmed Range
HSSFName namedRange1 = wb.getNameAt(0);
//Getting it sheet name
sheetName = namedRange1.getSheetName();
//Getting its reference
String referece = namedRange1.getReference();
// sanity check
SanityChecker c = new SanityChecker();
c.checkHSSFWorkbook(wb);
File file = File.createTempFile("testNamedRange",
".xls");
FileOutputStream fileOut = new FileOutputStream(file);
wb.write(fileOut);
fis.close();
fileOut.close();
assertTrue("file exists",file.exists());
FileInputStream in = new FileInputStream(file);
wb = new HSSFWorkbook(in);
HSSFName nm =wb.getNameAt(wb.getNameIndex("RangeTest"));
assertTrue("Name is "+nm.getNameName(),"RangeTest".equals(nm.getNameName()));
assertTrue("Reference is "+nm.getReference(),(wb.getSheetName(0)+"!$D$4:$E$8").equals(nm.getReference()));
}
}
|