Changeset 7651


Ignore:
Timestamp:
Mar 14, 2019, 11:55:13 AM (5 years ago)
Author:
Nicklas Nordborg
Message:

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

Better handling of collections, multi-valued annotations and annotations with units.

Location:
trunk/src
Files:
3 edited

Legend:

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

    r7650 r7651  
    648648                  Type valueType = ep.annotationType.getValueType();
    649649                  UnitConverter unitConverter = null;
    650                   if (defaultUnit != null)
    651                   {
    652                     if (ep.unit != null) unitConverter = ep.unit.getUnitConverter(defaultUnit);
    653                     if (ep.unit == null) unitSymbol = defaultUnit.getDisplaySymbol();
    654                   }
    655650                  values = new ArrayList();
    656651                  for (AnnotationSnapshot s : list)
    657652                  {
     653                    if (defaultUnit != null && ep.unit == null && unitConverter == null) // Units should be converted for each item
     654                    {
     655                      Unit u = s.getActualUnit(dc);
     656                      unitConverter = u.getUnitConverter(defaultUnit);
     657                      unitSymbol = u.getDisplaySymbol();
     658                    }
    658659                    values.addAll(s.getActualValues(unitConverter, valueType));
    659660                  }
  • trunk/src/clients/web/net/sf/basedb/clients/web/plugins/XlsxTemplate.java

    r7650 r7651  
    2525import java.io.OutputStream;
    2626import java.util.Collection;
    27 import java.util.Date;
    28 import java.util.HashMap;
    2927import java.util.List;
    30 import java.util.Map;
    3128
    3229import org.apache.poi.ss.usermodel.Cell;
    33 import org.apache.poi.ss.usermodel.CellStyle;
    34 import org.apache.poi.ss.usermodel.DataFormat;
    3530import org.apache.poi.ss.usermodel.Row;
    3631import org.apache.poi.ss.usermodel.Sheet;
     
    4439import net.sf.basedb.util.Values;
    4540import net.sf.basedb.util.excel.CellStyleCreator;
    46 import net.sf.basedb.util.excel.ExcelFormatter;
    4741import net.sf.basedb.util.excel.ExcelValue;
    4842import net.sf.basedb.util.formatter.Formatter;
     
    157151    throws IOException
    158152  {
    159     if (values != null && values.size() == 1)
     153    if (values == null || values.size() == 0)
     154    {
     155      writeProperty(ep, null);
     156    }
     157    else if (values.size() == 1 && unit == null)
    160158    {
    161159      writeProperty(ep, values.get(0));
    162       return;
    163     }
    164     Cell cell = currentRow.createCell(colNum++);
    165     Formatter f = ep.formatter;
    166     if (unit != null) f = new PrefixSuffixFormatter("", f, PrefixSuffixFormatter.NBSP + unit);
    167     cell.setCellValue(Values.getString(values, collectionSeparator, true, f));
     160    }
     161    else
     162    {
     163      Cell cell = currentRow.createCell(colNum++);
     164      Formatter f = ep.formatter;
     165      if (unit != null) f = new PrefixSuffixFormatter("", f, PrefixSuffixFormatter.NBSP + unit);
     166      ExcelValue ev = ExcelValue.asString(Values.getString(values, collectionSeparator, true, f));
     167      ev.writeTo(cell, styleCreator);
     168    }
    168169  }
    169170
     
    172173  */
    173174  @Override
    174   @SuppressWarnings("unchecked")
     175  @SuppressWarnings({ "unchecked", "rawtypes" })
    175176  public void writeProperty(ExportedProperty ep, Object data)
    176177    throws IOException
     
    186187   */
    187188  @Override
    188   @SuppressWarnings("unchecked")
     189  @SuppressWarnings({ "unchecked", "rawtypes" })
    189190  public void writeCollection(ExportedProperty ep, Collection<?> values)
    190191    throws IOException
    191192  {
    192     Cell cell = currentRow.createCell(colNum++);
    193     cell.setCellValue(Values.getString(values, collectionSeparator, true, ep.formatter));
     193    if (values == null || values.size() == 0)
     194    {
     195      writeProperty(ep, null);
     196    }
     197    else if (values.size() == 1)
     198    {
     199      writeProperty(ep, values.iterator().next());
     200    }
     201    else
     202    {
     203      Cell cell = currentRow.createCell(colNum++);
     204      ExcelValue ev = ExcelValue.asString(Values.getString(values, collectionSeparator, true, ep.formatter));
     205      ev.writeTo(cell, styleCreator);
     206    }
    194207  }
    195208
  • trunk/src/core/net/sf/basedb/util/excel/ExcelValue.java

    r7649 r7651  
    129129  protected final String format;
    130130 
    131   ExcelValue(T value, String format)
     131  protected ExcelValue(T value, String format)
    132132  {
    133133    this.value = value;
Note: See TracChangeset for help on using the changeset viewer.