We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Export to Excel Gantt MVC

Hello,

I'm trying to export Gantt to excel. For that, I'm using below two methods.

  public void ExportToExcel(string GanttModel)
    {
        ExcelExport exp = new ExcelExport();
        var DataSource = TempData["GanttData"];
        GanttProperties obj = ConvertGanttObject(GanttModel);
        exp.Export(obj, DataSource, new GanttExportSettings() { Theme = ExportTheme.FlatSaffron });
    }

    private GanttProperties ConvertGanttObject(string gridProperty)
    {
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        IEnumerable div = (IEnumerable)serializer.Deserialize(gridProperty, typeof(IEnumerable));
        GanttProperties gridProp = new GanttProperties();
        foreach (KeyValuePair<string, object> ds in div)
        {
            var property = gridProp.GetType().GetProperty(ds.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
            if (property != null)
            {
                Type type = property.PropertyType;
                string serialize = serializer.Serialize(ds.Value);
                object value = serializer.Deserialize(serialize, type);
                property.SetValue(gridProp, value, null);
            }
        }
        return gridProp;
    }

I included folowwing dlls

Syncfusion.Compression.Base, Syncfusion.EJ.Export, Syncfusion.Linq.Base and Syncfusion.XlsIO.Base.

But exp.Export(obj, DataSource, new GanttExportSettings() { Theme = ExportTheme.FlatSaffron }); this line gives me error.

Would you please provide me the right code to implement the functionality. 

10 Replies

JS Jonesherine Stephen Syncfusion Team December 28, 2016 09:43 AM UTC

Hi Kuntal,  
We have analyzed the provided code snippet. We need to pass the server side data for exporting. We have prepared the sample and performed exporting action in server side.  
Please find the code example below:  
[controller] 
using Syncfusion.EJ.Export; 
using Syncfusion.XlsIO; 
using Syncfusion.JavaScript.Models;   
public void ExportToExcel(string GanttModel) 
        { 
            ExcelExport exp = new ExcelExport(); 
            var DataSource = this.GetEditingDataSource(); 
            GanttProperties obj = ConvertGanttObject(GanttModel); 
            exp.Export(obj, DataSource, "GanttExport.xlsx", ExcelVersion.Excel2010, new GanttExportSettings() { Theme = ExportTheme.FlatSaffron }); 
        } 
 
        private GanttProperties ConvertGanttObject(string gridProperty) 
        { 
            JavaScriptSerializer serializer = new JavaScriptSerializer(); 
            IEnumerable div = (IEnumerable)serializer.Deserialize(gridProperty, typeof(IEnumerable)); 
            GanttProperties gridProp = new GanttProperties(); 
            foreach (KeyValuePair<string, object> ds in div) 
            { 
                var property = gridProp.GetType().GetProperty(ds.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase); 
                if (property != null) 
                { 
                    Type type = property.PropertyType; 
                    string serialize = serializer.Serialize(ds.Value); 
                    object value = serializer.Deserialize(serialize, type); 
                    property.SetValue(gridProp, value, null); 
                } 
            } 
            return gridProp; 
        } 
To call the server side exporting method we need to map “mappers” in client side and we need to include “ExcelExport” item in Toolbar to render the exporting icon in Toolbar  
Please find the code example below:  
 [view] 
@(Html.EJ().Gantt("GanttContainer")              
             .Mappers(mp => mp.ExportToExcelAction("Gantt/ExportToExcel"))              
             .ToolbarSettings(tool => 
              { 
                  tool.ShowToolbar(true); 
                  tool.ToolbarItems(new List<GanttToolBarItems>() 
                 { 
                      GanttToolBarItems.ExcelExport, 
                 }); 
              }) 
              .Datasource(ViewBag.datasource) 
    )@(Html.EJ().ScriptManager()) 
 
We have also prepared the sample based on this. Please find the sample from below location  
Disclaimer: We have removed bin and obj folder in the given sample for some security reasons, we must include Syncfusion.EJ, Syncfusion.EJ.MVC, Syncfusion.EJ.Export, Syncfusion.XlsIO.Base, Syncfusion.Linq.Base, Syncfusion.Compression.Base dlls to perform exporting in Gantt control which is available in Essential Studio installed location.    
 
If still issue exists at your end please revert us by modifying the sample based on your application along with the replication procedure? This would be helpful for us to serve you. 
Please let us know if you require further assistance on this.
Regards, 
Jone sherine P S
 



KP Kuntal Patel May 23, 2017 11:21 AM UTC

Hello,

After a long time,i am again working on this issue. It's still gives me error on this line.

exp.Export(obj, DataSource, new GanttExportSettings() { Theme = ExportTheme.FlatSaffron });

"Given key was not present in the collection".

Apart from that,after including all the dlls,i'm not event seeing the way to export to pdf. Even gantt toolbar items shows error for pdfexport property(not found). Would you please provide any solution for the same.


JD Jayakumar Duraisamy Syncfusion Team May 24, 2017 12:50 PM UTC

Hi Kuntal, 
Please find the response below. 
In Gantt, we had provided PDF export support in our latest volume 2, 2017 main release version 15.2.0.40. If you are using any earlier version, please upgrade to our latest version.  
Please refer following code snippet for enable Excel and PDF export support in Gantt. 
[chtml] 
@(Html.EJ().Gantt("GanttContainer") 
//…  
// Call server method from client side 
.Mappers(mp => mp.ExportToExcelAction("Gantt/ExportToExcel") 
                 .ExportToExcelAction("Gantt/ExportToPdf")) 
.ToolbarSettings(tool => 
              { 
                  tool.ShowToolbar(true); 
                  tool.ToolbarItems(new List<GanttToolBarItems>() 
                 { 
                     GanttToolBarItems.ExcelExport, 
                      GanttToolBarItems.PdfExport 
                 }); 
[CS] 
// For Excel export server side method 
public void ExportToExcel(string GanttModel) 
        { 
            ExcelExport exp = new ExcelExport(); 
   // Please ensure with given parameter order 
            exp.Export(obj, DataSource, "GanttExport.xlsx", ExcelVersion.Excel2010, new GanttExportSettings() { Theme = ExportTheme.FlatSaffron }); 
        } 
// PDF export server side method 
public void PdfExport(string GanttModel,bool isFitToWidth) 
        { 
            PdfExport exp = new PdfExport(); 
//… 
            exp.Export(obj, DataSource, settings, "Gantt"); 
        } 
 
We have prepared a sample with PDF and Excel export support, but we could not able to reproduce the reported error in our sample. 
Can you please revert us by modifying the sample based on your application along with the replication procedure?  
This would be helpful for us to serve you. 
Regards, 
Jayakumar D 



KP Kuntal Patel May 25, 2017 08:17 AM UTC

Thanks for the reply Jayakumar.

I've updated to latest version (dll version 15.2400.0.40). Now it shows support for pdf. The function you provided will not work. I've made some changes into pdf function

 public void PdfExport(string GanttModel)
    {
        PdfExport exp = new PdfExport();
        List<TaskData> DataSource = (List<TaskData>)TempData["GanttData"];
        GanttProperties obj = ConvertGanttObject(GanttModel);
        GanttPdfExportSettings settings = new GanttPdfExportSettings();
        settings.Theme = GanttExportTheme.FlatSaffron;   
        exp.Export(obj, DataSource, settings);
    }

This function shows object reference error on last line : exp.Export(obj, DataSource, settings);

I can see that,none of the objects are null from my side. obj, DataSource and settings are having values. Request to provide resolution on urgent basis.

Apart from that,i'm still facing previously mentioned issue while exporting to excel.

 public void ExportToExcel(string GanttModel)
    {
        ExcelExport exp = new ExcelExport();
        List<TaskData> DataSource = (List<TaskData>)TempData["GanttData"];
        GanttProperties obj = ConvertGanttObject(GanttModel);
        // Please ensure with given parameter order 
        exp.Export(obj, DataSource, "GanttExport.xlsx", ExcelVersion.Excel2010, new GanttExportSettings() { Theme = ExportTheme.FlatSaffron });
    }

This function shows The given key was not present in the dictionary error on last line :   exp.Export(obj, DataSource, "GanttExport.xlsx", ExcelVersion.Excel2010, new GanttExportSettings() { Theme = ExportTheme.FlatSaffron });

If it would be a data issue,it should get resolved by different data. However, i'm facing this error on each and every type of data. Even with a single item. Would you please provide final solution to this issue?


KP Kuntal Patel May 25, 2017 09:22 AM UTC

Looks like it's problem with list of resources. I'm working with the data which something like this.


GanttData objGanttData = new GanttData();
        if (id!= 0)
        {
            objGanttData.LstTaskData = GetData(id);
        }
        objGanttData.LstAllResource = GetResources();

        objGanttData.objPriorityCodes = JsonConvert.SerializeObject(GetPriorities(), Formatting.None);
        objGanttData.objProjectWiseStatus = JsonConvert.SerializeObject(GetStatus(), Formatting.None);
        objGanttData.objWorkItemTypes = JsonConvert.SerializeObject(GetWorkItemType(), Formatting.None);
        ViewBag.GanttData = objGanttData;
        TempData["GanttData"] = objGanttData.LstTaskData;

in view page of gantt

....
.Resources((List<Resource>)ViewBag.GanttData.LstAllResource)
......
.Datasource(ViewBag.GanttData.LstTaskData)




JD Jayakumar Duraisamy Syncfusion Team May 26, 2017 04:20 PM UTC

Hi Kuntal, 
 
We regret for the inconvenience caused. 
 
Query 1: This function shows object reference error on last line : exp.Export(obj, DataSource, settings); 
 
Answer: We have analyzed the reported issue in our sample. We can able to reproduce this issue due to resources collection data type has changed to dictionary type in the “ConvertGanttObject” method. 
Hence, it caused the object reference error. To overcome this issue, we have to set resources collection as list data type to the property of resources. 
 
Please refer following code snippet, 
private GanttProperties ConvertGanttObject(string gridProperty) 
        { 
            JavaScriptSerializer serializer = new JavaScriptSerializer(); 
            IEnumerable div = (IEnumerable)serializer.Deserialize(gridProperty, typeof(IEnumerable)); 
            GanttProperties gridProp = new GanttProperties(); 
            foreach (KeyValuePair<string, object> ds in div) 
            { 
                var property = gridProp.GetType().GetProperty(ds.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase); 
                if (ds.Key == "resources") 
                    property.SetValue(gridProp, GetResourceCollection(), null); 
                else if (property != null) 
                { 
                    Type type = property.PropertyType; 
                    string serialize = serializer.Serialize(ds.Value); 
                    object value = serializer.Deserialize(serialize, type); 
                    property.SetValue(gridProp, value, null); 
                } 
            } 
            return gridProp; 
        } 
We have prepared a sample for your reference. Please find the sample location as below, 
 
Query 2: This function shows The given key was not present in the dictionary error on last line :   exp.Export(obj, DataSource, "GanttExport.xlsx", ExcelVersion.Excel2010, new GanttExportSettings() { Theme = ExportTheme.FlatSaffron }); 
 
Answer: We can able to reproduce the issue in Gantt Excel and we have logged a report on this. Also we have created a support incident under your account to track the status of this issue. 
Please log on to our support website to check for further updates. 
 
Please let us know if you require further assistance on this. 
Regards, 
Jayakumar D 



KP Kuntal Patel June 23, 2017 11:20 AM UTC

Tried to implement the way you suggested. Still getting error : 

Unable to cast object of type 'System.Collections.Generic.List`1[Cygnet.Projman.EFData.ViewModel.ResourceListData]' to type 'System.Collections.Generic.IList`1[System.Object]'.


  private GanttProperties ConvertGanttObject(string gridProperty)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        IEnumerable div = (IEnumerable)serializer.Deserialize(gridProperty, typeof(IEnumerable));
        GanttProperties gridProp = new GanttProperties();
        foreach (KeyValuePair<string, object> ds in div)
        {
            var property = gridProp.GetType().GetProperty(ds.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
            if (ds.Key == "resources")
                property.SetValue(gridProp, GetResources().Select(x => new ResourceListData() { Id = x.ResourceId, Name = x.ResourceName}).ToList(), null);
            else if (property != null)
            {
                Type type = property.PropertyType;
                string serialize = serializer.Serialize(ds.Value);
                object value = serializer.Deserialize(serialize, type);
                property.SetValue(gridProp, value, null);
            }
        }
        return gridProp;
    }

Problem is with the highlighted line. Below are the classes

 public class Resource
    {
        public int ResourceId { get; set; }
        public string ResourceName { get; set; }
        public string Image { get; set; }

    }
    public class ResourceListData
    {

        public int Id { get; set; }
        public string Name { get; set; }

    }

Find the attached screenshot of the error. After these many clarifications,this issue is not getting resolved. Looks like we are testing your product. Is it the case? It would be nice if you can test this functionality and then only provide any inputs.

Attachment: Gantt_Export_4e8a6e24.zip


JD Jayakumar Duraisamy Syncfusion Team June 26, 2017 03:29 PM UTC

Hi Kuntal, 
We regret for the inconvenience caused. 
We have analyzed the reported issue in our sample but couldn’t able to reproduce the issue. We are suspecting that you are using old Syncfusion.EJ.Export assembly file. Hence, we would like to suggest you that replace your old dll’s with our latest patch files and source file provided in the incident # 180223 which included all bug fixes and new features support. 
If you are facing this same issue in our updated patch files, please revert us with demo sample. It will help us to serve you better. 
Regards, 
Jayakumar D 



KP Kuntal Patel June 29, 2017 12:24 PM UTC

Thanks for the response. We've resolved this issue as of now.However, the columns which are added from client events are not getting exported. Code to add the columns are as below:

.......

<script type="text/x-jsrender" id="columnPriority">

    {{if #data.item.Priority}}


    <div style="text-align:right">

        {{:#data.item.Priority}}

    </div>

    {{/if}}


</script>

.......

Request you to resolve this at the earliest as we are running short of time for delivery.



JD Jayakumar Duraisamy Syncfusion Team June 30, 2017 09:13 AM UTC

We regret for the inconvenience caused. 
To export custom column template, we have to define an API called “IncludeTemplateColumn” as true for Pdf export in “GanttPdfExportSettings” object before pass it to the server side export method. 
For both excel & Pdf export, we must define the column field name to column property “mappingName”  then only the values of template columns are exported properly. 
Note: Pdf export will export only template column values. It is not support template styles. 
Please refer following code snippet, 
public void PdfExport(string GanttModel, bool isFitToWidth) 
        { 
            PdfExport exp = new PdfExport(); 
            //… 
            GanttPdfExportSettings settings = new GanttPdfExportSettings(); 
            settings.IncludeTemplateColumn = true; 
            exp.Export(obj, DataSource, settings, "Gantt"); 
        } 
function load(args) { 
            var columns = this.getColumns(); 
            var priority = { 
                field: "Priority", 
                headerText: "Priority", 
                textAlign: "right", 
                isTemplateColumn: true, 
                templateID: "columnPriority", 
                mappingName: "Priority", 
            }; 
            columns.splice(2, 0, priority); 
} 
We have also prepared a sample for your reference. Please find the sample location as below, 
Please let us know, if you require any other assistance. 
Regards, 
Jayakumar D 


Loader.
Live Chat Icon For mobile
Up arrow icon