KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > examples > BigExample


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.hssf.usermodel.examples;
20
21 import org.apache.poi.hssf.usermodel.*;
22 import org.apache.poi.hssf.util.HSSFColor;
23
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 /**
28  * Demonstrates many features of the user API at once. Used in the HOW-TO guide.
29  *
30  * @author Glen Stampoultzis (glens at apache.org)
31  * @author Andrew Oliver (acoliver at apache.org)
32  */

33 public class BigExample
34 {
35     public static void main(String JavaDoc[] args)
36         throws IOException JavaDoc
37     {
38         short rownum;
39
40         // create a new file
41
FileOutputStream JavaDoc out = new FileOutputStream JavaDoc("workbook.xls");
42         // create a new workbook
43
HSSFWorkbook wb = new HSSFWorkbook();
44         // create a new sheet
45
HSSFSheet s = wb.createSheet();
46         // declare a row object reference
47
HSSFRow r = null;
48         // declare a cell object reference
49
HSSFCell c = null;
50         // create 3 cell styles
51
HSSFCellStyle cs = wb.createCellStyle();
52         HSSFCellStyle cs2 = wb.createCellStyle();
53         HSSFCellStyle cs3 = wb.createCellStyle();
54         // create 2 fonts objects
55
HSSFFont f = wb.createFont();
56         HSSFFont f2 = wb.createFont();
57
58         //set font 1 to 12 point type
59
f.setFontHeightInPoints((short) 12);
60         //make it red
61
f.setColor((short) HSSFColor.RED.index);
62         // make it bold
63
//arial is the default font
64
f.setBoldweight(f.BOLDWEIGHT_BOLD);
65
66         //set font 2 to 10 point type
67
f2.setFontHeightInPoints((short) 10);
68         //make it the color at palette index 0xf (white)
69
f2.setColor((short) HSSFColor.WHITE.index);
70         //make it bold
71
f2.setBoldweight(f2.BOLDWEIGHT_BOLD);
72
73         //set cell stlye
74
cs.setFont(f);
75         //set the cell format see HSSFDataFromat for a full list
76
cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
77
78         //set a thin border
79
cs2.setBorderBottom(cs2.BORDER_THIN);
80         //fill w fg fill color
81
cs2.setFillPattern((short) HSSFCellStyle.SOLID_FOREGROUND);
82         // set foreground fill to red
83
cs2.setFillForegroundColor((short) HSSFColor.RED.index);
84
85         // set the font
86
cs2.setFont(f2);
87
88         // set the sheet name to HSSF Test
89
wb.setSheetName(0, "HSSF Test");
90         // create a sheet with 300 rows (0-299)
91
for (rownum = (short) 0; rownum < 300; rownum++)
92         {
93             // create a row
94
r = s.createRow(rownum);
95             // on every other row
96
if ((rownum % 2) == 0)
97             {
98                 // make the row height bigger (in twips - 1/20 of a point)
99
r.setHeight((short) 0x249);
100             }
101
102             //r.setRowNum(( short ) rownum);
103
// create 50 cells (0-49) (the += 2 becomes apparent later
104
for (short cellnum = (short) 0; cellnum < 50; cellnum += 2)
105             {
106                 // create a numeric cell
107
c = r.createCell(cellnum);
108                 // do some goofy math to demonstrate decimals
109
c.setCellValue(rownum * 10000 + cellnum
110                         + (((double) rownum / 1000)
111                         + ((double) cellnum / 10000)));
112
113                 // on every other row
114
if ((rownum % 2) == 0)
115                 {
116                     // set this cell to the first cell style we defined
117
c.setCellStyle(cs);
118                 }
119
120                 // create a string cell (see why += 2 in the
121
c = r.createCell((short) (cellnum + 1));
122
123                 // set the cell's string value to "TEST"
124
c.setCellValue("TEST");
125                 // make this column a bit wider
126
s.setColumnWidth((short) (cellnum + 1), (short) ((50 * 8) / ((double) 1 / 20)));
127
128                 // on every other row
129
if ((rownum % 2) == 0)
130                 {
131                     // set this to the white on red cell style
132
// we defined above
133
c.setCellStyle(cs2);
134                 }
135
136             }
137         }
138
139         //draw a thick black border on the row at the bottom using BLANKS
140
// advance 2 rows
141
rownum++;
142         rownum++;
143
144         r = s.createRow(rownum);
145
146         // define the third style to be the default
147
// except with a thick black border at the bottom
148
cs3.setBorderBottom(cs3.BORDER_THICK);
149
150         //create 50 cells
151
for (short cellnum = (short) 0; cellnum < 50; cellnum++)
152         {
153             //create a blank type cell (no value)
154
c = r.createCell(cellnum);
155             // set it to the thick black border style
156
c.setCellStyle(cs3);
157         }
158
159         //end draw thick black border
160

161
162         // demonstrate adding/naming and deleting a sheet
163
// create a sheet, set its title then delete it
164
s = wb.createSheet();
165         wb.setSheetName(1, "DeletedSheet");
166         wb.removeSheetAt(1);
167         //end deleted sheet
168

169         // write the workbook to the output stream
170
// close our file (don't blow out our file handles
171
wb.write(out);
172         out.close();
173     }
174 }
175
Popular Tags