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 | Option | 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. | ||
IgnoreRangeOutOfRowCoulumnCount | Indicates whether to ignore data out of RowCount and ColumnCount while saving the file. By default, the value is false. | ||
IncludeAutoMergedCells | Indicates whether to include the automatically merged cells. By default, the value is false. | ||
IncludeBindingSource | Indicates whether to include the binding source when 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"); |
DsExcel allows you to set the count of rows and columns in a worksheet while performing .sjs I/O. setRowCount and setColumnCount methods of the IWorksheet interface enable you to achieve the same. setRowCount and setColumnCount methods also increase or decrease the row and column count when you insert or delete the rows or columns.
You can also use the setIgnoreRangeOutOfRowColumnCount method of SjsSaveOptions class to choose whether to ignore the data outside the range of the specified row and column count. The default value of this method is false, which exports the data outside the range of the specified row and column count to .sjs.
Refer to the following example code to set the row and column count in a worksheet and export it to a .sjs file:
Java |
Copy Code |
---|---|
// Create a new workbook. var workbook = new Workbook(); // Open .sjs file. workbook.open("LoanDetails.sjs", OpenFileFormat.Sjs); // Access first worksheet. IWorksheet worksheet = workbook.getWorksheets().get(0); // Adjust row and column count to 4. worksheet.setRowCount(4); worksheet.setColumnCount(4); // Control the exclusion of content outside the row or column count from being exported. SjsSaveOptions saveOptions = new SjsSaveOptions(); saveOptions.setIgnoreRangeOutOfRowColumnCount(true); // Save the file in .sjs file format. workbook.save("IgnoreDataOption.sjs", saveOptions); |
Limitations
DsExcel allows you to add decorations to cells or cell ranges in the form of corner folds or icons using setDecoration method of IRange interface, which uses the instances of ICornerFold and ICellDecorationIcon interfaces. DsExcel also provides CornerPosition and IconPosition enumerations to set the position of the corner fold and icon.
You must create instances of ICornerFold and ICellDecorationIcon interfaces using CornerFold and CellDecorationIcon constructors and configure the corner fold and icon before setting the decoration of a cell or cell range.
Refer to the following example code to add cell decoration to the cells:
Java |
Copy Code |
---|---|
// Create a new workbook. var workbook = new Workbook(); // Access first worksheet. IWorksheet worksheet = workbook.getWorksheets().get(0); // Add values to cell range. worksheet.getRange("C4").setValue("FY 2019"); worksheet.getRange("C5").setValue("Sales"); worksheet.getRange("C6").setValue("Monthly"); String[] months = { "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar" }; int[] monthlySales = { 188897, 208146, 226196, 277318, 263273, 259845, 241047, 256306, 195845, 204934, 257852, 227779 }; for (int i = 0; i < months.length; i++) { worksheet.getRange("B" + (7 + i)).setValue(months[i]); worksheet.getRange("C" + (7 + i)).setValue(monthlySales[i]); } // Set color using string. worksheet.getRange("B4:C6").getInterior().setColor(Color.GetLightBlue()); worksheet.getRange("C4:C6").getBorders().setColor(Color.GetBlack()); worksheet.getRange("B7:B18").getInterior().setColor(Color.GetLightGray()); worksheet.getRange("B7:C18").getBorders().setColor(Color.GetBlack()); // Set cell range style. worksheet.getRange("B4:B6").getBorders().get(BordersIndex.EdgeLeft).setLineStyle(BorderLineStyle.Thin); worksheet.getRange("B4:B6").getBorders().get(BordersIndex.EdgeTop).setLineStyle(BorderLineStyle.Thin); worksheet.getRange("B4:B6").merge(); worksheet.getRange("C2:D18").setColumnWidth(15); worksheet.getRange("B2:D18").setHorizontalAlignment(HorizontalAlignment.Center); worksheet.getRange("B2:C18").setVerticalAlignment(VerticalAlignment.Center); worksheet.getRange("B4:C6").getFont().setBold(true); worksheet.getRange("C7:C18").setNumberFormat("#,##0"); worksheet.getRange("C2").setHorizontalAlignment(HorizontalAlignment.Right); // Hightlight highest sales using cell decoration. ICornerFold cornerFold1 = new CornerFold(Color.GetRed(), EnumSet.of(CornerPosition.LeftTop), 8); ICellDecorationIcon cellDecorationIcon1 = new CellDecorationIcon( "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTIiIGhlaWdodD0iMTIiIHZ" + "pZXdCb3g9IjAgMCAxMiAxMiIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3" + "cudzMub3JnLzIwMDAvc3ZnIj4KPHJlY3Qgd2lkdGg9IjEyIiBoZWlnaHQ9IjEyI" + "iBmaWxsPSJ0cmFuc3BhcmVudCIvPgo8cGF0aCBmaWxsLXJ1bGU9ImV2ZW5vZGQiI" + "GNsaXAtcnVsZT0iZXZlbm9kZCIgZD0iTTcgOUg1TDUgNS45NjA0NmUtMDhIN0w3" + "IDlaTTYgMTBDNi41NTIyOCAxMCA3IDEwLjQ0NzcgNyAxMUM3IDExLjU1MjMgNi4" + "1NTIyOCAxMiA2IDEyQzUuNDQ3NzIgMTIgNSAxMS41NTIzIDUgMTFDNSAxMC40N" + "Dc3IDUuNDQ3NzIgMTAgNiAxMFoiIGZpbGw9InJlZCIvPgo8L3N2Zz4K", 12, 12, IconPosition.OutsideRight); worksheet.getRange("C10").setDecoration(new com.grapecity.documents.excel.CellDecoration(cornerFold1, Collections.singletonList(cellDecorationIcon1))); worksheet.getRange("D10").setValue("Highest"); // Hightlight lowest sales using cell decoration. ICornerFold cornerFold2 = new CornerFold(Color.GetGreen(), EnumSet.of(CornerPosition.LeftTop), 8); ICellDecorationIcon cellDecorationIcon2 = new CellDecorationIcon( "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTIiIGhlaWd" + "odD0iMTIiIHZpZXdCb3g9IjAgMCAxMiAxMiIgZmlsbD0ibm9uZSIge" + "G1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHJlY3Qgd2" + "lkdGg9IjEyIiBoZWlnaHQ9IjEyIiBmaWxsPSJ0cmFuc3BhcmVudCIvPgo8" + "cGF0aCBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGNsaXAtcnVsZT0iZXZlbm9kZ" + "CIgZD0iTTcgOUg1TDUgNS45NjA0NmUtMDhIN0w3IDlaTTYgMTBDNi41NT" + "IyOCAxMCA3IDEwLjQ0NzcgNyAxMUM3IDExLjU1MjMgNi41NTIyOCAxMi" + "A2IDEyQzUuNDQ3NzIgMTIgNSAxMS41NTIzIDUgMTFDNSAxMC40NDc3I" + "DUuNDQ3NzIgMTAgNiAxMFoiIGZpbGw9ImdyZWVuIi8+Cjwvc3ZnPgo=", 12, 12, IconPosition.OutsideRight); worksheet.getRange("C7").setDecoration(new com.grapecity.documents.excel.CellDecoration(cornerFold2, Collections.singletonList(cellDecorationIcon2))); worksheet.getRange("D7").setValue("Lowest"); // Save the workbook to .sjs document. workbook.save("CellDecoration.sjs"); |
Refer to the following example code to remove the cell decoration completely:
Java |
Copy Code |
---|---|
// Remove cell decoration. worksheet.getRange("C7").setDecoration(null); |
Refer to the following example code to remove only cell icon decoration:
Java |
Copy Code |
---|---|
// Remove icon decoration. worksheet.getRange("C7").setDecoration(new CellDecoration(cornerFold2, null)); // Or worksheet.getRange("C7").setDecoration(new CellDecoration(cornerFold2)); |
Refer to the following example code to remove only cell corner fold decoration:
Java |
Copy Code |
---|---|
// Remove corner fold decoration. worksheet.getRange("C7").setDecoration(new CellDecoration(null, Arrays.asList(cellDecorationIcon2))); // Or worksheet.getRange("C7").setDecoration(new CellDecoration(Arrays.asList(cellDecorationIcon2))); |
DsExcel enables you to control whether to export the bound data source to the file when exporting to .sjs file using setIncludeBindingSource method of SjsSaveOptions.
Refer to the following example code to exclude the binding source when exporting to .sjs file:
Java |
Copy Code |
---|---|
// Create a new workbook. var workbook = new Workbook(); // Define a JSON data source. String dataSource = "{ \"ds\":" + "[\n" + " {\"Area\": \"North America\",\"City\": \"Chicago\",\"Category\": \"Consumer Electronics\",\"Name\": \"Bose 785593-0050\",\"Revenue\": 92800},\n" + " {\"Area\": \"North America\",\"City\": \"New York\",\"Category\": \"Consumer Electronics\",\"Name\": \"Bose 785593-0050\",\"Revenue\": 92800},\n" + " {\"Area\": \"South America\",\"City\": \"Santiago\",\"Category\": \"Consumer Electronics\",\"Name\": \"Bose 785593-0050\",\"Revenue\": 19550},\n" + " {\"Area\": \"Europe\",\"City\": \"Berlin\",\"Category\": \"Consumer Electronics\",\"Name\": \"Sony WH-1000XM4\",\"Revenue\": 30000},\n" + " {\"Area\": \"Asia\",\"City\": \"Tokyo\",\"Category\": \"Consumer Electronics\",\"Name\": \"Sony WH-1000XM4\",\"Revenue\": 45000},\n" + " {\"Area\": \"North America\",\"City\": \"Los Angeles\",\"Category\": \"Consumer Electronics\",\"Name\": \"Apple AirPods\",\"Revenue\": 60000},\n" + " {\"Area\": \"Europe\",\"City\": \"Paris\",\"Category\": \"Consumer Electronics\",\"Name\": \"Apple AirPods\",\"Revenue\": 55000},\n" + " {\"Area\": \"Asia\",\"City\": \"Seoul\",\"Category\": \"Consumer Electronics\",\"Name\": \"Samsung Galaxy Buds\",\"Revenue\": 40000},\n" + " {\"Area\": \"South America\",\"City\": \"Buenos Aires\",\"Category\": \"Consumer Electronics\",\"Name\": \"Samsung Galaxy Buds\",\"Revenue\": 35000},\n" + " {\"Area\": \"North America\",\"City\": \"Toronto\",\"Category\": \"Consumer Electronics\",\"Name\": \"Bose 785593-0050\",\"Revenue\": 50000}\n" + " ]" + "}"; // Add data source to worksheet. IWorksheet dataSourceSheet = workbook.getWorksheets().add(); dataSourceSheet.setName("DataSource"); ITable table = dataSourceSheet.getTables().add(dataSourceSheet.getRange("A1:E4"), true); // Set binding path. table.setBindingPath("ds"); table.getColumns().get(0).setDataField("Area"); table.getColumns().get(1).setDataField("City"); table.getColumns().get(2).setDataField("Category"); table.getColumns().get(3).setDataField("Name"); table.getColumns().get(4).setDataField("Revenue"); // Set data source. dataSourceSheet.setDataSource(new JsonDataSource(dataSource)); // Create pivot table sheet. IWorksheet pivotSheet = workbook.getWorksheets().get(0); pivotSheet.setName("PivotSheet"); // Create pivot table. IPivotCache pivotcache = workbook.getPivotCaches().create(table); IPivotTable pivottable = pivotSheet.getPivotTables().add(pivotcache, pivotSheet.getRange("A1"), "pivottable1"); // Configure pivot table fields. IPivotField fieldArea = pivottable.getPivotFields().get("Area"); fieldArea.setOrientation(PivotFieldOrientation.RowField); IPivotField fieldCity = pivottable.getPivotFields().get("City"); fieldCity.setOrientation(PivotFieldOrientation.RowField); IPivotField fieldName = pivottable.getPivotFields().get("Name"); fieldName.setOrientation(PivotFieldOrientation.ColumnField); IPivotField fieldRevenue = pivottable.getPivotFields().get("Revenue"); fieldRevenue.setOrientation(PivotFieldOrientation.DataField); pivotSheet.getUsedRange().autoFit(); pivottable.setColumnGrand(false); pivottable.setRowGrand(false); pivottable.refresh(); SjsSaveOptions saveOptions = new SjsSaveOptions(); // Set IncludeBindingSource property to false to exclude the binding source from being exported. saveOptions.setIncludeBindingSource(false); // Save the workbook. workbook.save("IncludeBindingSourceOption.sjs", saveOptions); |