]> source.dussan.org Git - poi.git/commitdiff
Example AligningCells: Use int instead of short for column-index, closes Github #35
authorDominik Stadler <centic@apache.org>
Mon, 7 Nov 2016 20:50:10 +0000 (20:50 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 7 Nov 2016 20:50:10 +0000 (20:50 +0000)
Also use more of the base classes where possible

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1768584 13f79535-47bb-0310-9956-ffa450edef68

src/examples/src/org/apache/poi/ss/examples/AligningCells.java
src/examples/src/org/apache/poi/xssf/usermodel/examples/AligningCells.java

index fa8619ae171fcafe9ebf2863af93216c07f66344..8ee6440fdc04f5ee38ee0c4c8677cbf793ce05a8 100644 (file)
@@ -18,9 +18,10 @@ package org.apache.poi.ss.examples;
 \r
 import java.io.FileOutputStream;\r
 import java.io.IOException;\r
+import java.io.OutputStream;\r
 \r
 import org.apache.poi.ss.usermodel.*;\r
-import org.apache.poi.xssf.usermodel.*;\r
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
 \r
 /**\r
  * Shows how various alignment options work.\r
@@ -31,25 +32,26 @@ public class AligningCells {
         Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();\r
 \r
         Sheet sheet = wb.createSheet();\r
-        Row row = sheet.createRow((short) 2);\r
+        Row row = sheet.createRow(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, HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM);\r
-        createCell(wb, row, (short) 1, HorizontalAlignment.CENTER_SELECTION, VerticalAlignment.BOTTOM);\r
-        createCell(wb, row, (short) 2, HorizontalAlignment.FILL, VerticalAlignment.CENTER);\r
-        createCell(wb, row, (short) 3, HorizontalAlignment.GENERAL, VerticalAlignment.CENTER);\r
-        createCell(wb, row, (short) 4, HorizontalAlignment.JUSTIFY, VerticalAlignment.JUSTIFY);\r
-        createCell(wb, row, (short) 5, HorizontalAlignment.LEFT, VerticalAlignment.TOP);\r
-        createCell(wb, row, (short) 6, HorizontalAlignment.RIGHT, VerticalAlignment.TOP);\r
+        createCell(wb, row, 0, HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM);\r
+        createCell(wb, row, 1, HorizontalAlignment.CENTER_SELECTION, VerticalAlignment.BOTTOM);\r
+        createCell(wb, row, 2, HorizontalAlignment.FILL, VerticalAlignment.CENTER);\r
+        createCell(wb, row, 3, HorizontalAlignment.GENERAL, VerticalAlignment.CENTER);\r
+        createCell(wb, row, 4, HorizontalAlignment.JUSTIFY, VerticalAlignment.JUSTIFY);\r
+        createCell(wb, row, 5, HorizontalAlignment.LEFT, VerticalAlignment.TOP);\r
+        createCell(wb, row, 6, HorizontalAlignment.RIGHT, VerticalAlignment.TOP);\r
 \r
         // Write the output to a file\r
-        FileOutputStream fileOut = new FileOutputStream("ss-example-align.xlsx");\r
+        OutputStream fileOut = new FileOutputStream("ss-example-align.xlsx");\r
         wb.write(fileOut);\r
         fileOut.close();\r
+\r
         wb.close();\r
     }\r
 \r
@@ -61,7 +63,7 @@ public class AligningCells {
      * @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, HorizontalAlignment halign, VerticalAlignment valign) {\r
+    private static void createCell(Workbook wb, Row row, int column, HorizontalAlignment halign, VerticalAlignment valign) {\r
         CreationHelper ch = wb.getCreationHelper();\r
         Cell cell = row.createCell(column);\r
         cell.setCellValue(ch.createRichTextString("Align It"));\r
index b210c1301c70f77a9618a1569c07a9871bb13e6e..0bb002fb3c7ea45145242250fee689b5168fd13f 100644 (file)
@@ -18,15 +18,16 @@ package org.apache.poi.xssf.usermodel.examples;
 
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.CreationHelper;
 import org.apache.poi.ss.usermodel.HorizontalAlignment;
 import org.apache.poi.ss.usermodel.VerticalAlignment;
 import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFCellStyle;
-import org.apache.poi.xssf.usermodel.XSSFRichTextString;
 import org.apache.poi.xssf.usermodel.XSSFRow;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@@ -47,27 +48,27 @@ public class AligningCells {
         XSSFWorkbook wb = new XSSFWorkbook();
 
         XSSFSheet sheet = wb.createSheet();
-        XSSFRow row = sheet.createRow((short) 2);
+        XSSFRow row = sheet.createRow(2);
         row.setHeightInPoints(30);
         for (int i = 0; i < 8; i++) {
             //column width is set in units of 1/256th of a character width
             sheet.setColumnWidth(i, 256 * 15);
         }
 
-        createCell(wb, row, (short) 0, HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM);
-        createCell(wb, row, (short) 1, HorizontalAlignment.CENTER_SELECTION, VerticalAlignment.BOTTOM);
-        createCell(wb, row, (short) 2, HorizontalAlignment.FILL, VerticalAlignment.CENTER);
-        createCell(wb, row, (short) 3, HorizontalAlignment.GENERAL, VerticalAlignment.CENTER);
-        createCell(wb, row, (short) 4, HorizontalAlignment.JUSTIFY, VerticalAlignment.JUSTIFY);
-        createCell(wb, row, (short) 5, HorizontalAlignment.LEFT, VerticalAlignment.TOP);
-        createCell(wb, row, (short) 6, HorizontalAlignment.RIGHT, VerticalAlignment.TOP);
+        createCell(wb, row, 0, HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM);
+        createCell(wb, row, 1, HorizontalAlignment.CENTER_SELECTION, VerticalAlignment.BOTTOM);
+        createCell(wb, row, 2, HorizontalAlignment.FILL, VerticalAlignment.CENTER);
+        createCell(wb, row, 3, HorizontalAlignment.GENERAL, VerticalAlignment.CENTER);
+        createCell(wb, row, 4, HorizontalAlignment.JUSTIFY, VerticalAlignment.JUSTIFY);
+        createCell(wb, row, 5, HorizontalAlignment.LEFT, VerticalAlignment.TOP);
+        createCell(wb, row, 6, HorizontalAlignment.RIGHT, VerticalAlignment.TOP);
 
         //center text over B4, C4, D4
-        row = sheet.createRow((short) 3);
-        centerAcrossSelection(wb, row, (short) 1, (short) 3, VerticalAlignment.CENTER);
+        row = sheet.createRow(3);
+        centerAcrossSelection(wb, row, 1, 3, VerticalAlignment.CENTER);
 
         // Write the output to a file
-        FileOutputStream fileOut = new FileOutputStream("xssf-align.xlsx");
+        OutputStream fileOut = new FileOutputStream("xssf-align.xlsx");
         wb.write(fileOut);
         fileOut.close();
         
@@ -82,10 +83,11 @@ public class AligningCells {
      * @param column the column number to create the cell in
      * @param halign the horizontal alignment for the cell.
      */
-    private static void createCell(XSSFWorkbook wb, XSSFRow row, short column,
-            HorizontalAlignment halign, VerticalAlignment valign) {
+    private static void createCell(XSSFWorkbook wb, XSSFRow row, int column,
+                                   HorizontalAlignment halign, VerticalAlignment valign) {
+        CreationHelper ch = wb.getCreationHelper();
         XSSFCell cell = row.createCell(column);
-        cell.setCellValue(new XSSFRichTextString("Align It"));
+        cell.setCellValue(ch.createRichTextString("Align It"));
         CellStyle cellStyle = wb.createCellStyle();
         cellStyle.setAlignment(halign);
         cellStyle.setVerticalAlignment(valign);
@@ -100,11 +102,10 @@ public class AligningCells {
      * @param start_column  the column number to create the cell in and where the selection starts
      * @param end_column    the column number where the selection ends
      * @param valign the horizontal alignment for the cell.
-     *
-     * @author Cristian Petrula, Romania
      */
     private static void centerAcrossSelection(XSSFWorkbook wb, XSSFRow row,
-            short start_column, short end_column, VerticalAlignment valign) {
+            int start_column, int end_column, VerticalAlignment valign) {
+        CreationHelper ch = wb.getCreationHelper();
 
         // Create cell style with ALIGN_CENTER_SELECTION
         XSSFCellStyle cellStyle = wb.createCellStyle();
@@ -119,7 +120,7 @@ public class AligningCells {
 
         // Set value to the first cell
         XSSFCell cell = row.getCell(start_column);
-        cell.setCellValue(new XSSFRichTextString("Align It"));
+        cell.setCellValue(ch.createRichTextString("Align It"));
 
         // Make the selection
         CTRowImpl ctRow = (CTRowImpl) row.getCTRow();