Changeset 7646


Ignore:
Timestamp:
Mar 13, 2019, 1:17:17 PM (3 years ago)
Author:
Nicklas Nordborg
Message:

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

Started to implement a way for regular formatters to apply their style to Excel cells as well.

It is based on the new interface ExcelFormatter which defines a single method that return a ExcelValue object. There are a currently a few static helper method for creating date cells (and string/empty cell but those are trivial). One issue is that there are some minor differences in the format templates between Java/Excel?, but there is a converter in Apache POI that I hope works.

It has been tested with the table exporter should work with regular property columns and single-valued annotations so far.

Location:
trunk/src
Files:
4 added
2 edited

Legend:

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

    r7645 r7646  
    2525import java.io.OutputStream;
    2626import java.util.Collection;
     27import java.util.Date;
     28import java.util.HashMap;
    2729import java.util.List;
     30import java.util.Map;
    2831
    2932import org.apache.poi.ss.usermodel.Cell;
     33import org.apache.poi.ss.usermodel.CellStyle;
     34import org.apache.poi.ss.usermodel.DataFormat;
    3035import org.apache.poi.ss.usermodel.Row;
    3136import org.apache.poi.ss.usermodel.Sheet;
     
    3843import net.sf.basedb.core.ProgressReporter;
    3944import net.sf.basedb.util.Values;
     45import net.sf.basedb.util.excel.CellStyleCreator;
     46import net.sf.basedb.util.excel.ExcelFormatter;
     47import net.sf.basedb.util.excel.ExcelValue;
    4048import net.sf.basedb.util.formatter.Formatter;
    4149import net.sf.basedb.util.formatter.PrefixSuffixFormatter;
     
    5967  private final Workbook workbook;
    6068  private final Sheet sheet;
     69  private final CellStyleCreator styleCreator;
    6170  private Row currentRow;
    62  
     71
    6372 
    6473  /**
     
    7786    this.workbook = numItems > 1000 ? new SXSSFWorkbook(1000) : new XSSFWorkbook(XSSFWorkbookType.XLSX);
    7887    this.sheet = workbook.createSheet(itemType.name());
     88    this.styleCreator = new CellStyleCreator(workbook);
    7989  }
    8090
     
    147157    throws IOException
    148158  {
     159    if (values != null && values.size() == 1)
     160    {
     161      writeProperty(ep, values.get(0));
     162      return;
     163    }
    149164    Cell cell = currentRow.createCell(colNum++);
    150165    Formatter f = ep.formatter;
     
    162177  {
    163178    Cell cell = currentRow.createCell(colNum++);
    164     cell.setCellValue(ep.formatter.format(data));
     179   
     180    ExcelValue ev = null;
     181    if (ep.formatter instanceof ExcelFormatter)
     182    {
     183      ExcelFormatter ef = (ExcelFormatter)ep.formatter;
     184      ev = ef.toExcelValue(data);
     185    }
     186    else
     187    {
     188      ev = ExcelValue.asString(ep.formatter.format(data));
     189    }
     190    if (ev == null) ev = ExcelValue.EMPTY_CELL;
     191    ev.writeTo(cell, styleCreator);
    165192  }
    166193 
     
    210237  }
    211238
     239
     240
    212241}
  • trunk/src/core/net/sf/basedb/util/formatter/DateFormatter.java

    r7514 r7646  
    2828
    2929import net.sf.basedb.core.DateFormatException;
     30import net.sf.basedb.util.excel.ExcelFormatter;
     31import net.sf.basedb.util.excel.ExcelValue;
    3032
    3133
     
    4042*/
    4143public class DateFormatter
    42   implements Formatter<Date>
     44  implements Formatter<Date>, ExcelFormatter<Date, Date>
    4345{
    4446
    45   private DateFormat dateFormat;
    46   private String format;
     47  private final DateFormat dateFormat;
     48  private final String format;
    4749 
    4850  /**
     
    5355  {
    5456    this.dateFormat = dateFormat;
    55     this.format = null;
     57    if (dateFormat instanceof SimpleDateFormat)
     58    {
     59      this.format = ((SimpleDateFormat)dateFormat).toPattern();
     60    }
     61    else
     62    {
     63      this.format = null;
     64    }
    5665  }
    5766 
     
    109118    return dateFormat;
    110119  }
     120
     121  /**
     122    @since 3.15
     123  */
     124  @Override
     125  public ExcelValue<Date> toExcelValue(Date value)
     126  {
     127    return ExcelValue.asDate(value, format);
     128  }
    111129 
    112130}
Note: See TracChangeset for help on using the changeset viewer.