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
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
I am experiencing the same problem on the latest spreadsheet version. Have you been able to resolve your issue?
It appears my issue was due to SaveSettings.PdfLayoutSettings being null causes an exception in Workbook.Save.
Hope this helps.
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!");
}
});
});
} |
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!