Documents for Excel .NET Edition | GCDocuments
File Operations / Import and Export JSON Stream
In This Topic
    Import and Export JSON Stream
    In This Topic

    GcExcel .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. 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
    
    //GcExcel import an excel file.
    //change the path to real source file path.
    string source = "savingfile.xlsx";
    workbook.Open(source);
    
    //GcExcel 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.
    //GcExcel use the json string to initialize.
    workbook.FromJson(jsonstr);
    //GcExcel export workbook to an excel file.
    //change the path to real export file path.
    
    string export = "export.xlsx";
    workbook.Save(export);

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

    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:http://spread.grapecity.com/spreadjs/sheets/
    
    //GrapeCity 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 } };
    
        //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
        new_workbook.Worksheets[worksheet.Name].FromJson(json);
    }
    
    //GrapeCity Documents for Excel export workbook to an excel file
    string export = "ExcelJsonOutput.xlsx";
    new_workbook.Save(export);
    

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

    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.