Documents for Excel, Java Edition Documentation
File Operations / Import and Export JSON Stream
In This Topic
    Import and Export JSON Stream
    In This Topic

    The sole purpose of facilitating users in importing and exporting to and from json stream is to enable them to exchange and organize object data as and when required. This reference summarizes how GcExcel Java handles the import and export of json stream using Java.

    This topic includes the following tasks.

    Import and Export JSON Stream for workbook

    You can export a workbook to a json string/stream using the toJson method of the IWorkbook interface. You can also import a json string or stream to your workbook using the fromJson method of the IWorkbook interface.

    Refer to the following example code to import and export json stream.

    Copy Code
     // Create workbook 
     Workbook workbook = new Workbook();
     Workbook workbook1 = new Workbook();
     // Import an excel file"test.xlsx");
     // Import or Export from or to a JSON string
     OutputStream outputStream = new ByteArrayOutputStream();
     ByteArrayOutputStream buffer = (ByteArrayOutputStream) outputStream;
     byte[] bytes = buffer.toByteArray();
     InputStream inputStream = new ByteArrayInputStream(bytes);
    // Export workbook to an excel file"json_out.xlsx");

    Import and Export JSON Stream for worksheet

    You can export the information in a worksheet to a json string using the toJson method of the IWorksheet interface. Similarly, you can also import a json string to your worksheet using the fromJson of the IWorksheet interface. The worksheet can also be exported or imported to the same or another workbook.

    It also enables you to view a large Excel file in SpreadJS. The Excel file can be opened in GcExcel and the json string of a worksheet can be exported using the toJson method. Further, the json string of the worksheet can be transfered to client to be loaded in SpreadJS.


    Refer to the following example code to export and import json string of a worksheet.

    Copy Code
    Workbook workbook = new Workbook();
    // ToJson&FromJson can be used in combination with spreadjs
    // product:
    // GrapeCity Documents for Excel import an excel file
    String source = "ExcelJsonInput.xlsx";;
    // Open the file
    IWorkbook new_workbook = new Workbook();;
    for (IWorksheet worksheet : workbook.getWorksheets()) {
    worksheet.getRange("A1:C4").setValue(new Object[][] { { "Device", "Quantity", "Unit Price" },
    { "T540p", 12, 9850 }, { "T570", 5, 7460 }, { "Y460", 6, 5400 }, { "Y460F", 8, 6240 } });
    // GrapeCity Documents for Excel export a worksheet to json string
    String json = worksheet.toJson();
    // You can use the json string to initialize spreadjs product
    // Product spreadjs will show the excel file contents
    // You can use spreadjs product export a json string of worksheet
    // GrapeCity Documents for Excel use the json string to update content of the
    // corresponding worksheet
    // GrapeCity Documents for Excel export workbook to an excel file
    String export = "ExcelJsonOutput.xlsx";;

    SpreadJS SSJSON Support

    GcExcel Java provides support for SpreadJS SSJSON. You can import a SSJSON file created with SpreadJS Designer and save it back after modifying it as per your preferences.

    Copy Code
    // Create a new workbook
    Workbook workbook = new Workbook();
    // Load SSJSON file
        FileInputStream stream = new FileInputStream("test.ssjson");
    catch (Exception e) 
    // Save file"workbook-ssjson.xlsx");

    Import and Export SpreadJS JSON Files with Shapes

    GcExcel Java allows users to load and save Grapecity SpreadJS JSON files along with shapes. Besides supporting the import and export of SpreadJS JSON files containing shapes, users can also modify the exported SpreadJS JSON files with shapes and save them back to the original SpreadJS JSON files as and when required.

    Copy Code
    // Initialize workbook
    Workbook workbook = new Workbook();
    // Initialize another workbookWithShape
    Workbook workbookWithShape = new Workbook();
    // Fetch default worksheet of workbookWithShape
    IWorksheet worksheet = workbookWithShape.getWorksheets().get(0);
    // Add a shape in worksheet
    IShape shape = worksheet.getShapes().addShape(AutoShapeType.Parallelogram, 1, 1, 200, 100);
    IColorFormat color = shape.getFill().getColor();
    // Converting workbook containing shape to JsonString
    String jsonString = workbookWithShape.toJson();
    // GcExcel can load json string containing shapes
    // Saving workbook"4-LoadAndSaveSSJSONContainingShapes.xlsx");

    Note: The following limitations must be kept in mind while exporting Excel files with vertical text to PDF -
    • The orientation can only be set to 0, 90, -90 and 255. Other values will be treated as 0 while rendering the PDF file.
    • If the font name starts with "@" and the orientation is 255, GcExcel will ignore the "@".

    Retreive Errors while Importing JSON Files

    GcExcel provides the option to get JSON errors, if any, while importing the JSON file using fromJson method of IWorkbook interface. The error message is displayed by the getErrorMessage property of JsonError class. Two types of error messages are supported:

    Refer to the below example code which will display a formula JSON error as the JSON file containing formula error is imported in GcExcel.

    Copy Code
       // create a new workbook
    Workbook workbook = new Workbook();
    // Open JSON file contaning JSON errors
    InputStream stream = new FileInputStream("ErrorJson.json");
    List<JsonError> errors = workbook.fromJson(stream);
    for (JsonError item : errors) {
        if (item instanceof FormulaJsonError) {
            FormulaJsonError fError = (FormulaJsonError) item;
                    .println(fError.getErrorMessage() + " "
                            + workbook.getWorksheets().get(fError.getWorksheetName())
                                    .getRange(fError.getRow(), fError.getColumn()).toString()
                            + " " + fError.getFormula());
        if (item instanceof DataValidationJsonError) {
            DataValidationJsonError dError = (DataValidationJsonError) item;
            System.out.println(dError.getErrorMessage() + " "
                    + workbook.getWorksheets().get(dError.getWorksheetName()).getRange(dError.getRange().toString())
                    + " " + dError.getErrorContent());


    If the data validation in JSON file has error in its formula, Data Validation JSON error will be generated.