[]
GcExcel .NET supports the import and export of a json stream using .NET core.
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.
//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);
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.
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
Importing worksheet json to another workbook on server might cause data loss or conflict
Cell styles used in SpreadJS ssjson are lost in Excel after using Worksheet.toJSON()
SpreadJS doesn't support all the page settings of Excel. Hence, GcExcel does not get all the settings when imported from ssjson.
Refer to the following example code to export and import json string of a worksheet.
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);
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:
Formula JSON Error - Implemented using the FormulaJsonError class and can be raised in case of a formula error in JSON file
Data Validation JSON Error - Implemented using the DataValidationJsonError class and can be raised in case of a data validation error in JSON file
Refer to the below example code which will display a formula JSON error as the JSON file containing formula error is imported in GcExcel.
Workbook workbook = new Workbook();
IList 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.