Select to view content in your preferred language

Problem with Exporting Records as a .csv - Records not matching Fields

3659
7
11-21-2012 05:00 AM
IanPeebles
Frequent Contributor
When exporting records to a .csv, I have an issue where the data is not lining up with the fields.  When the data is exported, there are 5 blank columns in before the first field name.  When opening the data as a text file, I see 5 commas before the first field.  Please refer to the screenshots below. Notice how the long and lat coordinates are under map page and phone number rather than the X Longtitude and Y Latitude fields.  Anyone have any ideas on what is causing this problem?
0 Kudos
7 Replies
IanPeebles
Frequent Contributor
Does anyone know when there will be a fix for this issue?
0 Kudos
JohanCarlsson
Regular Contributor
I made my own if this is to any help. Not the prettiest code and not sure how effective it is with large amounts of data, but it works for my purposes.

private void GetAttributesFromSelected()
        {
            GraphicsLayer selectedLayer = MapApplication.Current.SelectedLayer as GraphicsLayer;
            IEnumerable<Graphic> selectedGraphics = selectedLayer.SelectedGraphics;
            int numberOfRows = selectedGraphics.Count();
            
            if (numberOfRows > 0)
            {
                string[] columnNames = GetColumnNames(selectedGraphics.First());
                string[] attributeArray;
                List<string[]> dataList = new List<string[]>();

                int attributeCounter = 0;

                foreach (Graphic g in selectedGraphics) // Iterates through chosen objects
                {
                    attributeArray = new string[25];
                    ICollection<object> valueCollection = g.Attributes.Values;
                    foreach (object obj in valueCollection) // Iterates through attribute values
                    {
                        if (obj != null)
                            attributeArray[attributeCounter] = obj.ToString();
                        attributeCounter++;
                    }

                    attributeCounter = 0;
                    dataList.Add(attributeArray);
                }
                WriteToCSV(columnNames, dataList);
            }
            else
                MessageBox.Show("No rows exported.");
        }

        // Gets column names
        private string[] GetColumnNames(Graphic graphic)
        {
            string[] columnNames = new string[graphic.Attributes.Keys.Count];
            int columnIndex = 0;
            foreach (string s in graphic.Attributes.Keys)
            { 
                columnNames[columnIndex] = s.ToString();
                columnIndex++;
            }
            return columnNames;
        }

        // Creates the CSV
        private void WriteToCSV(string[] columnNames, List<string[]> dataList)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "CSV File|*.csv";

            if (sfd.ShowDialog() == true)
            {
                using (Stream stream = sfd.OpenFile())
                {
                    StreamWriter sw = new StreamWriter(stream, System.Text.Encoding.UTF8);
                    foreach (string s in columnNames)
                        sw.Write(s + ","); //Skriver ut alla kolumnnamn
                    sw.WriteLine();
                    foreach (string[] strArr in dataList)
                    {
                        foreach (string str in strArr)
                        {
                            sw.Write(str + ",");
                        }
                        sw.WriteLine();
                    }
                    sw.Close();
                    stream.Close();
                }
            }

        }

        public bool CanExecute(object parameter)
        {
            return MapApplication.Current.SelectedLayer != null && MapApplication.Current.SelectedLayer is GraphicsLayer &&
                (MapApplication.Current.SelectedLayer as GraphicsLayer).SelectedGraphics.Count() > 0;
        }
0 Kudos
IanPeebles
Frequent Contributor
I made my own if this is to any help. Not the prettiest code and not sure how effective it is with large amounts of data, but it works for my purposes.

private void GetAttributesFromSelected()
        {
            GraphicsLayer selectedLayer = MapApplication.Current.SelectedLayer as GraphicsLayer;
            IEnumerable<Graphic> selectedGraphics = selectedLayer.SelectedGraphics;
            int numberOfRows = selectedGraphics.Count();
            
            if (numberOfRows > 0)
            {
                string[] columnNames = GetColumnNames(selectedGraphics.First());
                string[] attributeArray;
                List<string[]> dataList = new List<string[]>();

                int attributeCounter = 0;

                foreach (Graphic g in selectedGraphics) // Iterates through chosen objects
                {
                    attributeArray = new string[25];
                    ICollection<object> valueCollection = g.Attributes.Values;
                    foreach (object obj in valueCollection) // Iterates through attribute values
                    {
                        if (obj != null)
                            attributeArray[attributeCounter] = obj.ToString();
                        attributeCounter++;
                    }

                    attributeCounter = 0;
                    dataList.Add(attributeArray);
                }
                WriteToCSV(columnNames, dataList);
            }
            else
                MessageBox.Show("No rows exported.");
        }

        // Gets column names
        private string[] GetColumnNames(Graphic graphic)
        {
            string[] columnNames = new string[graphic.Attributes.Keys.Count];
            int columnIndex = 0;
            foreach (string s in graphic.Attributes.Keys)
            { 
                columnNames[columnIndex] = s.ToString();
                columnIndex++;
            }
            return columnNames;
        }

        // Creates the CSV
        private void WriteToCSV(string[] columnNames, List<string[]> dataList)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "CSV File|*.csv";

            if (sfd.ShowDialog() == true)
            {
                using (Stream stream = sfd.OpenFile())
                {
                    StreamWriter sw = new StreamWriter(stream, System.Text.Encoding.UTF8);
                    foreach (string s in columnNames)
                        sw.Write(s + ","); //Skriver ut alla kolumnnamn
                    sw.WriteLine();
                    foreach (string[] strArr in dataList) // Itererar igenom listans arrayer
                    {
                        foreach (string str in strArr) // Itererar igenom arrayens strängar
                        {
                            sw.Write(str + ",");
                        }
                        sw.WriteLine();
                    }
                    sw.Close();
                    stream.Close();
                }
            }

        }

        public bool CanExecute(object parameter)
        {
            return MapApplication.Current.SelectedLayer != null && MapApplication.Current.SelectedLayer is GraphicsLayer &&
                (MapApplication.Current.SelectedLayer as GraphicsLayer).SelectedGraphics.Count() > 0;
        }


Did you create this as a add-in?  If so, I would be interested in trying this out.  Basically, I want to select features in the web application, then export the selected records as a .csv or .dbf.
0 Kudos
JohanCarlsson
Regular Contributor
Did you create this as a add-in?  If so, I would be interested in trying this out.  Basically, I want to select features in the web application, then export the selected records as a .csv or .dbf.


Yes, it is a tool that I'm using with the attribute table. You just mark the posts you want in the table or on the map and then run the tool and you get a save file dialog to save the output. This is csv only though, but it works.
0 Kudos
IanPeebles
Frequent Contributor
The problem still exists in the Application Builder version 3.1.  Any fixes for the future?
0 Kudos
KatherineDalton
Esri Regular Contributor
Hi Ian,

Yes, we've seen this issue before and it is caused by having a record with a field with no value. The workaround is to ensure each field has a value/data.

See this previous post: http://forums.arcgis.com/threads/54576-CSV-Export-Issue

Hope that helps,
Katy
Katy Dalton | Technical Consultant
THE SCIENCE OF WHERE™
0 Kudos
IanPeebles
Frequent Contributor
Hi Ian,

Yes, we've seen this issue before and it is caused by having a record with a field with no value. The workaround is to ensure each field has a value/data.

See this previous post: http://forums.arcgis.com/threads/54576-CSV-Export-Issue

Hope that helps,
Katy


Any idea on when this issue will be fixed?  For our data, we cannot guarantee there will never be null values.
0 Kudos