Document Solutions for Excel, Java Edition | Document Solutions
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 DsExcel 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. The method even lets you export the workbooks having formulas with external reference. 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");
    Note: To get better performance in case of large workbooks with many worksheets having complex formula, you can export and import the workbook and worksheets to separate JSON streams. For more information, see Import and Export from JSON Without Worksheets.

    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 DsExcel 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.
    // 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 } });
    // 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
    // Documents for Excel use the json string to update content of the
    // corresponding worksheet
    // Documents for Excel export workbook to an excel file
    String export = "ExcelJsonOutput.xlsx";;

    Retreive Errors while Importing JSON Files

    DsExcel 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 DsExcel.

    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.