@legtech/export
v2.0.0
Published
Functions to format an html table and data into json for export to Excel
Downloads
4
Keywords
Readme
Functions to format an html table and data into json for export to Excel
Steps to use:
- Put the following in your html page or javascript:
WSLApp.Export.init();
- 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]")"/>
- 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:
- 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"
- 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]"
- 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'.
- 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.