Skip to main content

CsvDataAwareExporterOptions Class

Specifies options for exporting the document to CSV format.

Namespace: DevExpress.XtraExport.Csv

Assembly: DevExpress.Printing.v23.2.Core.dll

NuGet Package: DevExpress.Printing.Core

Declaration

public class CsvDataAwareExporterOptions :
    IXlDocumentOptions

Example

The following code snippet illustrates how to specify options to export the document to CSV format.

A new instance of the CsvDataAwareExporterOptions class is initialized.The IXlDocument.Options property of a document is casted to the CsvDataAwareExporterOptions. It provides access to all the options associated to the CsvDataAwareExporterOptions object.

Note

A complete sample project is available at https://github.com/DevExpress-Examples/excel-export-api-examples

// Create an exporter instance.
IXlExporter exporter = XlExport.CreateExporter(documentFormat);

// Create a new document.
using(IXlDocument document = exporter.CreateDocument(stream)) {
    document.Options.Culture = CultureInfo.CurrentCulture;

    // Specify options for exporting the document to CSV format.
    CsvDataAwareExporterOptions csvOptions = document.Options as CsvDataAwareExporterOptions;
    if(csvOptions != null) {
        // Set the encoding of the text-based file to which the workbook is exported.
        csvOptions.Encoding = Encoding.UTF8;
        // Write a preamble that specifies the encoding used.
        csvOptions.WritePreamble = true;
        // Convert a cell value to a string by using the current culture's format string. 
        csvOptions.UseCellNumberFormat = false;
        // Insert the newline character after the last row of the resulting text.
        csvOptions.NewlineAfterLastRow = true;
    }

    // Generate data for the document.
    using(IXlSheet sheet = document.CreateSheet()) {
        using(IXlRow row = sheet.CreateRow()) {
            using(IXlCell cell = row.CreateCell()) {
                cell.Value = "Product";
            }
            for(int i = 0; i < 4; i++) {
                using(IXlCell cell = row.CreateCell()) {
                    cell.Value = string.Format("Q{0}", i + 1);
                }
            }
        }
        string[] products = new string[] { 
            "Camembert Pierrot", "Gorgonzola Telino", "Mascarpone Fabioli", "Mozzarella di Giovanni",
            "Gnocchi di nonna Alice", "Gudbrandsdalsost", "Gustaf's Knäckebröd", "Queso Cabrales",
            "Queso Manchego La Pastora", "Raclette Courdavault", "Singaporean Hokkien Fried Mee", "Wimmers gute Semmelknödel"
        };
        Random random = new Random();
        for(int i = 0; i < 12; i++) {
            using(IXlRow row = sheet.CreateRow()) {
                using(IXlCell cell = row.CreateCell()) {
                    cell.Value = products[i];
                }
                for(int j = 0; j < 4; j++) {
                    using(IXlCell cell = row.CreateCell()) {
                        cell.Value = Math.Round(random.NextDouble() * 2000 + 3000);
                    }
                }
            }
        }
    }
}

Implements

Inheritance

Object
CsvDataAwareExporterOptions
See Also