We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Saving workbook using SaveSettings causes JSON deserialization exception

I am trying to save a workbook using my ASP.NET Core 6 backend API. The workbook is sent to a controller method on my API as a `SaveSettings` object from my frontend app which has the ejs-spreadsheet control (Angular). I want to write the workbook to a Stream as I intend to send the file to an external storage location (i.e. not on disk).


[HttpPost("Save")]
public async Task<IActionResult> SaveAsync([FromBody] SaveSettings saveSettings) 
{
// ...
    Stream fileStream = Workbook.Save<Stream>(saveSettings);
// ...
}

I have installed these nuget packages:

Syncfusion.EJ2.AspNet.Core - 19.3.0.57

Syncfusion.EJ2.Spreadsheet.AspNet.Core - 19.3.0.57

Syncfusion.XlsIO.Net.Core - 19.3.0.57


My Newtonsoft.Json version is 13.0.1.


When Workbook.Save is invoked, I get the following error:

System.ArgumentNullException: 'Value cannot be null. (Parameter 'value')'
at Newtonsoft.Json.Utilities.ValidationUtils.ArgumentNotNull(Object value, String parameterName)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
   at Syncfusion.EJ2.Spreadsheet.SaveHelper.GetDeserializedObject[T](String JSONData)
   at Syncfusion.EJ2.Spreadsheet.SaveHelper..ctor(SaveSettings saveSettings)
   at Syncfusion.EJ2.Spreadsheet.Workbook.Save[T](SaveSettings saveSettings)


From the stack trace, the error appears to occur when deserializing saveSettings.JSONData into a 

Syncfusion.EJ2.Spreadsheet.Spreadsheet object. Upon inspection of the Syncfusion.EJ2.Spreadsheet assembly, the issue seems to be in the first line of the following:

internal SaveHelper(SaveSettings saveSettings)
{
    spreadData = GetDeserializedObject<Spreadsheet>(saveSettings.JSONData);
    pdfLayoutSettings = GetDeserializedObject<PDFLayoutSettings(saveSettings.PdfLayoutSettings);
    this.saveSettings = saveSettings;
}

GetDeserializedObject internally calls JsonConvert.DeserializeObject<Spreadsheet>. 

I am confused as to why the Syncfusion library code throws an error because running the following code in my application doesn't produce any error and gives me a populated spreadsheet object: 

var spreadsheet = Newtonsoft.Json.JsonConvert.DeserializeObject<Syncfusion.EJ2.Spreadsheet.Spreadsheet>(saveSettings.JSONData);


For your reference, my saveSettings.JSONData is as follows

{"enableRtl":false,"locale":"en-US","allowEditing":true,"allowOpen":"true","openUrl":"http://localhost:51780/api/SpreadSheet/Open","enablePersistence":false,"cellStyle":{"fontFamily":"Calibri","verticalAlign":"bottom","textIndent":"0pt","backgroundColor":"#ffffff","color":"#000000","textAlign":"left","fontSize":"11pt","fontWeight":"normal","fontStyle":"normal","textDecoration":"none","border":"","borderLeft":"","borderTop":"","borderRight":"","borderBottom":""},"showRibbon":true,"showFormulaBar":true,"showSheetTabs":true,"allowSave":true,"enableContextMenu":true,"allowAutoFill":true,"selectionSettings":{"mode":"Multiple"},"enableKeyboardNavigation":true,"allowNumberFormatting":true,"enableKeyboardShortcut":true,"enableClipboard":true,"allowCellFormatting":true,"allowSorting":true,"allowResizing":true,"allowHyperlink":true,"allowUndoRedo":true,"allowFiltering":true,"allowWrap":true,"allowInsert":true,"allowDelete":true,"allowDataValidation":true,"allowFindAndReplace":true,"allowMerge":true,"allowConditionalFormat":true,"allowImage":true,"allowChart":true,"activeSheetIndex":0,"cssClass":"","height":"100%","width":"100%","allowScrolling":true,"scrollSettings":{"enableVirtualization":true,"isFinite":false},"definedNames":[],"isProtected":false,"password":"","autoFillSettings":{"showFillOptions":true},"saveUrl":"","sheets":[{"id":1,"name":"Sheet1","rowCount":100,"colCount":100,"topLeftCell":"A1","activeCell":"B3","selectedRange":"B3:B3","usedRange":{"rowIndex":0,"colIndex":0},"rows":[{"cells":[{"value":"sdfs"}]}],"columns":[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}],"showHeaders":true,"showGridLines":true,"state":"Visible","protectSettings":{"selectCells":false,"formatCells":false,"formatRows":false,"formatColumns":false,"insertLink":false},"isProtected":false,"frozenRows":0,"frozenColumns":0,"paneTopLeftCell":"A1"}]}

