objectListView (Export to csv)

1k views Asked by At

I'm using DatasListView from OLV, and I want to export my data to csv. They said to use olvexporter, but I can not find an example of it.

Can anyone explain how to use OlvExporter to me?

1

There are 1 answers

1
Christina On

You can try this:

        string csv = string.Empty;
        var olvExporter = new OLVExporter(objectListView1, 
        objectListView1.FilteredObjects);
        csv = olvExporter.ExportTo(OLVExporter.ExportFormat.CSV);

        csv = csv.Replace(",", ";");

        SaveFileDialog saveFile = new SaveFileDialog();
        saveFile.FileName = "ExportFile.csv";
        saveFile.Filter = "csv files (*.csv)|*.csv";

        if (saveFile.ShowDialog() == DialogResult.OK)
        {
            using (StreamWriter sw = new StreamWriter(saveFile.FileName))
            {
                sw.Write(csv);
            }
        }

The second parameter at the OLVExporter is only necessary if you want to enable Filtering at your objectListView. It it's set you are only exporting the results after the filtering.

If you choose CSV as ExportFormat you can use the csv.Replace() to change the output at the csv file.

The SaveFileDialog is also not necessary.