Changeset 7645


Ignore:
Timestamp:
Mar 12, 2019, 10:56:19 AM (3 years ago)
Author:
Nicklas Nordborg
Message:

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

Implemented exporter to Excel file. It will currently write all values as text values.

Location:
trunk
Files:
1 added
6 edited

Legend:

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

    r5843 r7645  
    2222package net.sf.basedb.clients.web.plugins;
    2323
     24import java.io.Closeable;
    2425import java.io.IOException;
    2526import java.util.Collection;
    2627import java.util.List;
     28
     29import net.sf.basedb.core.ProgressReporter;
    2730
    2831/**
     
    3538*/
    3639public interface ExportTemplate
     40  extends Closeable
    3741{
    3842  /**
     
    111115    @throws IOException If there is an error
    112116  */
    113   public void endDocument()
     117  public void endDocument(ProgressReporter progress)
    114118    throws IOException;
    115119 
  • trunk/src/clients/web/net/sf/basedb/clients/web/plugins/PlainTextTemplate.java

    r7398 r7645  
    2929
    3030import net.sf.basedb.core.Item;
     31import net.sf.basedb.core.ProgressReporter;
    3132import net.sf.basedb.util.Values;
    3233import net.sf.basedb.util.formatter.Formatter;
     
    186187  */
    187188  @Override
    188   public void endDocument()
    189     throws IOException
     189  public void endDocument(ProgressReporter progress)
     190    throws IOException
     191  {
     192    exportStream.flush();
     193  }
     194
     195 
     196  /**
     197    Do nothing.
     198  */
     199  @Override
     200  public void close()
     201    throws IOException
    190202  {}
    191203
    192  
    193204  private static final Pattern UNSAFE = Pattern.compile("(\\r?\\n\\r?|\\t)");
    194205  /**
  • trunk/src/clients/web/net/sf/basedb/clients/web/plugins/SimpleExport.java

    r7605 r7645  
    6868import net.sf.basedb.core.snapshot.SnapshotManager;
    6969import net.sf.basedb.util.Enumeration;
     70import net.sf.basedb.util.FileUtil;
    7071import net.sf.basedb.util.Values;
    7172import net.sf.basedb.util.formatter.Formatter;
     
    8081import java.util.List;
    8182import java.util.Set;
     83
    8284import java.util.HashSet;
    8385
     
    425427  {
    426428    String label = "Format";
    427     String description = "You may select to export in text or XML format.";
     429    String description = "You may select to export in text, XML or Excel (xlsx) format.";
    428430    Enumeration<String, String> options = new Enumeration<String, String>();
    429431    options.add("text", "Tab-separated text file");
    430432    options.add("xml", "XML");
     433    options.add("xlsx", "Excel (xlsx)");
    431434    PluginParameter<String> formatParameter = new PluginParameter<String>
    432435      (
     
    565568   
    566569    // Open stream for output and set file name and MIME type
    567     Writer exportStream = new OutputStreamWriter(out, "UTF-8");
    568     // Create export template for outputting the data
    569     ExportTemplate template = "xml".equals(format) ?
    570       new XMLTemplate(exportStream, exportedProperties, itemType) :
    571       new PlainTextTemplate(exportStream, exportedProperties, itemType, collectionSeparator);
     570    ExportTemplate template = null;
     571    if ("xlsx".equals(format))
     572    {
     573      template = new XlsxTemplate(out, exportedProperties, itemType, totalItems, collectionSeparator);
     574    }
     575    else
     576    {
     577      Writer exportStream = new OutputStreamWriter(out, "UTF-8");
     578      // Create export template for outputting the data
     579      template = "xml".equals(format) ?
     580        new XMLTemplate(exportStream, exportedProperties, itemType) :
     581        new PlainTextTemplate(exportStream, exportedProperties, itemType, collectionSeparator);
     582      out.setCharacterSet("UTF-8");
     583    }
    572584    out.setFilename(itemType.name().toLowerCase()+".export." + template.getDefaultFileExtension());
    573     out.setMimeType(template.getMimeType());
    574     out.setCharacterSet("UTF-8");
     585    out.setMimeType(template.getMimeType());   
    575586   
    576587    // Write start of document and headers
     
    679690        if (progress != null)
    680691        {
    681           int percent = (int)(99L * numExported / totalItems);
     692          int percent = (int)(97L * numExported / totalItems);
    682693          progress.display(percent, numExported + " of " + totalItems + " done.");
    683694        }
    684695      }
    685696     
     697      template.endDocument(progress);
    686698      if (progress != null)
    687699      {
    688         progress.display(100, numExported + " of " + totalItems + " done.");
    689       }
    690      
    691       template.endDocument();
     700        progress.display(100, numExported + " items were exported successfully");
     701      }
    692702      successMessage = numExported + " items were exported successfully";
    693703    }
     
    698708    finally
    699709    {
    700       exportStream.flush();
     710      out.flush();
     711      FileUtil.close(template);
    701712    }
    702713  }
  • trunk/src/clients/web/net/sf/basedb/clients/web/plugins/XMLTemplate.java

    r6473 r7645  
    2929
    3030import net.sf.basedb.core.Item;
     31import net.sf.basedb.core.ProgressReporter;
    3132
    3233import org.jdom2.DocType;
     
    204205  */
    205206  @Override
    206   public void endDocument()
     207  public void endDocument(ProgressReporter progress)
    207208    throws IOException
    208209  {
    209210    exportStream.write("</items>\n</export>");
    210   }
    211  
     211    exportStream.flush();
     212  }
     213 
     214  /**
     215    Do nothing.
     216  */
     217  @Override
     218  public void close()
     219    throws IOException
     220  {}
     221
    212222  private static final Pattern NEWLINE = Pattern.compile("\\r?\\n\\r?");
    213223  private String escape(String data)
  • trunk/www/plugins/net/sf/basedb/clients/web/plugins/simple_export.js

    r7604 r7645  
    4242    Events.addEventHandler('formatText', 'change', simpleExport.formatChanged);
    4343    Events.addEventHandler('formatXml', 'change', simpleExport.formatChanged);
     44    Events.addEventHandler('formatXlsx', 'change', simpleExport.formatChanged);
    4445    Events.addEventHandler('collectionSeparatorPreset', 'change', simpleExport.separatorPresetChanged);
    4546   
     
    9697  simpleExport.formatChanged = function()
    9798  {
    98     var textFormat = Doc.element('formatText').checked;
    99     Doc.element('collectionSeparator').disabled = !textFormat;
    100     Doc.element('collectionSeparatorPreset').disabled = !textFormat;
     99    var xmlFormat = Doc.element('formatXml').checked;
     100    Doc.element('collectionSeparator').disabled = xmlFormat;
     101    Doc.element('collectionSeparatorPreset').disabled = xmlFormat;
    101102  }
    102103 
  • trunk/www/plugins/net/sf/basedb/clients/web/plugins/simple_export.jsp

    r7605 r7645  
    109109          <input type="radio" name="parameter:format" id="formatText" value="text" checked
    110110            ><label for="formatText">Tab-separated text file</label><br>
    111             <span style="padding-left: 2em;">Separator for multi-valued columns:
     111          <input type="radio" name="parameter:format" id="formatXlsx" value="xlsx"
     112            ><label for="formatXlsx">Excel (xlsx)</label><br>
     113          <input type="radio" name="parameter:format" id="formatXml" value="xml"
     114            ><label for="formatXml">XML</label><br>
     115          <span style="padding-left: 2em;">Separator for multi-valued columns:
    112116            <input type="text" name="parameter:collectionSeparator"
    113117              id="collectionSeparator" value="," style="width: 3em;">
    114              
    115118            <select name="collectionSeparatorPreset" id="collectionSeparatorPreset">
    116119              <option value="">- presets -
     
    120123            </select>
    121124            </span><br>
    122           <input type="radio" name="parameter:format" id="formatXml" value="xml"
    123             ><label for="formatXml">XML</label><br>
    124125        </td>
    125126      </tr>
Note: See TracChangeset for help on using the changeset viewer.