Changeset 7649


Ignore:
Timestamp:
Mar 14, 2019, 7:52:48 AM (4 years ago)
Author:
Nicklas Nordborg
Message:

References #2160: Table exporter should support exporting to Excel files

Added support for Excel to the MultiFormatter and BooleanFormatter.

Location:
trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/clients/web/net/sf/basedb/clients/web/plugins/XlsxTemplate.java

    r7646 r7649  
    188188      ev = ExcelValue.asString(ep.formatter.format(data));
    189189    }
    190     if (ev == null) ev = ExcelValue.EMPTY_CELL;
     190    if (ev == null) ev = ExcelValue.emptyCell();
    191191    ev.writeTo(cell, styleCreator);
    192192  }
  • trunk/src/core/net/sf/basedb/util/excel/ExcelValue.java

    r7647 r7649  
    2020{
    2121
     22  // Some static cells
     23  private static final ExcelValue<Void> EMPTY_CELL = new BlankValue<>();
     24  private static final ExcelValue<Boolean> TRUE = new BooleanValue(true);
     25  private static final ExcelValue<Boolean> FALSE = new BooleanValue(false);
     26 
    2227  /**
    2328    This creates a blank cell.
    2429  */
    25   public static final ExcelValue<Void> EMPTY_CELL = new BlankValue<>();
     30  @SuppressWarnings("unchecked")
     31  public static final <T> ExcelValue<T> emptyCell()
     32  {
     33    return (ExcelValue<T>)EMPTY_CELL;
     34  }
    2635 
    2736  /**
     
    3039  public static ExcelValue<String> asString(String value)
    3140  {
     41    if (value == null) return emptyCell();
    3242    return new StringValue(value);
    3343  }
     
    4050  public static ExcelValue<Date> asDate(Date value, String javaFormat)
    4151  {
     52    if (value == null) return emptyCell();
    4253    if (javaFormat == null) javaFormat = "yyyy-MM-dd";
    4354    return new DateValue(value, javaFormat);
     
    5364  public static ExcelValue<Date> asTimestamp(Date value, String javaFormat)
    5465  {
     66    if (value == null) return emptyCell();
    5567    if (javaFormat == null) javaFormat = "yyyy-MM-dd HH:mm:ss";
    5668    return new DateValue(value, javaFormat);
     
    6476  public static final ExcelValue<Number> asInt(Number value)
    6577  {
     78    if (value == null) return emptyCell();
    6679    return new IntValue(value);
    6780  }
     
    7487  public static final ExcelValue<Number> asNumber(Number value, int numDecimals)
    7588  {
     89    if (value == null) return emptyCell();
    7690    return new NumericValue(value, numberFormat(numDecimals));
    7791  }
     
    8296  public static final ExcelValue<Number> asNumber(Number value, String format)
    8397  {
     98    if (value == null) return emptyCell();
    8499    return new NumericValue(value, format);
     100  }
     101 
     102  /**
     103    Create a numeric cell value with boolean format.
     104  */
     105  public static final ExcelValue<Boolean> asBoolean(Boolean value)
     106  {
     107    if (value == null) return emptyCell();
     108    return value ? TRUE : FALSE;
    85109  }
    86110 
     
    206230    public void writeTo(Cell cell, CellStyleCreator styleCreator)
    207231    {
    208       if (value == null)
    209       {
    210         EMPTY_CELL.writeTo(cell, styleCreator);
    211       }
    212       else
    213       {
    214         cell.setCellValue(value.doubleValue());
    215       }
     232      cell.setCellValue(value.doubleValue());
    216233      cell.setCellStyle(styleCreator.getIntCellStyle());
    217     }
    218    
    219   }
    220 
     234    }   
     235  }
    221236 
    222237  /**
     
    234249    public void writeTo(Cell cell, CellStyleCreator styleCreator)
    235250    {
    236       if (value == null)
    237       {
    238         EMPTY_CELL.writeTo(cell, styleCreator);
    239       }
    240       else
    241       {
    242         cell.setCellValue(value.doubleValue());
    243       }
     251      cell.setCellValue(value.doubleValue());
    244252      if (format != null)
    245253      {
     
    249257  }
    250258 
     259  /**
     260    Represents a cell with a numeric value.
     261  */
     262  static class BooleanValue
     263    extends ExcelValue<Boolean>
     264  {
     265    BooleanValue(boolean value)
     266    {
     267      super(value, "BOOLEAN");
     268    }
     269   
     270    @Override
     271    public void writeTo(Cell cell, CellStyleCreator styleCreator)
     272    {
     273      cell.setCellValue(value ? 1 : 0);
     274      cell.setCellStyle(styleCreator.getCellStyle(format));
     275    }
     276  }
     277
    251278}
  • trunk/src/core/net/sf/basedb/util/formatter/BooleanFormatter.java

    r6127 r7649  
    2323
    2424import net.sf.basedb.util.Values;
     25import net.sf.basedb.util.excel.ExcelFormatter;
     26import net.sf.basedb.util.excel.ExcelValue;
    2527
    2628/**
    2729  Formats a boolean value. It is possible to select between yes/no, true/false, and 1/0
    28   mappings.
     30  mappings. 
    2931
    3032  @author nicklas
     
    3335*/
    3436public class BooleanFormatter
    35   implements Formatter<Boolean>
     37  implements Formatter<Boolean>, ExcelFormatter<Boolean, Boolean>
    3638{
    3739
     
    6365  // -------------------------------------------
    6466 
     67  /**
     68    @since 3.15
     69  */
     70  @Override
     71  public ExcelValue<Boolean> toExcelValue(Boolean value)
     72  {
     73    return ExcelValue.asBoolean(value);
     74  }
     75
    6576  /**
    6677    Enum for holding the various types of boolean output.
     
    97108    }
    98109  }
     110
    99111}
  • trunk/src/core/net/sf/basedb/util/formatter/MultiFormatter.java

    r6875 r7649  
    2525import java.util.Map;
    2626
     27import net.sf.basedb.util.excel.ExcelFormatter;
     28import net.sf.basedb.util.excel.ExcelValue;
     29
    2730
    2831/**
     
    3841*/
    3942public class MultiFormatter
    40   implements Formatter<Object>
     43  implements Formatter<Object>, ExcelFormatter<Object, Object>
    4144{
    4245
     
    8891  {
    8992    if (value == null) return "";
     93    Formatter<Object> f = getFormatter(value);
     94    return f.format(value);
     95  }
     96  @Override
     97  public Number parseString(String value)
     98  {
     99    throw new UnsupportedOperationException("parseString");
     100  }
     101  // -------------------------------------------
     102
     103  private Formatter<Object> getFormatter(Object value)
     104  {
    90105    Class<?> c = value.getClass();
    91106    Formatter<Object> f = formatter;
     
    114129      if (decideOnFirst) formatter = f;
    115130    }
    116     return f.format(value);
     131    return f;
    117132  }
    118   @Override
    119   public Number parseString(String value)
    120   {
    121     throw new UnsupportedOperationException("parseString");
    122   }
    123   // -------------------------------------------
    124 
     133 
    125134  /**
    126135    Register a formatter for the specified class.
     
    133142    formatters.put((Class)clazz, (Formatter)formatter);
    134143  }
     144
     145  @SuppressWarnings({ "rawtypes", "unchecked" })
     146  @Override
     147  public ExcelValue<Object> toExcelValue(Object value)
     148  {
     149    if (value == null) return null;
     150    Formatter<Object> f = getFormatter(value);
     151    ExcelValue ev = null;
     152    if (f instanceof ExcelFormatter)
     153    {
     154      ExcelFormatter ef = (ExcelFormatter)f;
     155      ev = ef.toExcelValue(value);
     156    }
     157    else
     158    {
     159      ev = ExcelValue.asString(f.format(value));
     160    }
     161    return ev;
     162  }
    135163}
Note: See TracChangeset for help on using the changeset viewer.