Document Solutions for Excel, Java 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 Java 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 methods of the CsvOpenOptions class and the CsvSaveOptions class in order to configure several open and save options in a workbook.

    Methods Description

    CsvOpenOptions.setConvertNumericData

    CsvOpenOptions.getConvertNumericData

    Used to get or set a value that indicates whether the string in text file is converted to numeric data.

    CsvOpenOptions.setConvertDateTimeData

    CsvOpenOptions.getConvertDateTimeData

    Used to get or set a value that indicates whether the string in text file is converted to date data.

    CsvOpenOptions.setSeparatorString

    CsvOpenOptions.getSeparatorString

    Used to get or set the string value as a separator.

    CsvOpenOptions.setEncoding

    CsvOpenOptions.getEncoding

    Used to get or set the default encoding which is UTF-8.

    CsvOpenOptions.getParseStyle

    CsvOpenOptions.setParseStyle

    Used to specify whether the style for parsed values should be applied while converting the string values to number or date time.

    CsvOpenOptions.setHasFormula

    CsvOpenOptions.getHasFormula

    Used to specify whether the text is formula if it starts with "=".

    CsvSaveOptions.setSeparatorString

    CsvSaveOptions.getSeparatorString

    Used to get or set the string value as the separator. By default, this value is a comma separator.

    CsvSaveOptions.setEncoding

    CsvSaveOptions.getEncoding

    Used to specify the default encoding which is UTF-8.

    CsvSaveOptions.getValueQuoteType

    CsvSaveOptions.setValueQuoteType

    Used to get or set how to quote values in the exported text file.

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

    CsvSaveOptions.getTrimLeadingBlankRowAndColumn

    CsvSaveOptions.setTrimLeadingBlankRowAndColumn

    Used to specify whether the leading blank rows and columns should be trimmed like in Excel.

    CsvSaveOptions.getQuoteColumns

    CsvSaveOptions.setQuoteColumns

    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.

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

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

    Java
    Copy Code
    Workbook workbook = new Workbook();
            
    // Opening a CSV file 
    workbook.open("documents\source.csv", OpenFileFormat.Csv);
            
    // Opening a CSV file using several open options
    CsvOpenOptions options = new CsvOpenOptions();
    options.setSeparatorString("-");
    workbook.open("documents\source.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.

    Java
    Copy Code
    // Saving a CSV file from workbook
    Workbook workbook = new Workbook();
            
    // Saving to a CSV file
    workbook.save("SaveToCsvFile.csv", SaveFileFormat.Csv);
            
    // Saving to a csv file with advanced settings
    CsvSaveOptions options = new CsvSaveOptions();
    options.setSeparatorString("-");
    options.setValueQuoteType(ValueQuoteType.Always);
    options.setQuoteColumns(new int[] { 1, 3, 4 });  //ValueQuoteType is ignored when QuoteColumns is set.
    workbook.save("SaveToCsvFile.csv", options);