Document Solutions for Excel, Java 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 Java 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:

    Java
    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:

    Java
    Copy Code
    // Create a new workbook.
    var streamworkbook = new Workbook();
    // Create a new file stream to open a file.
    InputStream openFile;
    try {
        openFile = new FileInputStream("ProjectPlan.sjs");
        // Open xltx file.
        streamworkbook.open(openFile, OpenFileFormat.Sjs);
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
      
    // Create a new file stream to save a file.
    OutputStream out;
    try {
        out = new FileOutputStream("SaveProjectPlan.sjs");
        // Save workbook as xltx file.
        streamworkbook.save(out, SaveFileFormat.Sjs);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    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:

    Java
    Copy Code
    // Initialize Workbook.
    Workbook workbook = new Workbook();
    
    // Open a .sjs file with formulas.
    SjsOpenOptions openOptions = new SjsOpenOptions();
    openOptions.setIncludeFormulas(false);
    openOptions.setIncludeStyles(false);
    workbook.open("ProjectPlan.sjs", openOptions);
    
    // Save the .sjs file with styles.
    SjsSaveOptions saveOptions = new SjsSaveOptions();
    saveOptions.setIncludeStyles(false);
    saveOptions.setIncludeFormulas(true);
    saveOptions.setIncludeUnusedNames(false);
    saveOptions.setIncludeEmptyRegionCells(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:

    Java
    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.
    OutputStream out;
    try {
        out = new FileOutputStream("SaveProjectPlan.json");
        // Save workbook as xltx file.
        workbook.toSjsJson(out);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();        
    }