Documents for Excel .NET Edition | GCDocuments
File Operations / Import and Export CSV File
In This Topic
    Import and Export CSV File
    In This Topic

    This section summarizes how GcExcel .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.SeparatorString

    This property can be used to get or set the string value as the separator. By default, this value is a comma separator.

    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.

    CsvSaveOptions.TrimLeadingBlankRowAndColumn

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

    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;
     worksheet.Save(@"test.csv", options2);