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);

    Import and Export .sjs JSON String or Stream

    DsExcel provides ToSjsJson method that integrates all JSON files from the .sjs file into a single string or stream. Furthermore, DsExcel also provides FromSjsJson, which loads a string or stream of all JSON files generated from .sjs file. These methods also support SjsOpenOptions and SjsSaveOptions.

    Methods Description
    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.
    FromSjsJson(string json) Generates a workbook from a JSON string containing the contents of .sjs file format.
    FromSjsJson(string json, SjsOpenOptions options) Generates a workbook from a JSON string using open options containing the contents of .sjs file format.
    FromSjsJson(Stream stream) Generates a workbook from a JSON stream containing the contents of .sjs file format.
    FromSjsJson(Stream stream, SjsOpenOptions options) Generates a workbook from a JSON stream using open options containing the contents of .sjs file format.

    Refer to the following example code to export and import a single JSON string generated from .sjs file:

    C#
    Copy Code
    // Initialize Workbook.
    Workbook workbook = new Workbook();
    
    // Open workbook.
    workbook.Open("12-month cash flow statement1.sjs");
    
    // Generate a JSON string for .sjs file and save it to a stream.
    var exportStream = new FileStream("CashFlow.json", FileMode.Create);
    workbook.ToSjsJson(exportStream);
    exportStream.Close();
    
    
    // Import a JSON string from the stream and save it as an Excel file.
    var inputStream = new FileStream("CashFlow.json", FileMode.Open);
    workbook.FromSjsJson(inputStream);
    
    // Save workbook.
    workbook.Save("CashFlow.xlsx");

    Pixel Based Column-Width

    DsExcel allows you to render column width based on pixels instead of characters using PixelBasedColumnWidth property of WorkbookOptions class when exporting a .sjs file as a PDF or image.

    Refer to the following example code to render column width based on pixels when exporting as a PDF or image:

    C#
    Copy Code
    // Initialize WorkbookOptions.
    WorkbookOptions workbookOptions = new WorkbookOptions();
    
    // Enable pixel-based column width for the workbook.
    workbookOptions.PixelBasedColumnWidth = true;
    var workbook = new Workbook(workbookOptions);
    
    // Open .sjs file.
    workbook.Open("Event budget.sjs");
    IWorksheet worksheet = workbook.Worksheets[0];
    
    // Save to a PDF and PNG file.
    workbook.Save("SavePDFWithPixelBasedColumnWidth.pdf");
    worksheet.ToImage("SavePDFWithPixelBasedColumnWidth.png");