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

    SpreadJS v16 introduced a new file format, .sjs, to work with large and complex files faster and generate smaller files (in size) when saved. The new .sjs format is a zipped file that contains multiple smaller JSON files and is structured similarly to the Excel XML structure.

    DsExcel allows you to import and export the new .sjs file format just like the XLSX, CSV, and other file formats. You can import a .sjs file using the Open method of Workbook class. Once loaded in DsExcel, it can be exported to Excel (XLSX) or back to .sjs file using the Save method of Workbook class. While loading or saving a .sjs file, you can use the new option "Sjs" in OpenFileFormat and SaveFileFormat enums.

    Refer to the following example code to import and export a .sjs file from the file name:

    C#
    Copy Code
    // Initialize Workbook.
    Workbook workbook = new Workbook();
    
    // Open .sjs file.
    workbook.Open("ProjectPlan.sjs", OpenFileFormat.Sjs);
    
    // Save .sjs file.
    workbook.Save("SaveProjectPlan.sjs", SaveFileFormat.Sjs);

    Refer to the following example code to import and export a .sjs file from a file stream:

    C#
    Copy Code
    // Initialize Workbook.
    Workbook workbook = new Workbook();
    
    // Open a .sjs file from stream.
    var importStream = new FileStream("ProjectPlan.sjs", FileMode.Open);
    workbook.Open(importStream, OpenFileFormat.Sjs);
    
    // Save the .sjs file to stream.
    var exportStream = new FileStream("SaveProjectPlan.sjs", FileMode.Create);
    workbook.Save(exportStream, SaveFileFormat.Sjs);

    In addition, DsExcel provides SjsOpenOptions and SjsSaveOptions classes to customize the import and export of a .sjs file. These options are especially useful in dealing with large files, such as those containing many formulas, styles, or unused names. These options are listed below:

    Class Options Description
    Import Options SjsOpenOptions IncludeStyles Indicates whether the style can be included when loading .sjs files. By default, it is true.
    IncludeFormulas Indicates whether the formula can be included when loading .sjs files. By default, it is true.
    Export Options SjsSaveOptions IncludeStyles Indicates whether the style can be included when saving files. By default, the value is true.
    IncludeFormulas Indicates whether the formula can be included when saving the file. By default, the value is true.
    IncludeUnusedNames Indicates whether the unused custom name can be included when saving the file. By default, the value is true.
    IncludeEmptyRegionCells Indicates whether any empty cells outside the used data range can be included while saving the file. By default, the value is true.

    Refer to the following example code to import and export a .sjs file using SjsOpenOptions and SjsSaveOptions:

    C#
    Copy Code
    // Initialize Workbook.
    Workbook workbook = new Workbook();
    
    // Open a .sjs file with formulas.
    SjsOpenOptions openOptions = new SjsOpenOptions();
    openOptions.IncludeFormulas = false;
    openOptions.IncludeStyles = false;
    workbook.Open("ProjectPlan.sjs", openOptions);
    
    // Save the .sjs file with styles.
    SjsSaveOptions saveOptions = new SjsSaveOptions();
    saveOptions.IncludeStyles = false;
    saveOptions.IncludeFormulas = true;
    saveOptions.IncludeUnusedNames = false;
    saveOptions.IncludeEmptyRegionCells = false;
    workbook.Save("SaveProjectPlan.sjs", saveOptions);

    DsExcel also provides ToSjsJson method that integrates all JSON files from the .sjs file into a single string or stream. You can also use the SjsSaveOptions with this method.

    Class Methods Description
    Workbook ToSjsJson() Generates a JSON string from a workbook. It integrates all JSON files from the .sjs file into a single string.
    ToSjsJson(SjsSaveOptions options) Generates a JSON string from a workbook using save options. It integrates all JSON files from the .sjs file into a single string.
    ToSjsJson(Stream stream) Integrates all JSON files from the .sjs file into a single string, then puts the string into the stream.
    ToSjsJson(Stream stream, SjsSaveOptions options) Integrates all JSON files from the .sjs file into a single string using save options, then puts the string into the stream.

    Refer to the following example code to export a .sjs file into a single string and save the string to a stream:

    C#
    Copy Code
    // Initialize Workbook.
    Workbook workbook = new Workbook();
    
    // Open .sjs file.
    workbook.Open("ProjectPlan.sjs", OpenFileFormat.Sjs);
    
    // Generate a JSON string for .sjs file and save it to a stream.
    var exportStream = new FileStream("SaveProjectPlan.json", FileMode.Create);
    workbook.ToSjsJson(exportStream);