This is a migrated thread and some comments may be shown as answers.

RadGridView ExcelML Export doesn't cover '<', '>' chars

2 Answers 44 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jimin
Top achievements
Rank 1
Jimin asked on 22 Mar 2012, 09:22 AM
Hello.

I am developing some application that export contents of RadGridView using "GridViewExportOptions".

Below are my custom method.

public class ExcelHelper
    {
        public static void ExcelExport(RadGridView gridView)
        {
            if (gridView != null)
            {
                gridView.ElementExporting -= ElementExporting;
                gridView.ElementExporting += ElementExporting;

                ExportFormat format = ExportFormat.Html;

                string extension = "xls";
                format = ExportFormat.ExcelML;

                SaveFileDialog dialog = new SaveFileDialog();
                dialog.DefaultExt = extension;
                dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, "ExcelML");
                dialog.FilterIndex = 1;


                if (dialog.ShowDialog() == true)
                {
                    using (Stream stream = dialog.OpenFile())
                    {
                        GridViewExportOptions exportOptions = new GridViewExportOptions();
                        exportOptions.Format = format;
                        exportOptions.ShowColumnFooters = true;
                        exportOptions.ShowColumnHeaders = true;
                        exportOptions.ShowGroupFooters = true;

                        gridView.Export(stream, exportOptions);
                    }
                }
            }
        }
    }

This method works well usually but sometimes doesn't. The problem is <, > chars in the cell.

// Initialize Class
            this._users = new List<Users>();
            // Create User 1
            _user = new Users();
            // Assign Values
            _user.FirstName = "Bill";
            _user.Surname = "Clinton";
            _user.Username = "BillC";
            // Add User
            this._users.Add(this._user);


            // Create User 2
            _user = new Users();
            // Assign Values
            _user.FirstName = "George";
            _user.Surname = "Bush";
            _user.Username = "GeorgeB";
            // Add User
            this._users.Add(this._user);


            // Create User 3
            _user = new Users();
            // Assign Values
            _user.FirstName = "Baracka";
            _user.Surname = "Obama";
            _user.Username = "<BarackO>";               //This is the reason of Excel open error.
            // Add User
            this._users.Add(this._user);


            // Assign Items
            this.grdList.ItemsSource = this._users;

User 1, 2 is ok, but 3rd user has name that contains <xxx>.
In this case, Excel file is made but couldn't be open.


What can be the best solution for this case?

2 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 22 Mar 2012, 09:41 AM
Hello Jimin,

Please try using e.ShouldEncodeValue = true  inside the ElementExporting event handler of RadGridView.

This should do the trick.

Greetings,
Pavel Pavlov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Jimin
Top achievements
Rank 1
answered on 23 Mar 2012, 01:57 AM
Thanks, Pablov.

It doesn't work in ExportFormat.ExcelML mode, but it works well in ExportFormat.HTML mode.

Thanks for nice solution.
Tags
GridView
Asked by
Jimin
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Jimin
Top achievements
Rank 1
Share this question
or