From 93b23de02feaf3eb5ccf0dc8dd6aa4112aa6b2d6 Mon Sep 17 00:00:00 2001 From: Build Automaion Date: Tue, 16 Jul 2024 17:10:54 +0530 Subject: [PATCH] Integrated latest changes at 07-16-2024 4:30:25 PM --- ej2-react/spreadsheet/open-save.md | 579 +++++++++++++++++++---------- 1 file changed, 374 insertions(+), 205 deletions(-) diff --git a/ej2-react/spreadsheet/open-save.md b/ej2-react/spreadsheet/open-save.md index 339a6713f..3b37811da 100644 --- a/ej2-react/spreadsheet/open-save.md +++ b/ej2-react/spreadsheet/open-save.md @@ -48,51 +48,6 @@ Please find the below table for the beforeOpen event arguments. > * Use `Ctrl + O` keyboard shortcut to open Excel documents. > * The default value of the [allowOpen](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#allowopen) property is `true`. For demonstration purpose, we have showcased the [allowOpen](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#allowopen) property in previous code snippet. -### Open an external URL excel file while initial load - -You can achieve to access the remote Excel file by using the [`created`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#created) event. In this event you can fetch the Excel file and convert it to a blob. Convert this blob to a file and [`open`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#open) this file by using Spreadsheet component open method. - -{% tabs %} -{% highlight js tabtitle="app.jsx" %} -{% include code-snippet/spreadsheet/open-save-cs2/app/app.jsx %} -{% endhighlight %} -{% highlight ts tabtitle="app.tsx" %} -{% include code-snippet/spreadsheet/open-save-cs2/app/app.tsx %} -{% endhighlight %} -{% endtabs %} - - {% previewsample "page.domainurl/code-snippet/spreadsheet/open-save-cs2" %} - -### To add custom header during open - -You can add your own custom header to the open action in the Spreadsheet. For processing the data, it has to be sent from server to client side and adding customer header can provide privacy to the data with the help of Authorization Token. Through the [`beforeOpen`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#beforeopen) event, the custom header can be added to the request during open action. - -{% tabs %} -{% highlight js tabtitle="app.jsx" %} -{% include code-snippet/spreadsheet/open-save-cs3/app/app.jsx %} -{% endhighlight %} -{% highlight ts tabtitle="app.tsx" %} -{% include code-snippet/spreadsheet/open-save-cs3/app/app.tsx %} -{% endhighlight %} -{% endtabs %} - - {% previewsample "page.domainurl/code-snippet/spreadsheet/open-save-cs3" %} - -### Open excel file into a read-only mode - -You can open Excel file into a read-only mode by using the [`openComplete`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#opencomplete) event. In this event, you must protect all the sheets and lock its used range cells by using [`protectSheet`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#protectsheet) and [`lockCells`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#lockcells) methods. - -{% tabs %} -{% highlight js tabtitle="app.jsx" %} -{% include code-snippet/spreadsheet/open-save-cs4/app/app.jsx %} -{% endhighlight %} -{% highlight ts tabtitle="app.tsx" %} -{% include code-snippet/spreadsheet/open-save-cs4/app/app.tsx %} -{% endhighlight %} -{% endtabs %} - - {% previewsample "page.domainurl/code-snippet/spreadsheet/open-save-cs4" %} - ### Open an excel file using a file uploader If you explore your machine to select and upload an Excel document using the file uploader, you will receive the uploaded document as a raw file in the [success](https://ej2.syncfusion.com/react/documentation/api/uploader/#success) event of the file uploader. In this `success` event, you should pass the received raw file as an argument to the Spreadsheet's [open](https://ej2.syncfusion.com/react/documentation//api/spreadsheet/#open) method to see the appropriate output. @@ -110,61 +65,39 @@ The following code example shows how to import an Excel document using file uplo {% previewsample "page.domainurl/code-snippet/spreadsheet/open-save-cs9" %} -### Configure JSON deserialization options - -Previously, when opening a workbook JSON object into the Spreadsheet using the [openFromJson](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#openfromjson) method, the entire workbook, including all features specified in the JSON object, was processed and loaded into the Spreadsheet. - -Now, you have the option to selectively ignore some features during the opening of the JSON object by configuring deserialization options and passing them as arguments to the `openFromJson` method. This argument is optional, and if not configured, the entire workbook JSON object will be loaded without ignoring any features. - -```ts -spreadsheet.openFromJson({ file: file }, { ignoreStyle: true }); -``` - -| Options | Description | -| ----- | ----- | -| onlyValues | If **true**, only the cell values will be loaded. | -| ignoreStyle | If **true**, styles will be excluded when loading the JSON data. | -| ignoreFormula | If **true**, formulas will be excluded when loading the JSON data. | -| ignoreFormat | If **true**, number formats will be excluded when loading the JSON data. | -| ignoreConditionalFormat | If **true**, conditional formatting will be excluded when loading the JSON data. | -| ignoreValidation | If **true**, data validation rules will be excluded when loading the JSON data. | -| ignoreFreezePane | If **true**, freeze panes will be excluded when loading the JSON data. | -| ignoreWrap | If **true**, text wrapping settings will be excluded when loading the JSON data. | -| ignoreChart | If **true**, charts will be excluded when loading the JSON data. | -| ignoreImage | If **true**, images will be excluded when loading the JSON data. | -| ignoreNote | If **true**, notes will be excluded when loading the JSON data. | +### Open an external URL excel file while initial load -The following code snippet demonstrates how to configure the deserialization options and pass them as arguments to the openFromJson method: +You can achieve to access the remote Excel file by using the [`created`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#created) event. In this event you can fetch the Excel file and convert it to a blob. Convert this blob to a file and [`open`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#open) this file by using Spreadsheet component open method. {% tabs %} {% highlight js tabtitle="app.jsx" %} -{% include code-snippet/spreadsheet/open-from-json/app/app.jsx %} +{% include code-snippet/spreadsheet/open-save-cs2/app/app.jsx %} {% endhighlight %} {% highlight ts tabtitle="app.tsx" %} -{% include code-snippet/spreadsheet/open-from-json/app/app.tsx %} +{% include code-snippet/spreadsheet/open-save-cs2/app/app.tsx %} {% endhighlight %} {% endtabs %} -{% previewsample "page.domainurl/code-snippet/spreadsheet/open-from-json" %} + {% previewsample "page.domainurl/code-snippet/spreadsheet/open-save-cs2" %} -### Open an excel file from Base64 string data +### Open an excel file from blob data -In the Syncfusion Spreadsheet component, there is no direct option to open data as a `Base64` string. To achieve this, the `import()` function fetches the `Base64` string, converts it to a Blob, creates a File object from the Blob, and then opens it using the [open](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#open) method in the spreadsheet. +By default, the Spreadsheet component provides an option to browse files from the local file system and open them within the component. If you want to open an Excel file from blob data, you need to fetch the blob data from the server or another source and convert this blob data into a `File` object. Then, you can use the [open](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#open) method in the Spreadsheet component to load that `File` object. -The following code example shows how to open the spreadsheet data as base64 string. +Please find the code to fetch the blob data and load it into the Spreadsheet component below. {% tabs %} {% highlight js tabtitle="app.jsx" %} -{% include code-snippet/spreadsheet/base-64-string/app/app.jsx %} +{% include code-snippet/spreadsheet/open-from-blobdata-cs1/app/app.jsx %} {% endhighlight %} {% highlight ts tabtitle="app.tsx" %} -{% include code-snippet/spreadsheet/base-64-string/app/app.tsx %} +{% include code-snippet/spreadsheet/open-from-blobdata-cs1/app/app.tsx %} {% endhighlight %} {% endtabs %} -{% previewsample "page.domainurl/code-snippet/spreadsheet/base-64-string" %} +{% previewsample "page.domainurl/code-snippet/spreadsheet/open-from-blobdata-cs1" %} -### To open an Excel file located on a server +### Open an Excel file located on a server By default, the Spreadsheet component provides an option to browse files from the local file system and open them within the component. If you want to load an Excel file located on a server, you need to configure the server endpoint to fetch the Excel file from the server location, process it using `Syncfusion.EJ2.Spreadsheet.AspNet.Core`, and send it back to the client side as `JSON data`. On the client side, you should use the [openFromJson](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#openfromjson) method to load that `JSON data` into the Spreadsheet component. @@ -219,22 +152,174 @@ You can find the server endpoint code to fetch and process the Excel file in thi fetch('https://localhost:{port number}/Home/Open') ``` -### To open an excel file from blob data +### Open an excel file using a hosted web service in AWS Lambda -By default, the Spreadsheet component provides an option to browse files from the local file system and open them within the component. If you want to open an Excel file from blob data, you need to fetch the blob data from the server or another source and convert this blob data into a `File` object. Then, you can use the [open](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#open) method in the Spreadsheet component to load that `File` object. +Before proceeding with the opening process, you should deploy the spreadsheet open/save web API service in AWS Lambda. To host the open/save web service in the AWS Lambda environment, please refer to the following KB documentation. -Please find the code to fetch the blob data and load it into the Spreadsheet component below. +After deployment, you will get the AWS service URL for the open and save actions. Before opening the Excel file with this hosted open URL, you need to prevent the default file opening process to avoid getting a corrupted file on the open service end. The spreadsheet component appends the file to the `formData` and sends it to the open service, which causes the file to get corrupted. To prevent this, set the `args.cancel` value to `true` in the [`beforeOpen`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#beforeopen) event. After that, you will get the selected file in the `beforeOpen` event argument. Then, convert this file into a base64 string and send it to the open service URL using a fetch request. + +On the open service end, convert the base64 string back to a file and pass it as an argument to the workbook `Open` method. The open service will process the file and return the spreadsheet data in JSON format. You will then receive this JSON data in the fetch success callback. Finally, use the [openFromJson](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#openfromjson) method to load this JSON data into the spreadsheet component. + +The following code example shows how to open an Excel file using a hosted web service in AWS Lambda, as mentioned above. + +```js +function Default() { + let spreadsheet; + const beforeOpenHandler = (eventArgs) => { + eventArgs.cancel = true; // To prevent the default open action. + if (eventArgs.file) { + const reader = new FileReader(); + reader.readAsDataURL(eventArgs.file); + reader.onload = () => { + // Removing the xlsx file content-type. + const base64Data = reader.result.replace('data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,', ''); + openExcel({ + file: base64Data, + extension: eventArgs.file.name.slice(eventArgs.file.name.lastIndexOf('.') + 1), + password: eventArgs.password || '' + }); + }; + } + }; + const openExcel = (requestData) => { + // Fetch call to AWS server for open processing. + fetch('https://xxxxxxxxxxxxxxxxxx.amazonaws.com/Prod/api/spreadsheet/open', { + method: 'POST', + headers: { + 'Accept': 'application/json, text/plain', + 'Content-Type': 'application/json;charset=UTF-8' + }, + body: JSON.stringify(requestData) + }).then((response) => { + if (response.ok) { + return response.json(); + } + }).then((data) => { + // Loading the JSON data into our spreadsheet. + if (data.Workbook && data.Workbook.sheets) { + spreadsheet.openFromJson({ file: data }); + } + }).catch((error) => { + console.log(error); + }); + }; + return (
+
+ { spreadsheet = ssObj; }} beforeOpen={beforeOpenHandler}> + +
+
); +} +export default Default; +``` + +```csharp +public IActionResult Open(OpenOptions openOptions) +{ + // Convert the base64 string to bytes array. + byte[] bytes = Convert.FromBase64String(openOptions.File); + // Loading the bytes array to stream. + MemoryStream stream = new MemoryStream(bytes); + OpenRequest open = new OpenRequest(); + // Converting the stream into FormFile. + open.File = new FormFile(stream, 0, bytes.Length, "Sample", "Sample." + openOptions.Extension); + if (string.IsNullOrEmpty(openOptions.Password)) + open.Password = openOptions.Password; + var result = Workbook.Open(open); + return Content(result); +} + +public class OpenOptions +{ + public string File { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; + public string Extension { get; set; } = string.Empty; +} +``` + +### Open an excel file from Base64 string data + +In the Syncfusion Spreadsheet component, there is no direct option to open data as a `Base64` string. To achieve this, the `import()` function fetches the `Base64` string, converts it to a Blob, creates a File object from the Blob, and then opens it using the [open](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#open) method in the spreadsheet. + +The following code example shows how to open the spreadsheet data as base64 string. {% tabs %} {% highlight js tabtitle="app.jsx" %} -{% include code-snippet/spreadsheet/open-from-blobdata-cs1/app/app.jsx %} +{% include code-snippet/spreadsheet/base-64-string/app/app.jsx %} {% endhighlight %} {% highlight ts tabtitle="app.tsx" %} -{% include code-snippet/spreadsheet/open-from-blobdata-cs1/app/app.tsx %} +{% include code-snippet/spreadsheet/base-64-string/app/app.tsx %} {% endhighlight %} {% endtabs %} -{% previewsample "page.domainurl/code-snippet/spreadsheet/open-from-blobdata-cs1" %} +{% previewsample "page.domainurl/code-snippet/spreadsheet/base-64-string" %} + +### Open excel file into a read-only mode + +You can open Excel file into a read-only mode by using the [`openComplete`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#opencomplete) event. In this event, you must protect all the sheets and lock its used range cells by using [`protectSheet`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#protectsheet) and [`lockCells`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#lockcells) methods. + +{% tabs %} +{% highlight js tabtitle="app.jsx" %} +{% include code-snippet/spreadsheet/open-save-cs4/app/app.jsx %} +{% endhighlight %} +{% highlight ts tabtitle="app.tsx" %} +{% include code-snippet/spreadsheet/open-save-cs4/app/app.tsx %} +{% endhighlight %} +{% endtabs %} + +{% previewsample "page.domainurl/code-snippet/spreadsheet/open-save-cs4" %} + +### Configure JSON deserialization options + +Previously, when opening a workbook JSON object into the Spreadsheet using the [openFromJson](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#openfromjson) method, the entire workbook, including all features specified in the JSON object, was processed and loaded into the Spreadsheet. + +Now, you have the option to selectively ignore some features during the opening of the JSON object by configuring deserialization options and passing them as arguments to the `openFromJson` method. This argument is optional, and if not configured, the entire workbook JSON object will be loaded without ignoring any features. + +```ts +spreadsheet.openFromJson({ file: file }, { ignoreStyle: true }); +``` + +| Options | Description | +| ----- | ----- | +| onlyValues | If **true**, only the cell values will be loaded. | +| ignoreStyle | If **true**, styles will be excluded when loading the JSON data. | +| ignoreFormula | If **true**, formulas will be excluded when loading the JSON data. | +| ignoreFormat | If **true**, number formats will be excluded when loading the JSON data. | +| ignoreConditionalFormat | If **true**, conditional formatting will be excluded when loading the JSON data. | +| ignoreValidation | If **true**, data validation rules will be excluded when loading the JSON data. | +| ignoreFreezePane | If **true**, freeze panes will be excluded when loading the JSON data. | +| ignoreWrap | If **true**, text wrapping settings will be excluded when loading the JSON data. | +| ignoreChart | If **true**, charts will be excluded when loading the JSON data. | +| ignoreImage | If **true**, images will be excluded when loading the JSON data. | +| ignoreNote | If **true**, notes will be excluded when loading the JSON data. | + +The following code snippet demonstrates how to configure the deserialization options and pass them as arguments to the openFromJson method: + +{% tabs %} +{% highlight js tabtitle="app.jsx" %} +{% include code-snippet/spreadsheet/open-from-json/app/app.jsx %} +{% endhighlight %} +{% highlight ts tabtitle="app.tsx" %} +{% include code-snippet/spreadsheet/open-from-json/app/app.tsx %} +{% endhighlight %} +{% endtabs %} + +{% previewsample "page.domainurl/code-snippet/spreadsheet/open-from-json" %} + +### Add custom header during open + +You can add your own custom header to the open action in the Spreadsheet. For processing the data, it has to be sent from server to client side and adding customer header can provide privacy to the data with the help of Authorization Token. Through the [`beforeOpen`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#beforeopen) event, the custom header can be added to the request during open action. + +{% tabs %} +{% highlight js tabtitle="app.jsx" %} +{% include code-snippet/spreadsheet/open-save-cs3/app/app.jsx %} +{% endhighlight %} +{% highlight ts tabtitle="app.tsx" %} +{% include code-snippet/spreadsheet/open-save-cs3/app/app.tsx %} +{% endhighlight %} +{% endtabs %} + + {% previewsample "page.domainurl/code-snippet/spreadsheet/open-save-cs3" %} ### External workbook confirmation dialog @@ -307,83 +392,195 @@ Please find the below table for the beforeSave event arguments. > * The default value of [allowSave](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#allowsave) property is `true`. For demonstration purpose, we have showcased the [allowSave](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#allowsave) property in previous code snippet. > * Demo purpose only, we have used the online web service url link. -### To send and receive custom params from client to server +### Save an excel file as blob data -Passing the custom parameters from client to server by using [`beforeSave`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#beforesave) event. +By default, the Spreadsheet component saves the Excel file and downloads it to the local file system. If you want to save an Excel file as blob data, you need to set `needBlobData` property to **true** and `isFullPost` property to **false** in the [beforeSave](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#beforesave) event of the spreadsheet. Subsequently, you will receive the spreadsheet data as a blob in the [saveComplete](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#savecomplete) event. You can then post the blob data to the server endpoint for saving. + +Please find below the code to retrieve blob data from the Spreadsheet component below. {% tabs %} {% highlight js tabtitle="app.jsx" %} -{% include code-snippet/spreadsheet/open-save-cs6/app/app.jsx %} +{% include code-snippet/spreadsheet/save-as-blobdata-cs1/app/app.jsx %} {% endhighlight %} {% highlight ts tabtitle="app.tsx" %} -{% include code-snippet/spreadsheet/open-save-cs6/app/app.tsx %} -{% endhighlight %} -{% highlight js tabtitle="datasource.jsx" %} -{% include code-snippet/spreadsheet/open-save-cs6/app/datasource.jsx %} -{% endhighlight %} -{% highlight ts tabtitle="datasource.tsx" %} -{% include code-snippet/spreadsheet/open-save-cs6/app/datasource.tsx %} +{% include code-snippet/spreadsheet/save-as-blobdata-cs1/app/app.tsx %} {% endhighlight %} {% endtabs %} - {% previewsample "page.domainurl/code-snippet/spreadsheet/open-save-cs6" %} -Server side code snippets: +{% previewsample "page.domainurl/code-snippet/spreadsheet/save-as-blobdata-cs1" %} + +### Save an Excel file to a server + +By default, the Spreadsheet component saves the Excel file and downloads it to the local file system. If you want to save an Excel file to a server location, you need to configure the server endpoint to convert the spreadsheet data into a file stream and save it to the server location. To do this, first, on the client side, you must convert the spreadsheet data into `JSON` format using the [saveAsJson](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#saveasjson) method and send it to the server endpoint. On the server endpoint, you should convert the received spreadsheet `JSON` data into a file stream using `Syncfusion.EJ2.Spreadsheet.AspNet.Core`, then convert the stream into an Excel file, and finally save it to the server location. + +**Client Side**: + +```js + + // Convert the spreadsheet workbook to JSON data. + spreadsheet.saveAsJson().then((json) => { + const formData = new FormData(); + formData.append('FileName', "Sample"); + formData.append('saveType', 'Xlsx'); + // Passing the JSON data to perform the save operation. + formData.append('JSONData', JSON.stringify(json.jsonObject.Workbook)); + formData.append('PdfLayoutSettings', JSON.stringify({ FitSheetOnOnePage: false })); + // Using fetch to invoke the save process. + fetch('https://localhost:{{Your port number}}/Home/Save', { + method: 'POST', + body: formData + }).then((response) => { + console.log(response); + }); + }); + +``` + +**Server Endpoint**: ```csharp - public IActionResult Save(SaveSettings saveSettings, string customParams) + public string Save(SaveSettings saveSettings) + { + ExcelEngine excelEngine = new ExcelEngine(); + IApplication application = excelEngine.Excel; + try { - Console.WriteLine(customParams); // you can get the custom params in controller side - return Workbook.Save(saveSettings); + + // Save the workbook as stream. + Stream fileStream = Workbook.Save(saveSettings); + // Using XLSIO, we are opening the file stream and saving the file in the server under "Files" folder. + // You can also save the stream file in your server location. + IWorkbook workbook = application.Workbooks.Open(fileStream); + string basePath = _env.ContentRootPath + "\\Files\\" + saveSettings.FileName + ".xlsx"; + var file = System.IO.File.Create(basePath); + fileStream.Seek(0, SeekOrigin.Begin); + // To convert the stream to file options. + fileStream.CopyTo(file); + file.Dispose(); + fileStream.Dispose(); + return string.Empty; + } + catch (Exception ex) + { + return ex.Message; } + } + ``` -### To add custom header during save +You can find the server endpoint code to save the spreadsheet data as an Excel file in this [attachment](https://www.syncfusion.com/downloads/support/directtrac/general/ze/WebApplication1_(1)-880363187). After launching the server endpoint, you need to update the URL on the client side sample as shown below. -You can add your own custom header to the save action in the Spreadsheet. For processing the data, it has to be sent from client to server side and adding customer header can provide privacy to the data with the help of Authorization Token. Through the [`fileMenuItemSelect`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#filemenuitemselect) event, the custom header can be added to the request during save action. +```js -{% tabs %} -{% highlight js tabtitle="app.jsx" %} -{% include code-snippet/spreadsheet/open-save-cs7/app/app.jsx %} -{% endhighlight %} -{% highlight ts tabtitle="app.tsx" %} -{% include code-snippet/spreadsheet/open-save-cs7/app/app.tsx %} -{% endhighlight %} -{% highlight js tabtitle="datasource.jsx" %} -{% include code-snippet/spreadsheet/open-save-cs7/app/datasource.jsx %} -{% endhighlight %} -{% highlight ts tabtitle="datasource.tsx" %} -{% include code-snippet/spreadsheet/open-save-cs7/app/datasource.tsx %} -{% endhighlight %} -{% endtabs %} +//To save an Excel file to the server. +fetch('https://localhost:{port number}/Home/Save') - {% previewsample "page.domainurl/code-snippet/spreadsheet/open-save-cs7" %} +``` -### To change the PDF orientation +### Save an excel file using a hosted web service in AWS Lambda -By default, the PDF document is created in **Portrait** orientation. You can change the orientation of the PDF document by using the `args.pdfLayoutSettings.orientation` argument settings in the [`beforeSave`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#beforesave) event. +Before proceeding with the save process, you should deploy the spreadsheet open/save web API service in AWS Lambda. To host the open/save web service in the AWS Lambda environment, please refer to the following KB documentation. -The possible values are: +After deployment, you will get the AWS service URL for the open and save actions. Before saving the Excel file with this hosted save URL, you need to prevent the default save action to avoid getting a corrupted excel file on the client end. The save service returns the file stream as a result to the client, which can cause the file to become corrupted. To prevent this, set the `args.cancel` value to `true` in the [`beforeSave`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#beforesave) event. After that, convert the spreadsheet data into JSON format using the [saveAsJson](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#saveasjson) method in the `beforeSave` event and send it to the save service endpoint URL using a fetch request. -* **Portrait** - Used to display content in a vertical layout. -* **Landscape** - Used to display content in a horizontal layout. +On the server side, the save service will take the received JSON data, pass it to the workbook `Save` method, and return the result as a base64 string. The fetch success callback will receive the Excel file in base64 string format on the client side. Finally, you can then convert the base64 string back to a file on the client end to obtain a non-corrupted Excel file. + +The following code example shows how to save an Excel file using a hosted web service in AWS Lambda, as mentioned above. + +```js +function Default() { + let spreadsheet; + let saveInitiated; + const beforeSaveHandler = (eventArgs) => { + if (!saveInitiated) { + eventArgs.cancel = true; // Preventing default save action. + saveInitiated = true; // The "beforeSave" event will trigger for "saveAsJson" action also, so we are preventing for the "saveAsJson". + saveAsExcel(eventArgs); + } + }; + const saveAsExcel = (eventArgs) => { + // Convert the spreadsheet workbook to JSON data. + spreadsheet.saveAsJson().then(Json => { + saveInitiated = false; + const formData = new FormData(); + // Passing the JSON data to server to perform save operation. + formData.append('JSONData', JSON.stringify(Json.jsonObject.Workbook)); + formData.append('saveType', 'Xlsx'); + formData.append('fileName', 'Worksheet'); + formData.append('pdfLayoutSettings', '{"fitSheetOnOnePage":false,"orientation":"Portrait"}'); + // Using fetch API to invoke the server for save processing. + fetch('https://xxxxxxxxxxxxxxxxxxxxxxxxx.amazonaws.com/Prod/api/spreadsheet/save', { + method: 'POST', body: formData + }).then(response => { + if (response.ok) { + return response.blob(); + } + }).then(data => { + const reader = new FileReader(); + reader.onload = function () { + //Converts the result of the file reading operation into a base64 string. + const textBase64Str = reader.result.toString(); + //Converts the base64 string into a Excel base64 string. + const excelBase64Str = atob(textBase64Str.replace('data:text/plain;base64,', '')); + //Converts the Excel base64 string into byte characters. + const byteCharacters = atob(excelBase64Str.replace('data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,', '')); + const byteArrays = []; + for (let i = 0; i < byteCharacters.length; i++) { + byteArrays.push(byteCharacters.charCodeAt(i)); + } + const byteArray = new Uint8Array(byteArrays); + //creates a blob data from the byte array with xlsx content type. + const blobData = new Blob([byteArray], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); + const blobUrl = URL.createObjectURL(blobData); + const anchor = document.createElement('a'); + anchor.download = 'Sample.xlsx'; + anchor.href = blobUrl; + document.body.appendChild(anchor); + anchor.click(); + URL.revokeObjectURL(blobUrl); + document.body.removeChild(anchor); + } + reader.readAsDataURL(data); + }); + }); + }; + return (
+
+ { spreadsheet = ssObj; }} beforeSave={beforeSaveHandler}> + +
+
); +} +export default Default; +``` + +```csharp +public string Save([FromForm]SaveSettings saveSettings) +{ + // This will return the Excel in base64 string format. + return Workbook.Save(saveSettings); +} +``` + +### Save data as a Base64 string + +In the Spreadsheet component, there is currently no direct option to save data as a `Base64` string. You can achieve this by saving the Spreadsheet data as blob data and then converting that saved blob data to a `Base64` string using `FileReader`. + +> You can get the Spreadsheet data as blob in the [saveComplete](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#savecomplete) event when you set the `needBlobData` as **true** and `isFullPost` as **false** in the [beforeSave](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#beforesave) event. + +The following code example shows how to save the spreadsheet data as base64 string. {% tabs %} {% highlight js tabtitle="app.jsx" %} -{% include code-snippet/spreadsheet/open-save-cs8/app/app.jsx %} +{% include code-snippet/spreadsheet/base-64-string/app/app.jsx %} {% endhighlight %} {% highlight ts tabtitle="app.tsx" %} -{% include code-snippet/spreadsheet/open-save-cs8/app/app.tsx %} -{% endhighlight %} -{% highlight js tabtitle="datasource.jsx" %} -{% include code-snippet/spreadsheet/open-save-cs8/app/datasource.jsx %} -{% endhighlight %} -{% highlight ts tabtitle="datasource.tsx" %} -{% include code-snippet/spreadsheet/open-save-cs8/app/datasource.tsx %} +{% include code-snippet/spreadsheet/base-64-string/app/app.tsx %} {% endhighlight %} {% endtabs %} - {% previewsample "page.domainurl/code-snippet/spreadsheet/open-save-cs8" %} +{% previewsample "page.domainurl/code-snippet/spreadsheet/base-64-string" %} ### Configure JSON serialization options @@ -422,110 +619,82 @@ The following code snippet demonstrates how to configure the serialization optio {% previewsample "page.domainurl/code-snippet/spreadsheet/save-as-json" %} -### To save data as a Base64 string - -In the Spreadsheet component, there is currently no direct option to save data as a `Base64` string. You can achieve this by saving the Spreadsheet data as blob data and then converting that saved blob data to a `Base64` string using `FileReader`. - -> You can get the Spreadsheet data as blob in the [saveComplete](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#savecomplete) event when you set the `needBlobData` as **true** and `isFullPost` as **false** in the [beforeSave](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#beforesave) event. +### Send and receive custom params from client to server -The following code example shows how to save the spreadsheet data as base64 string. +Passing the custom parameters from client to server by using [`beforeSave`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#beforesave) event. {% tabs %} {% highlight js tabtitle="app.jsx" %} -{% include code-snippet/spreadsheet/base-64-string/app/app.jsx %} +{% include code-snippet/spreadsheet/open-save-cs6/app/app.jsx %} {% endhighlight %} {% highlight ts tabtitle="app.tsx" %} -{% include code-snippet/spreadsheet/base-64-string/app/app.tsx %} +{% include code-snippet/spreadsheet/open-save-cs6/app/app.tsx %} +{% endhighlight %} +{% highlight js tabtitle="datasource.jsx" %} +{% include code-snippet/spreadsheet/open-save-cs6/app/datasource.jsx %} +{% endhighlight %} +{% highlight ts tabtitle="datasource.tsx" %} +{% include code-snippet/spreadsheet/open-save-cs6/app/datasource.tsx %} {% endhighlight %} {% endtabs %} -{% previewsample "page.domainurl/code-snippet/spreadsheet/base-64-string" %} - -### To save an Excel file to a server - -By default, the Spreadsheet component saves the Excel file and downloads it to the local file system. If you want to save an Excel file to a server location, you need to configure the server endpoint to convert the spreadsheet data into a file stream and save it to the server location. To do this, first, on the client side, you must convert the spreadsheet data into `JSON` format using the [saveAsJson](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#saveasjson) method and send it to the server endpoint. On the server endpoint, you should convert the received spreadsheet `JSON` data into a file stream using `Syncfusion.EJ2.Spreadsheet.AspNet.Core`, then convert the stream into an Excel file, and finally save it to the server location. - -**Client Side**: - -```js - - // Convert the spreadsheet workbook to JSON data. - spreadsheet.saveAsJson().then((json) => { - const formData = new FormData(); - formData.append('FileName', "Sample"); - formData.append('saveType', 'Xlsx'); - // Passing the JSON data to perform the save operation. - formData.append('JSONData', JSON.stringify(json.jsonObject.Workbook)); - formData.append('PdfLayoutSettings', JSON.stringify({ FitSheetOnOnePage: false })); - // Using fetch to invoke the save process. - fetch('https://localhost:{{Your port number}}/Home/Save', { - method: 'POST', - body: formData - }).then((response) => { - console.log(response); - }); - }); - -``` - -**Server Endpoint**: + {% previewsample "page.domainurl/code-snippet/spreadsheet/open-save-cs6" %} +Server side code snippets: ```csharp - public string Save(SaveSettings saveSettings) - { - ExcelEngine excelEngine = new ExcelEngine(); - IApplication application = excelEngine.Excel; - try - { - - // Save the workbook as stream. - Stream fileStream = Workbook.Save(saveSettings); - // Using XLSIO, we are opening the file stream and saving the file in the server under "Files" folder. - // You can also save the stream file in your server location. - IWorkbook workbook = application.Workbooks.Open(fileStream); - string basePath = _env.ContentRootPath + "\\Files\\" + saveSettings.FileName + ".xlsx"; - var file = System.IO.File.Create(basePath); - fileStream.Seek(0, SeekOrigin.Begin); - // To convert the stream to file options. - fileStream.CopyTo(file); - file.Dispose(); - fileStream.Dispose(); - return string.Empty; - } - catch (Exception ex) + public IActionResult Save(SaveSettings saveSettings, string customParams) { - return ex.Message; + Console.WriteLine(customParams); // you can get the custom params in controller side + return Workbook.Save(saveSettings); } - } - ``` +### Add custom header during save -You can find the server endpoint code to save the spreadsheet data as an Excel file in this [attachment](https://www.syncfusion.com/downloads/support/directtrac/general/ze/WebApplication1_(1)-880363187). After launching the server endpoint, you need to update the URL on the client side sample as shown below. +You can add your own custom header to the save action in the Spreadsheet. For processing the data, it has to be sent from client to server side and adding customer header can provide privacy to the data with the help of Authorization Token. Through the [`fileMenuItemSelect`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#filemenuitemselect) event, the custom header can be added to the request during save action. -```js +{% tabs %} +{% highlight js tabtitle="app.jsx" %} +{% include code-snippet/spreadsheet/open-save-cs7/app/app.jsx %} +{% endhighlight %} +{% highlight ts tabtitle="app.tsx" %} +{% include code-snippet/spreadsheet/open-save-cs7/app/app.tsx %} +{% endhighlight %} +{% highlight js tabtitle="datasource.jsx" %} +{% include code-snippet/spreadsheet/open-save-cs7/app/datasource.jsx %} +{% endhighlight %} +{% highlight ts tabtitle="datasource.tsx" %} +{% include code-snippet/spreadsheet/open-save-cs7/app/datasource.tsx %} +{% endhighlight %} +{% endtabs %} -//To save an Excel file to the server. -fetch('https://localhost:{port number}/Home/Save') + {% previewsample "page.domainurl/code-snippet/spreadsheet/open-save-cs7" %} -``` +### Change the PDF orientation -### To save an excel file as blob data +By default, the PDF document is created in **Portrait** orientation. You can change the orientation of the PDF document by using the `args.pdfLayoutSettings.orientation` argument settings in the [`beforeSave`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#beforesave) event. -By default, the Spreadsheet component saves the Excel file and downloads it to the local file system. If you want to save an Excel file as blob data, you need to set `needBlobData` property to **true** and `isFullPost` property to **false** in the [beforeSave](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#beforesave) event of the spreadsheet. Subsequently, you will receive the spreadsheet data as a blob in the [saveComplete](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/#savecomplete) event. You can then post the blob data to the server endpoint for saving. +The possible values are: -Please find below the code to retrieve blob data from the Spreadsheet component below. +* **Portrait** - Used to display content in a vertical layout. +* **Landscape** - Used to display content in a horizontal layout. {% tabs %} {% highlight js tabtitle="app.jsx" %} -{% include code-snippet/spreadsheet/save-as-blobdata-cs1/app/app.jsx %} +{% include code-snippet/spreadsheet/open-save-cs8/app/app.jsx %} {% endhighlight %} {% highlight ts tabtitle="app.tsx" %} -{% include code-snippet/spreadsheet/save-as-blobdata-cs1/app/app.tsx %} +{% include code-snippet/spreadsheet/open-save-cs8/app/app.tsx %} +{% endhighlight %} +{% highlight js tabtitle="datasource.jsx" %} +{% include code-snippet/spreadsheet/open-save-cs8/app/datasource.jsx %} +{% endhighlight %} +{% highlight ts tabtitle="datasource.tsx" %} +{% include code-snippet/spreadsheet/open-save-cs8/app/datasource.tsx %} {% endhighlight %} {% endtabs %} -{% previewsample "page.domainurl/code-snippet/spreadsheet/save-as-blobdata-cs1" %} + {% previewsample "page.domainurl/code-snippet/spreadsheet/open-save-cs8" %} ### Supported file formats