Hope this is enough information for diagnosing the issue.


Thanks,

Ranul



7 Replies 1 reply marked as answer

SP Sangeetha Priya Murugan Syncfusion Team December 10, 2021 05:04 PM UTC

Hi Ranul, 
 
Thank you for contacting Syncfusion support. 
 
We have checked your reported issue and we need to validate more this. So, we will update you the further details on December 14th, 2021. We appreciate your patience until then. 
 
Regards, 
Sangeetha M  



TS Thaneegairaj Sankar Syncfusion Team December 15, 2021 04:49 PM UTC

Hi Ranul,


Thanks for the patience.


We have checked your reported issue and we have created simple spreadsheet angular sample with the latest (19.3.59) Syncfusion.EJ2.Spreadsheet.AspNet.Core NuGet package, and we are unable to reproduce your reported issue in our end. Please find the below sample and service link


Sample: https://stackblitz.com/edit/angular-pfdkkm?file=app.component.ts



Service: https://www.syncfusion.com/downloads/support/directtrac/general/ze/ASP.NET_CORE1802397594


Please check the above details and if you are still facing issue, please share the below details.


·         Please check with latest version for NuGet packages.

·         If possible, please share the dummy excel file for reproducing the issue

·         If possible, please share us the video demonstration of this issue.


Please provide the above requested information, based on that we will check and provide you a better solution quickly.


Regards,

Thaneegairaj S



JB Jordan Brobyn January 19, 2022 04:09 AM UTC

I am experiencing the same problem on the latest spreadsheet version. Have you been able to resolve your issue?



JB Jordan Brobyn January 19, 2022 04:37 AM UTC

It appears my issue was due to SaveSettings.PdfLayoutSettings being null causes an exception in Workbook.Save.


Hope this helps.



TS Thaneegairaj Sankar Syncfusion Team January 20, 2022 11:10 AM UTC

Hi Jordan, 

 
Based on your provided details, we suspect that you customize the save action in your application by sending the JsonData to the server in an external action. So, we have prepared the sample that saves the spreadsheet as excel in an external button click, event by providing the PDFLayoutSettings as like in the below code example. 

 
Code example: 

 
function saveSpreadsheet() { 
            var fileName    = $("#fileName").val(); 
            var contentType = "Xlsx"; 
            var versionType = "Xlsx"; 
            var saveType = "Xlsx"; 
            var pdfSettings = { FitSheetOnOnePage: false }; 
  
            var spreadsheetObj = ej.base.getComponent(document.getElementById('spreadsheet'), 'spreadsheet'); 
            spreadsheetObj.saveAsJson().then((Json) => { 
                var jsonData = JSON.stringify(Json.jsonObject.Workbook); 
                $.ajax({ 
                    type: "POST", 
                    data: { FileName: fileName, JSONData: jsonData, SaveType: saveType, FileContentType: contentType, VersionType: versionType, PDFLayoutSettings: JSON.stringify(pdfSettings)}, 
                    url: "/api/ExcelEditor/Save", 
                    success: function (data) { 
                        if (data == "Success") { 
                            alert("File Has Been Saved Successfully!"); 
                        } 
                        else { 
                            alert("Error: Failed to Save File!"); 
                        } 
                    }, 
                    error: function (jqXHR, exception) { 
                        console.log(jqXHR); 
                        alert("Error: Failed to Save File!"); 
                    } 
                }); 
            }); 
        } 

 
Could you please check the attached sample, if you still getting the same issue? kindly replicate your issue with the attached sample and send back to us. Based on that we will check and provide you a better solution quickly. 

 
Regards, 
Thaneegairaj S 


Marked as answer

RP Ranul Pallemulle February 1, 2022 12:44 PM UTC

Hi,

Can confirm that adding a PDFLayoutSettings property to the SpreadSheetSaveSettings object in the angular code before posting the data to the backend worked for me. Thanks!



TS Thaneegairaj Sankar Syncfusion Team February 3, 2022 06:22 AM UTC

Hi Ranul, 

 
We are happy to hear that your issue has been resolved. Please feel free to contact us if you need any further assistance on this. 

 
Regards, 
Thaneegairaj S 


Loader.
Up arrow icon