Document Solutions for Excel, .NET Edition | Document Solutions
File Operations / Import and Export CSV File
In This Topic
    Import and Export CSV File
    In This Topic

    This section summarizes how DsExcel .NET handles the spreadsheet documents(.csv files).

    While importing and exporting a workbook in order to open and save a csv file or stream, you can use the following properties and methods of the CsvOpenOptions class and the CsvSaveOptions class in order to configure several open and save options in a workbook.

    Settings Description

    CsvOpenOptions.ConvertNumericData

    This property can be used to get or set a value that indicates whether the string in text file is converted to numeric data.

    CsvOpenOptions.ConvertDateTimeData

    This property can be used to get or set a value that indicates whether the string in text file is converted to date data.

    CsvOpenOptions.SeparatorString

    This property can be used to get or set the string value as a separator.

    CsvOpenOptions.Encoding

    This property can be used to get or set the default encoding which is UTF-8.

    CsvOpenOptions.ParseStyle

    This property can be used to specify whether the style for parsed values should be applied while converting the string values to number or date time.

    CsvOpenOptions.HasFormula

    This property can be used to specify whether the text is formula if it starts with "=".

    CsvSaveOptions.Encoding

    This property can be used to specify the default encoding which is UTF-8.

    CsvSaveOptions.ValueQuoteType

    This property can be used to get or set how to quote values in the exported text file.

    Note: DsExcel ignores this property when QuoteColumns property is set.

    CsvSaveOptions.TrimLeadingBlankRowAndColumn

    This property can be used to specify whether the leading blank rows and columns should be trimmed like in Excel.
    CsvSaveOptions.QuoteColumns

    This property can be used to specify which column values will be in quotes while the values in the remaining columns are not. The column number starts at 0, and specifying an invalid column number has no effect. The column values in quotes are indicated by CellSeparator property, which is typically set to double quote (") by default.

    Note: If the value contains special characters such as quotes or separators, it will be in quotes always.

    Refer to the following example code in order to import a .csv file.

    C#
    Copy Code
    IWorkbook workbook = new Workbook();
    
    //Method1 - Opening a csv file
    workbook.Open(@"test.csv", OpenFileFormat.Csv);
    
    //Method2 - Opening a csv file using several open options
    CsvOpenOptions options = new CsvOpenOptions();
    options.ConvertNumericData = false;
    options.ParseStyle = false;
    workbook.Open(@"test.csv", options);

    Refer to the following example code in order to export a .csv file from a workbook or a particular worksheet in the workbook.

    C#
    Copy Code
     // Save a csv file from workbook
    
     IWorkbook workbook1 = new Workbook();
    
     // Saving to a csv file 
     workbook1.Save(@"test.csv", SaveFileFormat.Csv);
    
     // Saving to a csv file with advanced settings
     CsvSaveOptions options1 = new CsvSaveOptions();
     options1.SeparatorString = "-";
     options1.ValueQuoteType = ValueQuoteType.Always;
     workbook1.Save(@"test.csv", options1);
    
    // Save a csv file from worksheet
    
     IWorkbook workbook2 = new Workbook();
     IWorksheet worksheet = workbook2.Worksheets[0];
    
     // Saving to a csv file 
     worksheet.Save(@"test.csv", SaveFileFormat.Csv);
    
     // Saving to a csv file with advanced settings
     CsvSaveOptions options2 = new CsvSaveOptions();
     options2.SeparatorString = "-";
     options2.ValueQuoteType = ValueQuoteType.Always;
     options2.QuoteColumns = new int[] { 1, 3, 4 }; // ValueQuoteType is ignored when QuoteColumns is set.
     worksheet.Save(@"test.csv", options2);
    
    Note: SeparatorString property is obsolete. You can use RowSeparator, ColumnSeparator, and CellSeparator properties instead. For more details, see Import and Export CSV File with Delimiters.