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

Export RadGridView to CSV and replace "," with ";"

2 Answers 101 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Noah
Top achievements
Rank 1
Noah asked on 13 Aug 2019, 12:50 PM

Hello,

I am trying to export the data of my whole RadGridView to CSV.

Now I got a problem.

I used this Code:

public void PdfExport(RadGridView grid)
        {
            string extension = "pdf";

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

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                using (Stream stream = dialog.OpenFile())
                {
                    grid.ExportToPdf(stream,
                        new GridViewPdfExportOptions()
                        {
                            ShowColumnFooters = true,
                            ShowColumnHeaders = true,
                            ShowGroupFooters = true,
                            AutoFitColumnsWidth = false,
                            PageOrientation = PageOrientation.Landscape
                        });
                }
            }
        }

When I use this Code and open it with Visual Studio Code, the csv-file is comma seperated and every word is in quotes.

When I build a csv-file with Microsoft Excel it is ";"-seperated and without quotes.

 

How can I export my whole RadGridView with ","-seperated words that are without quotes?

 

2 Answers, 1 is accepted

Sort by
0
Noah
Top achievements
Rank 1
answered on 13 Aug 2019, 12:51 PM

oops, false Code.

 

string extension = "csv";
            SaveFileDialog dialog = new SaveFileDialog()
            {
                DefaultExt = extension,
                Filter = String.Format("{1} files (.{0})|.{0}|All files (.)|.", extension, "Excel"),
                FilterIndex = 1
            };
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                folder = dialog.FileName;
                using (Stream stream = dialog.OpenFile())
                {
                    dg.Export(stream,
                        new GridViewExportOptions()
                        {
                            Format = ExportFormat.Csv,
                            ShowColumnHeaders = true,
                            ShowColumnFooters = true,
                            ShowGroupFooters = false,
                        });
                }
            }

0
Accepted
Dinko
Telerik team
answered on 16 Aug 2019, 08:41 AM
Hi Noah,

Thank you for the provided code snippet.

Let first start with that you can consider using GridViewCsvExportOptions() object. Using this option, you can change the column and row delimiter. However, the cell quotes can't be removed out of the box. To achieve the desired result, you will need to manually update the contents of the file after the export process has finished. Here's what I have in mind:
private void ExportToCSV()
{
    string extension = "csv";
    RadSaveFileDialog dialog = new RadSaveFileDialog()
    {
        DefaultExt = extension,
        InitialDirectory = @"C:\Temp\CSV FILES",
        Filter = String.Format("{1} files (.{0})|.{0}|All files (.)|.", extension, "Excel"),
        FilterIndex = 1
    };
    if ((bool)dialog.ShowDialog())
    {
 
        var exportOptions = new GridViewCsvExportOptions();
                 
        exportOptions.ColumnDelimiter = ";";
        exportOptions.Format = ExportFormat.Csv;
        exportOptions.ShowColumnHeaders = true;
        exportOptions.ShowColumnFooters = true;
        exportOptions.ShowGroupFooters = true;
 
        using (Stream stream = dialog.OpenFile())
        {
            myGrid.Export(stream,exportOptions);
        }
        var oldContent = File.ReadAllText(dialog.FileName);
        var newContent = oldContent.Replace("\"", "");
        File.WriteAllText(dialog.FileName, newContent);
    }
}

Regards,
Dinko
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Noah
Top achievements
Rank 1
Answers by
Noah
Top achievements
Rank 1
Dinko
Telerik team
Share this question
or