Data export

Data export

Scheduler data can be exported to the following formats:

Note!
All export techniques that will be considered in this article result in a JavaScript code that can be added to the page.
Detail information on the available client export functionality you can read in the related documentation - Data export and import.

Export to iCal

Client-side export

The following instructions describe how to export data from the scheduler into a iCal file:

Controller:

  • Add serialization support

    public override ActionResult Index() {
                var sched = new DHXScheduler();
                ...
                sched.Extensions.Add(SchedulerExtensions.Extension.Serialize);
                ...
    }
  • Create an action that will define exporting logic

    public ActionResult Export() {
                Response.ContentType = "text/plain";
                Response.AppendHeader("content-disposition", "attachment; filename=dhtmlxScheduler.ics");       
                return Content(Request.Form["data"].ToString());
    }

View:

  • Call method toICal(). For example, if you add a button by clicking on which the scheduler will start exporting, then its code will be the following:

    <input type="button" value="Print" onclick="<%= Model.ToICal("/ExportToICal/Export", "data")%>" />

There are 3 ways to call the method:

  • Model.ToICal(string url);
    where 'url' is the related path to an action implementing export.
  • Model.ToICal(string url,string name);
    where 'name' is the name of a POST parameter.
  • Model.ToICal();
    the method uses default 'url' and 'name' parameters which are '/%ControllerName%/Export' and 'data'

Server-side export

To get data in the iCal format directly from the DB, you should use the DHTMLX.Scheduler.Data.ICalRenderer class, particularly method string ICalRenderer.ToICal().

The ICalRenderer.ToICal() method has 4 overloads:

  1. public string ToICal(IEnumerable events)
  2. public string ToICal(IEnumerable events, string description)
  3. public string ToICal(IEnumerable events, Action<StringBuilder, object> renderer)
  4. public string ToICal(IEnumerable events, Action<StringBuilder, object> renderer, string description)
  • events - the collection of events in the default scheduler format.
  • description - adds the DESCRIPTION key to the output iCal object. By default, export will include only standard keys: DTSTART, DTEND, SUMMARY.
  • renderer - a custom function that renders event objects into an iCal string.
var renderer = new ICalRenderer();
var events = new DHXSchedulerDataContext().Events;
 
string result = renderer.ToICal(events);

When should you use the 'renderer' function?

The built-in serializer exports only 3 data properties: start_date, end_date and text. If you want to export some other data properties or change serializing of these 3 properties - 'renderer' comes in use.

var renderer = new ICalRenderer();
var events = new DHXSchedulerDataContext().Events;
 
string result = renderer.ToICal(events, RenderItem, "description");

and the RenderItem function can look as in:

public void RenderItem(StringBuilder builder, object item)
{
     var ev = item as Event;
 
     builder.AppendLine("BEGIN:VEVENT");
     builder.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHmmss}", ev.start_date));
     builder.AppendLine(string.Format("DTEND:{0:yyyyMMddTHmmss}", ev.end_date));
     builder.AppendLine(string.Format("SUMMARY:{0}", ev.text));
     builder.AppendLine("END:VEVENT");
}

The DHTMLX.Scheduler.Data.ICalRenderer class has 3 properties that store the names of data properties to be exported.

  • public string StartDate { get; set; } - default value 'start_date'.
  • public string EndDate { get; set; } - default value 'end_date'.
  • public string Text { get; set; } - default value 'text'.

So, by default, serializer expects event's text description to be stored in the item.text property, the start and end dates - in item.start_date, item.end_date respectively:

Event Table

If you store data in properties with some other names you can specify new values for those properties:

Let's assume you have data as in:

Event Table

Then, you should set new values for the DHTMLX.Scheduler.Data.ICalRenderer properties as in:

var renderer = new ICalRenderer();
renderer.StartDate = "event_start";
renderer.EndDate = "event_end";

See client-side documentation for more details.

Export to PNG

Starting from version 3.1, dhtmlxScheduler for ASP.NET can use new online service for exporting data to PNG, preserving all it's visual parameters. The solution is provided as a remote JavaScript library. Following script tag has to be added to the page:

<script src="http://export.dhtmlx.com/scheduler/api.js"></script>

The library will extend client-side instance of the scheduler with the method that allows exporting calendar to PDF, scheduler.exportToPDF:

HTML:

<script src="http://export.dhtmlx.com/scheduler/api.js"></script> 
<input value="Export to PDF" type="button" onclick='scheduler.exportToPNG()'>

You can refer the client-side documentation for the list of available settings or check the Online demo

Export to Excel

Starting from version 3.2, dhtmlxScheduler for ASP.NET can use the online service for exporting data into Excel spreadsheet. The solution is provided as a remote JavaScript library. Add the following script tag to the page:

<script src="http://export.dhtmlx.com/scheduler/api.js"></script>

The library will extend the client-side instance of the scheduler with the method that allows exporting calendar to Excel, scheduler.exportToExcel:

HTML:

<script src="http://export.dhtmlx.com/scheduler/api.js"></script> 
<input value="Export to Excel" type="button" onclick='scheduler.exportToExcel()'>

You can specify the name of the exported file and configuration of the columns. The method can take the object argument with the following properties (both optional):

  • name - (string) the name of the output file
  • columns (array) list of columns of exported spreadsheet
    • id - (string) name of the event's property that will be mapped to the column
    • header - (string) the column's header
    • width - (number) the column's width in pixels
