Select to view content in your preferred language

Export the data from silverlight datagrid to Excel!

2558
11
12-08-2011 10:25 AM
AnhTruong
Emerging Contributor
Hello,

Here are the codes where I got data to display in silverlight datagrid as following:

            QueryTask queryTask = new QueryTask(URLService);
            queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
            queryTask.Failed += QueryTask_Failed;

            ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();
            query.OutFields.Add("*");
            query.Where = "1=1";
            queryTask.ExecuteAsync(query);

I like to export the data displayed in silverlight gridview to MS Excel file.

I built a export() function, but the data exported look strange
Org                                 Address                  Contact
ESRI.ArcGIS.Client.Graphic ESRI.ArcGIS.Client.Graphic ESRI.ArcGIS.Client.Graphic ESRI.ArcGIS.Client.Graphic ESRI.ArcGIS.Client.Graphic     ESRI.ArcGIS.Client.Graphic

Please help me with the export() function to export the datagrid data to Excel and PDF file. Thanks in advance.
0 Kudos
11 Replies
NathalieNeagle
Regular Contributor
In this post

http://forums.arcgis.com/threads/13460-Export-to-Excel

Karen Blaney mentioned that the file won't export if there are null attributes.

THIS IS MY ISSUE.

The export works if every field has a value, if not it doesn't save.

I'm trying to add code to handle this but running into no love. '

I changed the simple if statement I added to the color of RED to highlight the change


 //Create the rows
                foreach (Graphic data in graphics)
                {                    
                    lstValues.Clear();
                    
                    foreach (string field in fields)
                    {
                        string strValue = string.Empty;

                        if ((data.Attributes[field]) != null)
                        {
                            strValue = data.Attributes[field].ToString();
                        }
                        lstValues.Add(FormatField(strValue, strFormat));

                        //strValue = data.Attributes[field].ToString();

                        //lstValues.Add(FormatField(strValue, strFormat));
                    }
                    //Build the row
                    BuildStringOfRow(strBuilder, lstValues, strFormat);
                                  
                }

                WriteExcelFile(objSFD, strFormat, strBuilder);   


 //Build the column headers row
                BuildStringOfRow(strBuilder, lstFields, strFormat);

                //Create the rows
                foreach (Graphic data in graphics)
                {
                    lstValues.Clear();                   
                    
                    foreach (DataGridColumn field in columns)
                    {
                        string strValue = string.Empty;

                        if ((data.Attributes[field.Header.ToString()]) != null)
                        {
                            strValue = data.Attributes[field.Header.ToString()].ToString();
                        }
                       

                        lstValues.Add(FormatField(strValue, strFormat));

                       
                    }
                    //Build the row
                    BuildStringOfRow(strBuilder, lstValues, strFormat);
                      
                }

                WriteExcelFile(objSFD, strFormat, strBuilder);
            }



Could any one point me in the right direction.

Thanks
Nathalie
0 Kudos
NathalieNeagle
Regular Contributor
My last code changes worked!  Thanks!
0 Kudos