KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 /**
27  * Demonstrates how to use newlines in cells.
28  *
29  * @author Glen Stampoultzis (glens at apache.org)
30  * @author Fauzia Lala <fauzia.lala at wcom.com>
31  */

32 public class NewLinesInCells
33 {
34     public static void main( String JavaDoc[] args ) throws IOException JavaDoc
35     {
36
37         HSSFWorkbook wb = new HSSFWorkbook();
38         HSSFSheet s = wb.createSheet();
39         HSSFRow r = null;
40         HSSFCell c = null;
41         HSSFCellStyle cs = wb.createCellStyle();
42         HSSFFont f = wb.createFont();
43         HSSFFont f2 = wb.createFont();
44
45         cs = wb.createCellStyle();
46
47         cs.setFont( f2 );
48         //Word Wrap MUST be turned on
49
cs.setWrapText( true );
50
51         r = s.createRow( (short) 2 );
52         r.setHeight( (short) 0x349 );
53         c = r.createCell( (short) 2 );
54         c.setCellType( HSSFCell.CELL_TYPE_STRING );
55         c.setCellValue( "Use \n with word wrap on to create a new line" );
56         c.setCellStyle( cs );
57         s.setColumnWidth( (short) 2, (short) ( ( 50 * 8 ) / ( (double) 1 / 20 ) ) );
58
59         FileOutputStream JavaDoc fileOut = new FileOutputStream JavaDoc( "workbook.xls" );
60         wb.write( fileOut );
61         fileOut.close();
62
63     }
64 }
65
Popular Tags