scheduler.exportToExcel({
    { name:"My document", columns:[
        { id:"text",  header:"Title", width:150 },
        { id:"start_date",  header:"Start date", width:250 }
    ]}
});

You can refer to the client-side documentation for more details.

Export to pdf

One-page export

Generally, export to PDF can be implemented in 2 ways:

Both ways use the DHXScheduler.ToPDF() method to invoke the export. The method makes scheduler serialization and passes serialized data to a handler (online service, controller action) for generating a pdf document.

There are 3 ways to call the method:

  • DHXScheduler.ToPDF() - only if you use the online export service! In this case the method sets the url parameter to the default value - 'http://docs.dhtmlx.com/scheduler/pdf.html'.
  • DHXScheduler.ToPDF(string url)
  • DHXScheduler.ToPDF(string url, ExportColorScheme color)
  • url - (string) the url to the action implementing the export or online service that will generate a pdf file.
  • color - (enum ExportColorScheme) specifies colormap.
    • enum DHTMLX.Scheduler.ExportColorScheme:
      1. 'Color' - full-color printing, default value.
      2. 'Gray' - prints in shades of black and white.
      3. 'BlackWhite' - uses only black and white colors.
      4. 'FullColor' - actual background and text colors used while exporting.

Using the online export service

Controller:

  • Add pdf extension

    public override ActionResult Index() {
                var sched = new DHXScheduler();
                ...
                sched.Extensions.Add(SchedulerExtensions.Extension.PDF);
                ...
    }

View:

  • Call the method toPDF(). For example, if you add a button by clicking on which scheduler will start exporting, then its code will be the following:

    <input type="button" value="Print" onclick="<%= Model.ToPDF("http://dhtmlxscheduler.appspot.com/export/pdf")%> />

Using the server-side export library

To not use the online export service you can download the Export package (download link) and call the ToPDF() method as in:

For ASP.NET MVC

View:

<input type="button" value="ToPdf" onclick="<%= Model.ToPDF(Url.Action('Export', 'ExportController'), ExportColorScheme.Gray)%>" />

Controller ('ExportController'):

using DHTMLX.Export.PDF;
....
 
public ActionResult Export()
{
       var generator = new SchedulerPDFWriter();
       var xml = this.Server.UrlDecode(this.Request.Form["mycoolxmlbody"]);           
       MemoryStream pdf = generator.Generate(xml);         
       return File(pdf.ToArray(), generator.ContentType);            
}
For ASP.NET
<input type="button" value="ToPdf" onclick="<%= this.Scheduler.ToPDF(this.ResolveUrl('~/Export.ashx'))%>" />

Handler:

public void ProcessRequest(HttpContext context)
{
      var generator = new SchedulerPDFWriter();
      context.Response.ContentType = generator.ContentType;
      var xml = context.Server.UrlDecode(context.Request.Form["mycoolxmlbody"]);
      var pdfStream = generator.Generate(xml);
 
      pdfStream.WriteTo(context.Response.OutputStream);
}

Multi-page export

There is a possibility to export into a PDF document several view pages at once. For this purpose you should use the DHXScheduler.ToPDFRange() method.

The method has 6 overloads:

  • DHXScheduler.ToPDFRange(string url, int days)
  • DHXScheduler.ToPDFRange(string url, int days, ExportColorScheme scheme)
  • DHXScheduler.ToPDFRange(string url, int days, ExportColorScheme scheme, string view)
  • DHXScheduler.ToPDFRange(string url, DateTime start, DateTime end)
  • DHXScheduler.ToPDFRange(string url, DateTime start, DateTime end, ExportColorScheme scheme)
  • DHXScheduler.ToPDFRange(string url, DateTime start, DateTime end, ExportColorScheme scheme, string view)
  • url - the url to the action implementing the export or online service that will generate a pdf file
  • start - the date to start export from
  • end - the date to end export at
  • view - the view to apply export to. By default, the currently active view
  • days - sets the number of days (starting from the current one) that should be exported. Alternative to start, end parameters.
  • scheme - specifies colormap
    • enum DHTMLX.Scheduler.ExportColorScheme:
      1. 'Color' - full-color printing, default value
      2. 'Gray' - prints in shades of black and white
      3. 'BlackWhite' - uses only black and white colors
      4. 'FullColor' - actual background and text colors used while exporting
<input type="button" value="Print" onclick="<%= Model.ToPDFRange("http://dhtmlxscheduler.appspot.com/export/pdf", new Date(2012,0,1), new Date(2012, 1,1))%> />

Online export service

Starting from version 3.1, dhtmlxScheduler for ASP.NET can use new online service for exporting data to PDF, preserving all it's visual parameters.

The solution is provided as a remote JavaScript library. Following script tag has to be added to the page:

<script src="http://export.dhtmlx.com/scheduler/api.js"></script>

The library will extend client-side instance of the scheduler with the method that allows exporting calendar to PDF, scheduler.exportToPDF: HTML:

<script src="http://export.dhtmlx.com/scheduler/api.js"></script> 
<input value="Export to PDF" type="button" onclick='scheduler.exportToPDF()'>

You can refer the client-side documentation for the list of available settings or check the Online demo


comments powered by Disqus