]> source.dussan.org Git - poi.git/commitdiff
Moved from xssf\usermodel examples following Christian's modification to that example.
authorMark Beardsley <msb@apache.org>
Thu, 27 May 2010 08:07:36 +0000 (08:07 +0000)
committerMark Beardsley <msb@apache.org>
Thu, 27 May 2010 08:07:36 +0000 (08:07 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@948710 13f79535-47bb-0310-9956-ffa450edef68

src/examples/src/org/apache/poi/ss/examples/AligningCells.java [new file with mode: 0644]

diff --git a/src/examples/src/org/apache/poi/ss/examples/AligningCells.java b/src/examples/src/org/apache/poi/ss/examples/AligningCells.java
new file mode 100644 (file)
index 0000000..6571ff9
--- /dev/null
@@ -0,0 +1,72 @@
+/* ====================================================================\r
+Licensed to the Apache Software Foundation (ASF) under one or more\r
+contributor license agreements.  See the NOTICE file distributed with\r
+this work for additional information regarding copyright ownership.\r
+The ASF licenses this file to You under the Apache License, Version 2.0\r
+(the "License"); you may not use this file except in compliance with\r
+the License.  You may obtain a copy of the License at\r
+\r
+http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+Unless required by applicable law or agreed to in writing, software\r
+distributed under the License is distributed on an "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+See the License for the specific language governing permissions and\r
+limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.ss.examples;\r
+\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+\r
+import org.apache.poi.ss.usermodel.*;\r
+import org.apache.poi.xssf.usermodel.*;\r
+\r
+/**\r
+ * Shows how various alignment options work.\r
+ */\r
+public class AligningCells {\r
+\r
+    public static void main(String[] args) throws IOException {\r
+        Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();\r
+\r
+        Sheet sheet = wb.createSheet();\r
+        Row row = sheet.createRow((short) 2);\r
+        row.setHeightInPoints(30);\r
+        for (int i = 0; i < 8; i++) {\r
+            //column width is set in units of 1/256th of a character width\r
+            sheet.setColumnWidth(i, 256 * 15);\r
+        }\r
+\r
+        createCell(wb, row, (short) 0, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM);\r
+        createCell(wb, row, (short) 1, CellStyle.ALIGN_CENTER_SELECTION, CellStyle.VERTICAL_BOTTOM);\r
+        createCell(wb, row, (short) 2, CellStyle.ALIGN_FILL, CellStyle.VERTICAL_CENTER);\r
+        createCell(wb, row, (short) 3, CellStyle.ALIGN_GENERAL, CellStyle.VERTICAL_CENTER);\r
+        createCell(wb, row, (short) 4, CellStyle.ALIGN_JUSTIFY, CellStyle.VERTICAL_JUSTIFY);\r
+        createCell(wb, row, (short) 5, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_TOP);\r
+        createCell(wb, row, (short) 6, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_TOP);\r
+\r
+        // Write the output to a file\r
+        FileOutputStream fileOut = new FileOutputStream("ss-example-align.xlsx");\r
+        wb.write(fileOut);\r
+        fileOut.close();\r
+    }\r
+\r
+    /**\r
+     * Creates a cell and aligns it a certain way.\r
+     *\r
+     * @param wb     the workbook\r
+     * @param row    the row to create the cell in\r
+     * @param column the column number to create the cell in\r
+     * @param halign the horizontal alignment for the cell.\r
+     */\r
+    private static void createCell(Workbook wb, Row row, short column, short halign, short valign) {\r
+        CreationHelper ch = wb.getCreationHelper();\r
+        Cell cell = row.createCell(column);\r
+        cell.setCellValue(ch.createRichTextString("Align It"));\r
+        CellStyle cellStyle = wb.createCellStyle();\r
+        cellStyle.setAlignment(halign);\r
+        cellStyle.setVerticalAlignment(valign);\r
+        cell.setCellStyle(cellStyle);\r
+    }\r
+}
\ No newline at end of file