[Schmitzm-commits] r1957 - in trunk/schmitzm-excelcsv/src/main/java/de/schmitzm: . jxl jxl/export

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Wed Apr 18 12:01:44 CEST 2012


Author: mojays
Date: 2012-04-18 12:01:44 +0200 (Wed, 18 Apr 2012)
New Revision: 1957

Added:
   trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/
   trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/
   trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/CellValueConverter.java
   trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/DefaultCellValueConverter.java
   trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/ExportUtil.java
   trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/LineExporter.java
Log:
JXL utility methods added (from WIME)

Added: trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/CellValueConverter.java
===================================================================
--- trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/CellValueConverter.java	                        (rev 0)
+++ trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/CellValueConverter.java	2012-04-18 10:01:44 UTC (rev 1957)
@@ -0,0 +1,24 @@
+package de.schmitzm.jxl.export;
+
+import jxl.write.WritableCellFormat;
+
+/**
+ * Interface to convert a cell value before export to excel cell.
+ * @see ExportUtil#createExcelCell(int, int, Object, String)
+ * @see ExportUtil#exportToExcel(java.io.File, String, java.util.List, LineExporter, CellValueConverter, javax.swing.JProgressBar)
+ */
+public interface CellValueConverter {
+  /**
+   * Converts a cell value.
+   */
+  public Object convertCellValue(Object value, String columnName);
+
+  /**
+   * Returns a cell format for a cell.
+   * @param value cell value to be exported
+   * @param columnName column name
+   * @return {@code null} to indicate that the default cell format should
+   *         be used
+   */
+  public WritableCellFormat getCellFormat(Object value, String columnName);
+}
\ No newline at end of file

Added: trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/DefaultCellValueConverter.java
===================================================================
--- trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/DefaultCellValueConverter.java	                        (rev 0)
+++ trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/DefaultCellValueConverter.java	2012-04-18 10:01:44 UTC (rev 1957)
@@ -0,0 +1,31 @@
+package de.schmitzm.jxl.export;
+
+import jxl.write.WritableCellFormat;
+
+/**
+ * Default implementation of {@link CellValueConverter}.
+ * @see ExportUtil#createExcelCell(int, int, Object, String)
+ * @see ExportUtil#exportToExcel(java.io.File, String, java.util.List, LineExporter, CellValueConverter, javax.swing.JProgressBar)
+ */
+public class DefaultCellValueConverter {
+  /**
+   * Converts a cell value. This implementation always returns
+   * the original {@code value}.
+   * @return always {@code value}
+   */
+  public Object convertCellValue(Object value, String columnName) {
+    return value;
+  }
+
+  /**
+   * Returns a cell format for a cell. This implementation always
+   * returns {@code null}.
+   * @param value cell value to be exported
+   * @param columnName column name
+   * @return {@code null} to indicate that the default cell format should
+   *         be used
+   */
+  public WritableCellFormat getCellFormat(Object value, String columnName) {
+    return null;
+  }
+}
\ No newline at end of file

