[Schmitzm-commits] r2414 - trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Wed Apr 8 23:07:16 CEST 2015


Author: mojays
Date: 2015-04-08 23:07:16 +0200 (Wed, 08 Apr 2015)
New Revision: 2414

Modified:
   trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/ExportUtil.java
Log:
ExportUtil: new method to createCellFormat(.), several autoSizeColumns(.) extended with optional column parameters, new autoSizeColumnsExcept(.)
 

Modified: trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/ExportUtil.java
===================================================================
--- trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/ExportUtil.java	2015-04-08 21:05:08 UTC (rev 2413)
+++ trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/ExportUtil.java	2015-04-08 21:07:16 UTC (rev 2414)
@@ -2,6 +2,7 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Arrays;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
@@ -12,9 +13,14 @@
 import jxl.CellView;
 import jxl.Sheet;
 import jxl.Workbook;
+import jxl.format.Alignment;
 import jxl.format.Border;
 import jxl.format.BorderLineStyle;
+import jxl.format.CellFormat;
 import jxl.format.Colour;
+import jxl.format.Orientation;
+import jxl.format.UnderlineStyle;
+import jxl.format.VerticalAlignment;
 import jxl.read.biff.BiffException;
 import jxl.write.Blank;
 import jxl.write.DateTime;
@@ -354,18 +360,90 @@
     }
     
     /**
-     * Resizes all columns of a sheet to fit the data. This might
+     * Creates a excel cell format. All parameters can be {@code null}. In this case
+     * they are taken from sample style.
+     * @param sampleFormat format used as default (can be {@code null})
+     * @param fontSize    font size
+     * @param bold        indicates bold / no-bold style
+     * @param italic      indicates italic / no-italic style
+     * @param underlined  indicates single-underlined / no-underlined style
+     * @param hAlign      horizontal text alignment
+     * @param vAlign      vertical text alignment
+     * @param orient      text orientation (angle)
+     */
+    public static WritableCellFormat createCellFormat(CellFormat sampleFormat, Integer fontSize, Boolean bold, Boolean italic, Boolean underlined, Alignment hAlign, VerticalAlignment vAlign, Orientation orient) throws WriteException {
+      if ( sampleFormat == null )
+        sampleFormat = WritableWorkbook.NORMAL_STYLE; //new Blank(0,0).getCellFormat();
+      WritableCellFormat cellFormat = new WritableCellFormat(sampleFormat);
+      
+      WritableFont cellFont = new WritableFont(cellFormat.getFont());
+      if ( fontSize != null )
+        cellFont.setPointSize(fontSize);
+      if ( bold != null )
+        cellFont.setBoldStyle( bold ? WritableFont.BOLD : WritableFont.NO_BOLD );
+      if ( italic != null )
+        cellFont.setItalic(italic);
+      if ( underlined != null )
+        cellFont.setUnderlineStyle( underlined ? UnderlineStyle.SINGLE : UnderlineStyle.NO_UNDERLINE );
+      cellFormat.setFont(cellFont);
+      
+      if ( vAlign != null )
+        cellFormat.setVerticalAlignment( vAlign );
+      if( hAlign != null )
+        cellFormat.setAlignment( hAlign );
+      if ( orient != null )
+        cellFormat.setOrientation( orient );
+     
+      return( cellFormat );
+    }
+
+    
+    /**
+     * Resizes a columns of a sheet to fit the data. This might
      * be very processor intensive for large files!
      */
-    public static void autoSizeColumns(WritableSheet sheet) {
+    public static void autoSizeColumn(WritableSheet sheet, int col, boolean autoSize) {
       // Workaround according to:
       //   http://stackoverflow.com/questions/1665391/jxl-cell-formating
+      CellView view = sheet.getColumnView(col);
+      view.setAutosize(autoSize);
+      sheet.setColumnView(col, view);
+    }
+
+    
+    /**
+     * Resizes several columns of a sheet to fit the data. This might
+     * be very processor intensive for large files! If no columns are specified,
+     * all columns are adjusted.
+     */
+    public static void autoSizeColumns(WritableSheet sheet, int... col) {
+      if ( col.length == 0 ) {
+        int colCount = sheet.getColumns();
+        for (int c=0; c<colCount; c++)
+          autoSizeColumn(sheet, c, true);
+      } else {
+        for (int i=0; i<col.length; i++)
+          autoSizeColumn(sheet, col[i], true);
+      }
+    }
+
+    /**
+     * Resizes all columns of a sheet to fit the data EXCEPT the specified ones.
+     * This might be very processor intensive for large files!
+     */
+    public static void autoSizeColumnsExcept(WritableSheet sheet, int... col) {
+      // Sort specified columns
+      Arrays.sort(col);
+      
       int colCount = sheet.getColumns();
       for (int c=0; c<colCount; c++) {
-        CellView view = sheet.getColumnView(c);
-        view.setAutosize(true);
-        sheet.setColumnView(c, view);
+        if ( Arrays.binarySearch(col, c) >= 0 )
+          continue;
+        autoSizeColumn(sheet, c, true);
       }
+      
+      
+      
     }
     
 }



More information about the Schmitzm-commits mailing list