001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.usermodel.examples;
019:
020: import org.apache.poi.hssf.usermodel.*;
021: import org.apache.poi.hssf.util.HSSFColor;
022:
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025:
026: /**
027: * Demonstrates many features of the user API at once. Used in the HOW-TO guide.
028: *
029: * @author Glen Stampoultzis (glens at apache.org)
030: * @author Andrew Oliver (acoliver at apache.org)
031: */
032: public class BigExample {
033: public static void main(String[] args) throws IOException {
034: short rownum;
035:
036: // create a new file
037: FileOutputStream out = new FileOutputStream("workbook.xls");
038: // create a new workbook
039: HSSFWorkbook wb = new HSSFWorkbook();
040: // create a new sheet
041: HSSFSheet s = wb.createSheet();
042: // declare a row object reference
043: HSSFRow r = null;
044: // declare a cell object reference
045: HSSFCell c = null;
046: // create 3 cell styles
047: HSSFCellStyle cs = wb.createCellStyle();
048: HSSFCellStyle cs2 = wb.createCellStyle();
049: HSSFCellStyle cs3 = wb.createCellStyle();
050: // create 2 fonts objects
051: HSSFFont f = wb.createFont();
052: HSSFFont f2 = wb.createFont();
053:
054: //set font 1 to 12 point type
055: f.setFontHeightInPoints((short) 12);
056: //make it red
057: f.setColor((short) HSSFColor.RED.index);
058: // make it bold
059: //arial is the default font
060: f.setBoldweight(f.BOLDWEIGHT_BOLD);
061:
062: //set font 2 to 10 point type
063: f2.setFontHeightInPoints((short) 10);
064: //make it the color at palette index 0xf (white)
065: f2.setColor((short) HSSFColor.WHITE.index);
066: //make it bold
067: f2.setBoldweight(f2.BOLDWEIGHT_BOLD);
068:
069: //set cell stlye
070: cs.setFont(f);
071: //set the cell format see HSSFDataFromat for a full list
072: cs.setDataFormat(HSSFDataFormat
073: .getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
074:
075: //set a thin border
076: cs2.setBorderBottom(cs2.BORDER_THIN);
077: //fill w fg fill color
078: cs2.setFillPattern((short) HSSFCellStyle.SOLID_FOREGROUND);
079: // set foreground fill to red
080: cs2.setFillForegroundColor((short) HSSFColor.RED.index);
081:
082: // set the font
083: cs2.setFont(f2);
084:
085: // set the sheet name to HSSF Test
086: wb.setSheetName(0, "HSSF Test");
087: // create a sheet with 300 rows (0-299)
088: for (rownum = (short) 0; rownum < 300; rownum++) {
089: // create a row
090: r = s.createRow(rownum);
091: // on every other row
092: if ((rownum % 2) == 0) {
093: // make the row height bigger (in twips - 1/20 of a point)
094: r.setHeight((short) 0x249);
095: }
096:
097: //r.setRowNum(( short ) rownum);
098: // create 50 cells (0-49) (the += 2 becomes apparent later
099: for (short cellnum = (short) 0; cellnum < 50; cellnum += 2) {
100: // create a numeric cell
101: c = r.createCell(cellnum);
102: // do some goofy math to demonstrate decimals
103: c
104: .setCellValue(rownum
105: * 10000
106: + cellnum
107: + (((double) rownum / 1000) + ((double) cellnum / 10000)));
108:
109: // on every other row
110: if ((rownum % 2) == 0) {
111: // set this cell to the first cell style we defined
112: c.setCellStyle(cs);
113: }
114:
115: // create a string cell (see why += 2 in the
116: c = r.createCell((short) (cellnum + 1));
117:
118: // set the cell's string value to "TEST"
119: c.setCellValue("TEST");
120: // make this column a bit wider
121: s.setColumnWidth((short) (cellnum + 1),
122: (short) ((50 * 8) / ((double) 1 / 20)));
123:
124: // on every other row
125: if ((rownum % 2) == 0) {
126: // set this to the white on red cell style
127: // we defined above
128: c.setCellStyle(cs2);
129: }
130:
131: }
132: }
133:
134: //draw a thick black border on the row at the bottom using BLANKS
135: // advance 2 rows
136: rownum++;
137: rownum++;
138:
139: r = s.createRow(rownum);
140:
141: // define the third style to be the default
142: // except with a thick black border at the bottom
143: cs3.setBorderBottom(cs3.BORDER_THICK);
144:
145: //create 50 cells
146: for (short cellnum = (short) 0; cellnum < 50; cellnum++) {
147: //create a blank type cell (no value)
148: c = r.createCell(cellnum);
149: // set it to the thick black border style
150: c.setCellStyle(cs3);
151: }
152:
153: //end draw thick black border
154:
155: // demonstrate adding/naming and deleting a sheet
156: // create a sheet, set its title then delete it
157: s = wb.createSheet();
158: wb.setSheetName(1, "DeletedSheet");
159: wb.removeSheetAt(1);
160: //end deleted sheet
161:
162: // write the workbook to the output stream
163: // close our file (don't blow out our file handles
164: wb.write(out);
165: out.close();
166: }
167: }
|