Added: trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/ExportUtil.java
===================================================================
--- trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/ExportUtil.java	                        (rev 0)
+++ trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/ExportUtil.java	2012-04-18 10:01:44 UTC (rev 1957)
@@ -0,0 +1,233 @@
+package de.schmitzm.jxl.export;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.swing.JProgressBar;
+
+import jxl.CellView;
+import jxl.Workbook;
+import jxl.format.Border;
+import jxl.format.BorderLineStyle;
+import jxl.format.Colour;
+import jxl.write.Blank;
+import jxl.write.DateTime;
+import jxl.write.Label;
+import jxl.write.NumberFormats;
+import jxl.write.WritableCell;
+import jxl.write.WritableCellFormat;
+import jxl.write.WritableFont;
+import jxl.write.WritableSheet;
+import jxl.write.WritableWorkbook;
+import jxl.write.WriteException;
+import jxl.write.biff.RowsExceededException;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.Logger;
+
+import de.schmitzm.io.IOUtil;
+import de.schmitzm.lang.LangUtil;
+import de.schmitzm.swing.SwingUtil;
+import de.schmitzm.temp.BaseTypeUtil;
+
+/**
+ * Utility methods for exporting data.
+ * 
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public class ExportUtil {
+    /** JXL format for integer cells. */
+    private static final WritableCellFormat DEFAULT_CELL_FORMAT_INT = new WritableCellFormat(NumberFormats.THOUSANDS_INTEGER);
+    /** JXL format for decimal cells. */
+    private static final WritableCellFormat DEFAULT_CELL_FORMAT_DECIMAL = new WritableCellFormat(NumberFormats.THOUSANDS_FLOAT);
+    
+    /** Internal variable for JXL format for integer cells (recreated for each export!). */
+    private static WritableCellFormat CELL_FORMAT_INT = null;
+    /** Internal variable for JXL format for decimal cells (recreated for each export!). */
+    private static WritableCellFormat CELL_FORMAT_DECIMAL = null;
+  
+//    /** "Mitglieds-Nummer" is exported as String (9an) with leading zeros. */ 
+//    public static final NumberFormat EXPORT_FORMAT_MITGL_NR = new DecimalFormat("000000000");
+//    /** "PLZ" is exported as String (5an) with leading zeros. */ 
+//    public static final NumberFormat EXPORT_FORMAT_PLZ = new DecimalFormat("00000");
+    
+    public static final Logger LOGGER = LangUtil.createLogger(ExportUtil.class);
+
+
+    ////////////////////////////////////////////////////////////////////
+    //////////////  Helper methods for EXCEL export  ///////////////////
+    ////////////////////////////////////////////////////////////////////
+
+    /**
+     * Exports data of the list an Excel file.
+     * @param exportFile destination file
+     * @param dataList list of data to export
+     * @param exporter handles the export of one item of {@code dataList}
+     * @param cellConverter converts a cell value before creating the
+     *                      excel cell (can be {@code null})
+//     * @param evictAfterExport indicates whether contact is released from database
+//     *                         after line is exported 
+     * @return number of exported contacts
+     */
+    public static <E> int exportToExcelFile(File exportFile, String sheetName, List<E> dataList, LineExporter<E> exporter, CellValueConverter cellConverter/*, boolean evictAfterExport*/, JProgressBar progressBar) throws IOException {
+      // Initialisieren der JXL Formate (irgendwie gibt es Probleme, wenn diese nicht fuer jeden
+      // Export neu erzeugt werden?!)
+      CELL_FORMAT_DECIMAL  = new WritableCellFormat( DEFAULT_CELL_FORMAT_DECIMAL );
+      CELL_FORMAT_INT      = new WritableCellFormat( DEFAULT_CELL_FORMAT_INT );
+//      CELL_FORMAT_MITGL_NR = new WritableCellFormat( DEFAULT_CELL_FORMAT_MITGL_NR );    	
+//      CELL_FORMAT_PLZ      = new WritableCellFormat( DEFAULT_CELL_FORMAT_PLZ );
+      
+      if ( !exportFile.exists() && StringUtils.isBlank(IOUtil.getFileExt(exportFile)) )
+        exportFile = IOUtil.appendFileExt(exportFile, "xls");
+      SwingUtil.setProgressBarDeterminateOnEDT(progressBar, dataList.size());
+//      Session session = HibernateSessionFactory.getSession();
+      int expCount = 0;
+      WritableWorkbook workbook = null;
+      WritableSheet sheet = null;
+      try {
+        // Create cell format for header cells (derive from
+        // default label cell)
+        WritableCellFormat headerCellFormat = new WritableCellFormat(new Blank(0,0).getCellFormat());
+        WritableFont       headerFont       = new WritableFont(headerCellFormat.getFont());
+        headerFont.setBoldStyle(WritableFont.BOLD);
+        headerCellFormat.setFont(headerFont);
+        headerCellFormat.setBackground(Colour.SKY_BLUE);
+        headerCellFormat.setBorder(Border.BOTTOM, BorderLineStyle.THIN);
+//        headerCellFormat.setShrinkToFit(true);
+        
+        // Create workbook
+        workbook = Workbook.createWorkbook(exportFile);
+        sheet = workbook.createSheet(sheetName,0);
+        sheet.getSettings().setVerticalFreeze(1);
+        sheet.getSettings().setPrintTitlesRow(0, 0);
+        // Create header line
+        List<String> header = exporter.getHeaderColumns();
+        for (int col=0; col<header.size(); col++) {
+          Label cell = new Label(col,0,header.get(col));
+          cell.setCellFormat(headerCellFormat);
+          sheet.addCell(cell);
+        }
+        // Write a line for each contact
+        Map<String,Object> map = new HashMap<String, Object>();
+        int row = 0;
+        for (E data : dataList) {
+          row++;
+          // Let contact instance fill prepared map
+          map.clear();
+//          session.refresh(data);
+          exporter.exportTo(data,map);
+          // put values in excel cells
+          for (int col=0; col<header.size(); col++) {
+            String key = header.get(col);
+			Object value = map.get(key);
+            if ( value == null )
+              continue;
+            WritableCell cell = createExcelCell(col, row, value, key, cellConverter);
+            sheet.addCell(cell);
+          }
+//          // release contact
+//          if ( evictAfterExport )
+//            session.evict(data);
+
+          expCount++;
+          SwingUtil.updateProgressBarValueOnEDT(progressBar, expCount);
+        }
+        // for "small" sheets, adjust the column width
+        if ( sheet.getColumns() <= 20 )
+          autoSizeColumns(sheet);
+      } catch (RowsExceededException err) {
+        throw new IOException(err);
+      } catch (WriteException err) {
+        throw new IOException(err);
+      } finally {
+        if ( workbook != null )
+          try {
+            workbook.write();
+            workbook.close();
+          } catch (WriteException err) {
+            throw new IOException(err);
+          }
+      }
+      return expCount;
+    }
+
+    /**
+     * Creates a adequate {@link WritableCell} for an object.
+     * @param col excel column to create the cell for
+     * @param row excel column to create the cell for 
+     * @param value value to create the cell for
+     * @param columnName name/key/id of column (piped to {@link CellValueConverter})
+     * @param cellConverter converts a cell value before creating the
+     *                      excel cell (can be {@code null})
+     * @return {@code BlankCell} if {@code null} value is given
+     */
+    public static WritableCell createExcelCell(int col, int row, Object value, String columnName, CellValueConverter cellConverter) {
+      WritableCellFormat numberCellFormat = null;
+//      // Sonderfall: Mitgliedsnummer mit Excel-Format 9-stellig mit fuehrenden Nullen
+//      if ( columnName.endsWith("Mitgl_Nr") && value instanceof Number )
+//    	  numberCellFormat = CELL_FORMAT_MITGL_NR;
+//      // Sonderfall: PLZ mit Excel-Format 5-stellig mit fuehrenden Nullen
+//      if ( columnName.endsWith("PLZ") && value instanceof Number )
+//    	  numberCellFormat = CELL_FORMAT_PLZ;
+//      // Instead of BasicType the description is exported
+//      if ( value instanceof BasicType )
+//        value = ((BasicType)value).getBezeichnung();
+      if ( cellConverter != null ) {
+        numberCellFormat = cellConverter.getCellFormat(value, columnName);
+        value = cellConverter.convertCellValue(value, columnName);
+      }    	
+      if ( value == null )
+        return new Blank(col, row);
+
+      // Create adaquate cell for value type
+      if ( value instanceof String )
+        return new Label(col, row, (String)value);
+      if ( value instanceof Date ) {
+        // Excel does not handle timezones, so it will always show the exported
+        // date in GMT (the Java internal format)
+        // To avoid that we convert the date, that GMT represents the day and time!
+        Date date = LangUtil.getGmtDate((Date)value); 
+        return new DateTime(col, row, date );
+      }
+      if ( value instanceof Number ) {
+        jxl.write.Number number = new jxl.write.Number(col, row, ((Number)value).doubleValue());
+        // Default format
+        if ( numberCellFormat == null ) {
+        	if ( BaseTypeUtil.isDecimal(value) )
+        		numberCellFormat = CELL_FORMAT_DECIMAL;
+        	else
+        		numberCellFormat = CELL_FORMAT_INT;
+        }
+        // Apply cell format for number
+        number.setCellFormat( numberCellFormat );
+        return number;
+      }
+      if ( value instanceof Boolean )
+        return new jxl.write.Boolean(col, row, (Boolean)value);
+
+      // Other classes are simply converted to string (with warning)
+      String errorIdent = "Zeile "+(row+1)+", Spalte "+(col+1);
+      errorIdent += ": ";
+      LOGGER.warn(errorIdent+LangUtil.getSimpleClassName(value)+" wird naiv in String '"+value.toString()+"' konvertiert");
+      return new Label(col, row, value.toString());
+    }
+    
+    /**
+     * Resizes all columns of a sheet to fit the data. This might
+     * be very processor intensive for large files!
+     */
+    public static void autoSizeColumns(WritableSheet sheet) {
+      // Workaround according to:
+      //   http://stackoverflow.com/questions/1665391/jxl-cell-formating
+      int colCount = sheet.getColumns();
+      for (int c=0; c<colCount; c++) {
+        CellView view = sheet.getColumnView(c);
+        view.setAutosize(true);
+        sheet.setColumnView(c, view);
+      }
+    }
+}

Added: trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/LineExporter.java
===================================================================
--- trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/LineExporter.java	                        (rev 0)
+++ trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/jxl/export/LineExporter.java	2012-04-18 10:01:44 UTC (rev 1957)
@@ -0,0 +1,23 @@
+package de.schmitzm.jxl.export;
+
+import java.io.File;
+import java.util.List;
+import java.util.Map;
+
+import javax.swing.JProgressBar;
+
+/**
+ * Interface to generically provide informations to
+ * {@link ExportUtil#exportToExcel(File, String, List, LineExporter, boolean, JProgressBar)}.
+ */
+public interface LineExporter<E> {
+  /**
+   * Returns the columns to export.
+   */
+  public List<String> getHeaderColumns();
+  /**
+   * Provides putting the export values to the given
+   * map.
+   */
+  public void exportTo(E data, Map<String,Object> map);
+}
\ No newline at end of file



More information about the Schmitzm-commits mailing list