2.0.0 • Published 2 years ago

@legtech/export v2.0.0

Weekly downloads
1
License
MIT
Repository
-
Last release
2 years ago

Functions to format an html table and data into json for export to Excel

Steps to use:

  1. Put the following in your html page or javascript:
WSLApp.Export.init();	
  1. Include an export button in the following format:
<input type="button" class="btn btn-primary exportTableButton" value="Export" disabled data-export-table="[id of the table you want to export]" data-export-url="@Url.Action("[ACTION NAME]","[CONTROLLER NAME]")"/>
  1. Include a controllor action to post the data too - for example:
[HttpPost]
public ActionResult ExportSearchResults(string exportSearchResultsData)
{
    if (exportSearchResultsData == null)
        return new EmptyResult();

    var searchResults = JsonConvert.DeserializeObject<IEnumerable<IEnumerable<string>>>(exportSearchResultsData);

    var csv = new StringBuilder();

    foreach (var rowResult in searchResults)
    {
        var quotedRow = rowResult.Select(p => "\"" + p.Replace("\"", "\"\"") + "\"");
        var row = string.Join(",", quotedRow);
        csv.AppendLine(row);
    }

    // This is a lot faster than using the File action.  See http://stackoverflow.com/questions/1928494/mvc-action-taking-a-long-time-to-return.

    Response.ContentType = "text/csv";
    Response.Headers.Add("Content-Disposition", "attachment;filename=SearchResults.csv");
    var csvBytes = Encoding.UTF8.GetBytes(csv.ToString());
    Response.Body.WriteAsync(csvBytes,0, csvBytes.Length );

    return new EmptyResult();
}

Additional Options:

  1. You can add a Title for a table, which will appear above the table in the first column. To set a title add the following attribute to a table: data-export-title="Your Title Here"
  2. You can specify mulitple tables for export by using a class instead of a table id. The tables will show up sequentially with a single line between them. To target multiple tables use the following on the export button data-export-table="css class on the tables you want to export"
  3. You can have the exporter ignore child elements within a table cell. To have it ignore child elements add data-trim-children="true" to the necessary table cell. For example, if your cell looks like TextSome Icon and you would like to just export 'Text'.
  4. You can override the text being exported from a cell. If set, the value of data-export-text will be used instead of the content of the cell. To use this, add attribute data-export-text="Text to export" to any td or th.
1.0.2

2 years ago

2.0.0

2 years ago

1.0.1

4 years ago

1.0.0

5 years ago