Document Solutions for Excel, Java Edition | Document Solutions
File Operations / Import and Export JSON Stream / Import and Export from JSON Without Worksheets
In This Topic
    Import and Export from JSON Without Worksheets
    In This Topic

    DsExcel allows you to convert skeleton of the workbook into a JSON stream. That is, you can export a workbook with just the worksheet without any data in them. This can be implemented by setting the SerializationOptions.setIgnoreSheets method to true and then pass it as a parameter while calling the IWorkbook.toJson method.

    You can also export the worksheet only to a separate JSON stream by using the IWorksheet.toJson(Stream stream) method. Similarly, IWorkSheet.fromJson(Stream stream) lets you import the worksheets from JSON stream when required.

    This feature is especially useful in optimizing the performance while loading large workbooks with many worksheets containing complex formula. Using the above-mentioned methods, you can load an empty workbook first with worksheet names and load the worksheet data when user selects a worksheet tab.

    The example below demonstrates how you can export the workbook and worksheet to separate JSON streams and load them in a new workbook instance when required.

    Copy Code
    // Create a new workbook
    Workbook workbook = new Workbook();
    // The old workbook.
    IWorkbook oldWorkbook = new com.grapecity.documents.excel.Workbook();
    InputStream fileStream = getResourceStream("xlsx\\12-month cash flow statement1.xlsx");;
    FileOutputStream workbookOutputJson = null;
    FileInputStream workbookInputJson = null;
    FileOutputStream worksheetOutputJson = null;
    FileInputStream worksheetInputJson = null;
    try {
        SerializationOptions serializationOptions = new SerializationOptions();
        // Export the skeleton of the workbook without worksheets to json stream. 
        workbookOutputJson = new FileOutputStream("workbookJava.json");
        oldWorkbook.toJson(workbookOutputJson, serializationOptions);
        // Import the workbook stream.
        workbookInputJson = new FileInputStream("workbookJava.json");
        // Export the worksheet to json stream. 
        worksheetOutputJson = new FileOutputStream("worksheetJava.json");
        // Import the worksheet stream.
        worksheetInputJson = new FileInputStream("worksheetJava.json");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
    } finally {
        // Close streams.
        try {
            if (workbookOutputJson != null) {
            if (workbookInputJson != null) {
            if (worksheetOutputJson != null) {
            if (worksheetInputJson != null) {
        } catch (Exception e2) {
            // TODO: handle exception
    // Save to an excel file"WorkbookToJsonWithoutSheets.xlsx");