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 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:
Java |
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. OutputStream outputStream; try { outputStream = new FileOutputStream("CashFlow.json"); workbook.toSjsJson(outputStream); outputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } // Import a JSON string from the stream and save it as an Excel file. InputStream inputStream; try { inputStream = new FileInputStream("CashFlow.json"); workbook.fromSjsJson(inputStream); } catch (FileNotFoundException e) { e.printStackTrace(); } // Save workbook. workbook.save("CashFlow.xlsx"); |
DsExcel allows you to render column width based on pixels instead of characters using setPixelBasedColumnWidth method 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:
Java |
Copy Code |
---|---|
// Initialize WorkbookOptions. WorkbookOptions workbookOptions = new WorkbookOptions(); // Enable pixel-based column width for the workbook. workbookOptions.setPixelBasedColumnWidth(true); var workbook = new Workbook(workbookOptions); // Open SSJSON file. workbook.open("Event budget.sjs"); IWorksheet worksheet = workbook.getWorksheets().get(0); // Save to a PDF and PNG file. workbook.save("SavePDFWithPixelBasedColumnWidth.pdf"); worksheet.toImage("SavePDFWithPixelBasedColumnWidth.png"); |