JavaScript Grid Exporting

The ShieldUI Grid provides built-in support for exporting its data to various formats. Currently the formats are:

  • Excel 2003 (.xml)
  • Excel 2007 and later (.xlsx)
  • CSV
  • PDF

To enable the exporting functionality for a Grid, one must add a toolbar containing a button with the "excel", "csv" or "pdf" commands.

The exporting functionality works in all major web browsers, starting fom IE8. For older browsers that do not implement local file save functionality, a server-side proxy for saving the data in a file would be required.

An example ASP.NET MVC WebAPI code of the server-side proxy for saving the file is shown below.

public class SaveData
{
    public String FileName { get; set; }
    public String ContentType { get; set; }
    public String Base64Content { get; set; }
}

public class FileSaverController : ApiController
{
    [HttpPost, ActionName("save")]
    public HttpResponseMessage Save([FromBody] SaveData saveData)
    {
        var data = Convert.FromBase64String(saveData.Base64Content);
        var contentType = saveData.ContentType;

        // strip the content encoding and other additional headers from the type
        contentType = Regex.Replace(contentType, ";.*$", "",
            RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase);

        var result = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new StreamContent(new MemoryStream(data))
        };

        result.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
        if (contentType.Contains("xml"))
        {
            result.Content.Headers.ContentType.CharSet = "UTF-8";
        }

        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = saveData.FileName
        };

        return result;
    }
}

To see more information on the exporting implementation and options, pleas check the help topics under this one.

For live demos of JavaScript, ASP.NET or ASP.NET MVC integration, please visit this link for exporting to Excel and this one for exporting to a PDF document.