Document Solutions for Excel, .NET Edition | Document Solutions
File Operations / Import and Export JSON Stream
In This Topic
    Import and Export JSON Stream
    In This Topic

    DsExcel .NET supports the import and export of a json stream using .NET core.

    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.

    C#
    Copy Code
    //ToJson&FromJson can be used in combination with spreadjs product
    
    //Import an excel file.
    //change the path to real source file path.
    string source = "savingfile.xlsx";
    workbook.Open(source);
    
    //Export to a json string.
    var jsonstr = workbook.ToJson();
    //use the json string to initialize spreadjs product.
    //spreadjs will show the excel file contents.
    
    //spreadjs product export a json string.
    //Use the json string to initialize.
    workbook.FromJson(jsonstr);
    //Export workbook to an excel file.
    //change the path to real export file path.
    
    string export = "export.xlsx";
    workbook.Save(export);
    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 String 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.

    Limitations

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

    C#
    Copy Code
    var workbook = new GrapeCity.Documents.Excel.Workbook();
    
    //ToJson&FromJson can be used in combination with spreadjs product.
    
    //Documents for Excel import an excel file
    string source = "ExcelJsonInput.xlsx";
    workbook.Open(source);
    
    //Open the file
    GrapeCity.Documents.Excel.Workbook new_workbook = new GrapeCity.Documents.Excel.Workbook();
    new_workbook.Open(source);
    
    foreach (IWorksheet worksheet in workbook.Worksheets)
    {
        worksheet.Range["D40:F40"].Value = new string[] { "Device", "Quantity", "Unit Price" };
        worksheet.Range["D41:F44"].Value = new object[,]
       { { "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
        new_workbook.Worksheets[worksheet.Name].FromJson(json);
    }
    
    //Documents for Excel export workbook to an excel file
    string export = "ExcelJsonOutput.xlsx";
    new_workbook.Save(export);
    

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

    C#
    Copy Code
    Workbook workbook = new Workbook();
    IList<JsonError> errors = workbook.FromJson(File.OpenRead("ErrorJson.json"));
    foreach (JsonError item in errors)
    {
        if (item is FormulaJsonError)
        {
            FormulaJsonError fError = item as FormulaJsonError;
            Console.WriteLine(fError.ErrorMessage + " " + workbook.Worksheets[fError.WorksheetName].Range[fError.Row, fError.Column].ToString() + " " + fError.Formula);
        }
        if (item is DataValidationJsonError)
        {
            DataValidationJsonError dError = item as DataValidationJsonError;
            Console.WriteLine(dError.ErrorMessage + " " + workbook.Worksheets[dError.WorksheetName].Range[dError.Range.ToString()] + " " + dError.ErrorContent);
        }
    }

    Limitation

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