[]
        
(Showing Draft Content)

CsvOpenOptions

Class CsvOpenOptions

java.lang.Object
com.grapecity.documents.excel.OpenOptionsBase
com.grapecity.documents.excel.CsvOpenOptions

public class CsvOpenOptions extends OpenOptionsBase
Represents options for opening a CSV file.

Use this class to configure how CSV content is interpreted when it is opened, including column separator, row separator, and quote character, encoding, numeric and date conversion, formula recognition, parsed-value styling, and a custom ICsvParser.

Instances of this class use OpenFileFormat.Csv as the target format.


 byte[] csvBytes = "Name,Value\r\nTest,001".getBytes(StandardCharsets.UTF_8);
 CsvOpenOptions options = new CsvOpenOptions();
 options.setColumnSeparator(",");
 options.setConvertNumericData(false);
 workbook.open(new ByteArrayInputStream(csvBytes), options);
 Object cellValue = workbook.getWorksheets().get(0).getRange("B2").getValue();
 
  • Constructor Details

    • CsvOpenOptions

      public CsvOpenOptions()
      Creates a new CsvOpenOptions instance for opening CSV files.

      This constructor initializes the options with the CSV file format and creates the internal option data used to control CSV parsing behavior, such as numeric and date conversion, separators, and encoding.

      
       byte[] csvBytes = "Name,Value\r\nTest,001".getBytes(StandardCharsets.UTF_8);
       CsvOpenOptions options = new CsvOpenOptions();
       options.setConvertNumericData(false);
       options.setColumnSeparator(",");
       workbook.open(new ByteArrayInputStream(csvBytes), options);
       Object cellValue = workbook.getWorksheets().get(0).getRange("B2").getValue();
       
  • Method Details

    • getConvertNumericData

      public final boolean getConvertNumericData()
      Gets a value that indicates whether strings in a CSV text file are converted to numeric data when the file is opened.

      The default value is true. When this property is true, text that can be recognized as numeric data is converted during CSV import.

      
       CsvOpenOptions options = new CsvOpenOptions();
       options.setConvertNumericData(false);
       boolean convertNumericData = options.getConvertNumericData();
       workbook.open("path/to/source.csv", options);
       
      Returns:
      true if strings in the text file are converted to numeric data during CSV import; otherwise, false.
    • setConvertNumericData

      public final void setConvertNumericData(boolean value)
      Sets a value that indicates whether strings in a CSV text file are converted to numeric data when the file is opened.

      The default value is true. When this property is true, text that can be recognized as numeric data is converted during CSV import.

      
       CsvOpenOptions options = new CsvOpenOptions();
       options.setConvertNumericData(false);
       workbook.open("path/to/source.csv", options);
       
      Parameters:
      value - true if strings in the text file are converted to numeric data during CSV import; otherwise, false.
    • getConvertDateTimeData

      public final boolean getConvertDateTimeData()
      Gets a value that indicates whether strings in a CSV text file are converted to date data when the file is opened.

      The default value is true. When this property is true, text that can be recognized as date or time data is converted during CSV import.

      
       CsvOpenOptions options = new CsvOpenOptions();
       options.setConvertDateTimeData(false);
       boolean convertDateTimeData = options.getConvertDateTimeData();
       workbook.open("path/to/data.csv", options);
       
      Returns:
      true if strings in the text file are converted to date data during CSV import; otherwise, false.
    • setConvertDateTimeData

      public final void setConvertDateTimeData(boolean value)
      Sets a value that indicates whether strings in a CSV text file are converted to date data when the file is opened.

      The default value is true. When this property is true, text that can be recognized as date or time data is converted during CSV import.

      
       CsvOpenOptions options = new CsvOpenOptions();
       options.setConvertDateTimeData(false);
       workbook.open("path/to/data.csv", options);
       
      Parameters:
      value - true if strings in the text file are converted to date data during CSV import; otherwise, false.
    • getColumnSeparator

      public final String getColumnSeparator()
      Gets the column separator used when opening CSV data.

      The column separator determines how fields are split in each row when a CSV file is opened. The default value is ",". Use setColumnSeparator(String) to change it before opening a CSV file.

      When opening CSV content, the column separator, row separator, and quote character must be distinct and must not contain one another.

      
       CsvOpenOptions options = new CsvOpenOptions();
       options.setColumnSeparator(",");
       String separator = options.getColumnSeparator();
       workbook.open("path/to/source.csv", options);
       
      Returns:
      The current column separator string.
    • setColumnSeparator

      public final void setColumnSeparator(String value)
      Sets the column separator used when opening CSV data.

      When opening CSV content, the column separator, row separator, and quote character must be distinct and must not contain one another.

      
       CsvOpenOptions options = new CsvOpenOptions();
       options.setColumnSeparator(",");
       workbook.open("path/to/source.csv", options);
       
      Parameters:
      value - The current column separator string.
    • getRowSeparator

      public final String getRowSeparator()
      Gets the row separator used when opening a CSV file.

      The row separator is the string delimiter that separates records in the source CSV data.

      When opening CSV content, the column separator, row separator, and quote character must be distinct and must not contain one another.

      
       CsvOpenOptions options = new CsvOpenOptions();
       options.setRowSeparator("\r\n");
       String rowSeparator = options.getRowSeparator();
       workbook.open("path/to/source.csv", options);
       
      Returns:
      The row separator string for CSV parsing.
    • setRowSeparator

      public final void setRowSeparator(String value)
      Sets the row separator used when opening a CSV file.

      The row separator is the string delimiter that separates records in the source CSV data.

      When opening CSV content, the column separator, row separator, and quote character must be distinct and must not contain one another.

      
       CsvOpenOptions options = new CsvOpenOptions();
       options.setRowSeparator("\r\n");
       workbook.open("path/to/source.csv", options);
       
      Parameters:
      value - The row separator string for CSV parsing.
    • getCellSeparator

      public final char getCellSeparator()
      Gets the quote character used to enclose quoted cell text in CSV content.

      The default value is '"'.

      When opening CSV content, the column separator, row separator, and quote character must be distinct and must not contain one another.

      
       CsvOpenOptions options = new CsvOpenOptions();
       options.setCellSeparator('\'');
       char cellSeparator = options.getCellSeparator();
       workbook.open("path/to/source.csv", options);
       
      Returns:
      The quote character used to enclose quoted cell text in CSV content.
    • setCellSeparator

      public final void setCellSeparator(char value)
      Sets the quote character used to enclose quoted cell text in CSV content.

      The default value is '"'.

      When opening CSV content, the column separator, row separator, and quote character must be distinct and must not contain one another.

      
       CsvOpenOptions options = new CsvOpenOptions();
       options.setCellSeparator('\'');
       workbook.open("path/to/source.csv", options);
       
      Parameters:
      value - The quote character used to enclose quoted cell text in CSV content.
    • setSeparatorString

      @Deprecated public final void setSeparatorString(String value)
      Deprecated.
      Sets the column separator string used when opening a CSV file.

      This method is deprecated. Use setColumnSeparator(String) instead.

      Parameters:
      value - The column separator string.
    • getSeparatorString

      @Deprecated public final String getSeparatorString()
      Deprecated.
      Gets the column separator string used when opening a CSV file.

      This method is deprecated. Use getColumnSeparator() instead.

      Returns:
      The column separator string.
    • getEncoding

      public final String getEncoding()
      Gets the default encoding used to open a CSV file.

      The default value is utf-8.

      
       CsvOpenOptions options = new CsvOpenOptions();
       options.setEncoding("utf-8");
       String encoding = options.getEncoding();
       workbook.open("path/to/source.csv", options);
       
      Returns:
      The default encoding name for opening a CSV file.
    • setEncoding

      public final void setEncoding(String value)
      Sets the default encoding used to open a CSV file.

      The default value is utf-8.

      
       CsvOpenOptions options = new CsvOpenOptions();
       options.setEncoding("utf-8");
       workbook.open("path/to/source.csv", options);
       
      Parameters:
      value - The default encoding name for opening a CSV file.
    • getParseStyle

      public final boolean getParseStyle()
      Gets whether styles are applied to parsed values when string values are converted to numbers or date/time values.

      The default value is true. Use setParseStyle(boolean) to control whether style information is applied during parsing.

      
       CsvOpenOptions options = new CsvOpenOptions();
       options.setParseStyle(false);
       boolean parseStyle = options.getParseStyle();
       workbook.open("path/to/source.csv", options);
       
      Returns:
      true if styles are applied to parsed numeric or date/time values; otherwise, false.
    • setParseStyle

      public final void setParseStyle(boolean value)
      Sets whether styles are applied to parsed values when string values are converted to numbers or date/time values.
      
       CsvOpenOptions options = new CsvOpenOptions();
       options.setParseStyle(false);
       workbook.open("path/to/source.csv", options);
       
      Parameters:
      value - true if styles are applied to parsed numeric or date/time values; otherwise, false.
    • getHasFormula

      public final boolean getHasFormula()
      Gets a value indicating whether text that starts with "=" is treated as a formula when a CSV file is opened.

      The default value is true.

      
       CsvOpenOptions options = new CsvOpenOptions();
       options.setHasFormula(false);
       boolean hasFormula = options.getHasFormula();
       workbook.open("path/to/data.csv", options);
       
      Returns:
      true if text that starts with "=" is treated as a formula when opening a CSV file; otherwise, false.
    • setHasFormula

      public final void setHasFormula(boolean value)
      Sets a value indicating whether text that starts with "=" is treated as a formula when a CSV file is opened.

      The default value is true.

      
       CsvOpenOptions options = new CsvOpenOptions();
       options.setHasFormula(false);
       workbook.open("path/to/data.csv", options);
       
      Parameters:
      value - true if text that starts with "=" is treated as a formula when opening a CSV file; otherwise, false.
    • getParser

      public ICsvParser getParser()
      Gets the custom parser used when opening CSV content.

      The parser is called for each parsed CSV cell and can inspect the original text or modify the imported value and number format.

      
       CsvOpenOptions options = new CsvOpenOptions();
       ICsvParser parser = new ICsvParser() {
           public void Parse(CsvParseResult result, CsvParseContext context) {
               result.setValue(context.getText());
           }
       };
       options.setParser(parser);
       ICsvParser customParser = options.getParser();
       workbook.open("path/to/source.csv", options);
       
      Returns:
      The custom parser used when opening CSV content.
    • setParser

      public void setParser(ICsvParser parser)
      Sets the custom parser used when opening CSV data.

      The parser is called for each parsed CSV cell and can inspect the original text or modify the imported value and number format.

      
       CsvOpenOptions options = new CsvOpenOptions();
       class LeadingZeroParser implements ICsvParser {
           public void Parse(CsvParseResult csvParseResult, CsvParseContext context) {
               if (context.getText().startsWith("00")) {
                   csvParseResult.setValue(context.getText());
               }
           }
       }
       options.setParser(new LeadingZeroParser());
       workbook.open("path/to/data.csv", options);
       
      Parameters:
      parser - The custom parser to use when opening CSV content.