01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: 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: package org.apache.poi.hssf.usermodel.examples;
19:
20: import org.apache.poi.hssf.usermodel.*;
21:
22: import java.io.FileOutputStream;
23: import java.io.IOException;
24:
25: /**
26: * Shows how various alignment options work.
27: *
28: * @author Glen Stampoultzis (glens at apache.org)
29: */
30: public class Alignment {
31: public static void main(String[] args) throws IOException {
32: HSSFWorkbook wb = new HSSFWorkbook();
33: HSSFSheet sheet = wb.createSheet("new sheet");
34: HSSFRow row = sheet.createRow((short) 2);
35: createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER);
36: createCell(wb, row, (short) 1,
37: HSSFCellStyle.ALIGN_CENTER_SELECTION);
38: createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL);
39: createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL);
40: createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY);
41: createCell(wb, row, (short) 5, HSSFCellStyle.ALIGN_LEFT);
42: createCell(wb, row, (short) 6, HSSFCellStyle.ALIGN_RIGHT);
43:
44: // Write the output to a file
45: FileOutputStream fileOut = new FileOutputStream("workbook.xls");
46: wb.write(fileOut);
47: fileOut.close();
48:
49: }
50:
51: /**
52: * Creates a cell and aligns it a certain way.
53: *
54: * @param wb the workbook
55: * @param row the row to create the cell in
56: * @param column the column number to create the cell in
57: * @param align the alignment for the cell.
58: */
59: private static void createCell(HSSFWorkbook wb, HSSFRow row,
60: short column, short align) {
61: HSSFCell cell = row.createCell(column);
62: cell.setCellValue("Align It");
63: HSSFCellStyle cellStyle = wb.createCellStyle();
64: cellStyle.setAlignment(align);
65: cell.setCellStyle(cellStyle);
66: }
67: }
|