Skip to content

Commit 386cf0e

Browse files
Merge pull request #2432 from syncfusion-content/EJ2-1011820-master
documentation(EJ2-1011820): Changed the code snippets to React
2 parents 29c765b + d9a9ac2 commit 386cf0e

2 files changed

Lines changed: 93 additions & 89 deletions

File tree

Document-Processing/Excel/Spreadsheet/React/open-excel-files.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ The following sample shows the `Open` option by using the [`openUrl`](https://ej
3939

4040
Please find the below table for the [beforeOpen](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/index-default#beforeopen) event arguments.
4141

42-
## BeforeOpenEventArgs – Properties
42+
**BeforeOpenEventArgs – Properties**
4343

4444
| **Property** | **Type** | **Description** |
4545
|-------------------|-------------------------------|-------------|

Document-Processing/Excel/Spreadsheet/React/save-excel-files.md

Lines changed: 92 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ layout: post
33
title: Save Excel Files in React Spreadsheet component | Syncfusion
44
description: Learn here all about Saving Excel files in Syncfusion React Spreadsheet component of Syncfusion Essential JS 2 and more.
55
platform: document-processing
6-
control: Open
6+
control: Save
77
documentation: ug
88
---
99

@@ -24,20 +24,26 @@ For a quick walkthrough on how the save functionality works, refer to the follow
2424

2525
In user interface, you can save Spreadsheet data as Excel document by clicking `File > Save As` menu item in ribbon.
2626

27-
The following sample shows the `Save` option by using the [`saveUrl`](https://ej2.syncfusion.com/documentation/api/spreadsheet/index-default#saveurl) property in the Spreadsheet control. You can also use the [`beforeSave`](https://ej2.syncfusion.com/documentation/api/spreadsheet/index-default#beforesave) event to customize or cancel the save action which gets triggered before saving the Spreadsheet as an Excel file.
27+
The following sample shows the `Save` option by using the [`saveUrl`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/index-default#saveurl) property in the Spreadsheet control. You can also use the [`beforeSave`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/index-default#beforesave) event to customize or cancel the save action which gets triggered before saving the Spreadsheet as an Excel file.
2828

2929
{% tabs %}
30-
{% highlight ts tabtitle="index.ts" %}
31-
{% include code-snippet/spreadsheet/javascript-es6/open-save-cs5/index.ts %}
30+
{% highlight js tabtitle="app.jsx" %}
31+
{% include code-snippet/spreadsheet/react/open-save-cs5/app/app.jsx %}
32+
{% endhighlight %}
33+
{% highlight ts tabtitle="app.tsx" %}
34+
{% include code-snippet/spreadsheet/react/open-save-cs5/app/app.tsx %}
3235
{% endhighlight %}
33-
{% highlight html tabtitle="index.html" %}
34-
{% include code-snippet/spreadsheet/javascript-es6/open-save-cs5/index.html %}
36+
{% highlight js tabtitle="datasource.jsx" %}
37+
{% include code-snippet/spreadsheet/react/open-save-cs5/app/datasource.jsx %}
38+
{% endhighlight %}
39+
{% highlight ts tabtitle="datasource.tsx" %}
40+
{% include code-snippet/spreadsheet/react/open-save-cs5/app/datasource.tsx %}
3541
{% endhighlight %}
3642
{% endtabs %}
37-
38-
{% previewsample "/document-processing/code-snippet/spreadsheet/javascript-es6/open-save-cs5" %}
3943

40-
Please find the below table for the [`beforeSave`](https://ej2.syncfusion.com/documentation/api/spreadsheet/index-default#beforesave) event arguments.
44+
{% previewsample "/document-processing/code-snippet/spreadsheet/react/open-save-cs5" %}
45+
46+
Please find the below table for the [`beforeSave`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/index-default#beforesave) event arguments.
4147

4248
| **Parameter** | **Type** | **Description** |
4349
| ----- | ----- | ----- |
@@ -51,14 +57,14 @@ Please find the below table for the [`beforeSave`](https://ej2.syncfusion.com/do
5157

5258
> * Use `Ctrl + S` keyboard shortcut to save the Spreadsheet data as Excel file.
5359
54-
> * The default value of [allowSave](https://ej2.syncfusion.com/documentation/api/spreadsheet/index-default#allowsave) property is `true`. For demonstration purpose, we have showcased the [allowSave](https://ej2.syncfusion.com/documentation/api/spreadsheet/index-default#allowsave) property in previous code snippet.
60+
> * The default value of [allowSave](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/index-default#allowsave) property is `true`. For demonstration purpose, we have showcased the [allowSave](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/index-default#allowsave) property in previous code snippet.
5561
> * Demo purpose only, we have used the online web service url link.
5662
5763
## Save Excel files programmatically
5864

5965
To save Excel files programmatically in the Spreadsheet, you can use the [`save`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/index-default#save) method of the Spreadsheet component. Before invoking this method, ensure that the [`saveUrl`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/index-default#saveurl) property is properly configured, as it is required for processing and generating the file on the server.
6066

61-
Please find the below table for the [`save`](https://ej2.syncfusion.com/documentation/api/spreadsheet/index-default#save) method arguments.
67+
Please find the below table for the [`save`](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/index-default#save) method arguments.
6268

6369
| **Parameter** | **Type** | **Description** |
6470
|-----------------------|------------------------|------------------------------------------------------------------|
@@ -105,7 +111,6 @@ const root = createRoot(document.getElementById('spreadsheet'));
105111
root.render(<App />);
106112
```
107113
108-
109114
## Supported Excel file formats for Save
110115
111116
The following file formats are supported when saving the Spreadsheet component:
@@ -119,20 +124,20 @@ The following file formats are supported when saving the Spreadsheet component:
119124
120125
### Save Excel files as Blob
121126
122-
By default, the Spreadsheet control 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/documentation/api/spreadsheet/index-default#beforesave) event of the spreadsheet. Subsequently, you will receive the spreadsheet data as a blob in the [saveComplete](https://ej2.syncfusion.com/documentation/api/spreadsheet/index-default#savecomplete) event. You can then post the blob data to the server endpoint for saving.
127+
By default, the Spreadsheet control 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/index-default#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/index-default#savecomplete) event. You can then post the blob data to the server endpoint for saving.
123128
124129
Please find below the code to retrieve blob data from the Spreadsheet control below.
125130
126131
{% tabs %}
127-
{% highlight ts tabtitle="index.ts" %}
128-
{% include code-snippet/spreadsheet/javascript-es6/save-as-blobdata-cs1/index.ts %}
132+
{% highlight js tabtitle="app.jsx" %}
133+
{% include code-snippet/spreadsheet/react/save-as-blobdata-cs1/app/app.jsx %}
129134
{% endhighlight %}
130-
{% highlight html tabtitle="index.html" %}
131-
{% include code-snippet/spreadsheet/javascript-es6/save-as-blobdata-cs1/index.html %}
135+
{% highlight ts tabtitle="app.tsx" %}
136+
{% include code-snippet/spreadsheet/react/save-as-blobdata-cs1/app/app.tsx %}
132137
{% endhighlight %}
133138
{% endtabs %}
134-
135-
{% previewsample "/document-processing/code-snippet/spreadsheet/javascript-es6/save-as-blobdata-cs1" %}
139+
140+
{% previewsample "/document-processing/code-snippet/spreadsheet/react/save-as-blobdata-cs1" %}
136141
137142
### Save Workbook as JSON
138143
@@ -155,7 +160,7 @@ The following example demonstrates how to save a workbook as JSON from the Sprea
155160
156161
### Save Excel files to a server
157162
158-
By default, the Spreadsheet control 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/documentation/api/spreadsheet/index-default#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.
163+
By default, the Spreadsheet control 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/index-default#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.
159164
160165
**Client Side**:
161166
@@ -219,78 +224,77 @@ Before proceeding with the save process, you should deploy the spreadsheet open/
219224
220225
[How to deploy a spreadsheet open and save web API service to AWS Lambda](https://support.syncfusion.com/kb/article/17184/how-to-deploy-a-spreadsheet-open-and-save-web-api-service-to-aws-lambda)
221226
222-
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/documentation/api/spreadsheet/index-default#beforesave) event. After that, convert the spreadsheet data into JSON format using the [saveAsJson](https://ej2.syncfusion.com/documentation/api/spreadsheet/index-default#saveasjson) method in the `beforeSave` event and send it to the save service endpoint URL using a fetch request.
227+
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/index-default#beforesave) event. After that, convert the spreadsheet data into JSON format using the [saveAsJson](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/index-default#saveasjson) method in the `beforeSave` event and send it to the save service endpoint URL using a fetch request.
223228
224229
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.
225230
226231
The following code example shows how to save an Excel file using a hosted web service in AWS Lambda, as mentioned above.
227232
228-
```ts
229-
import { Spreadsheet } from '@syncfusion/ej2-spreadsheet';
230-
231-
let saveInitiated: boolean;
232-
//Initialize Spreadsheet component
233-
let spreadsheet: Spreadsheet = new Spreadsheet({
234-
sheets: [
235-
],
236-
saveUrl:'https://xxxxxxxxxxxxxxxxxxxxxxxxx.amazonaws.com/Prod/api/spreadsheet/save',
237-
beforeSave: (eventArgs) => {
233+
```js
234+
function Default() {
235+
let spreadsheet;
236+
let saveInitiated;
237+
const beforeSaveHandler = (eventArgs) => {
238238
if (!saveInitiated) {
239239
eventArgs.cancel = true; // Preventing default save action.
240240
saveInitiated = true; // The "beforeSave" event will trigger for "saveAsJson" action also, so we are preventing for the "saveAsJson".
241241
saveAsExcel(eventArgs);
242242
}
243-
}
244-
});
245-
const saveAsExcel = (eventArgs) => {
246-
// Convert the spreadsheet workbook to JSON data.
247-
spreadsheet.saveAsJson().then(Json => {
248-
saveInitiated = false;
249-
const formData = new FormData();
250-
// Passing the JSON data to server to perform save operation.
251-
formData.append('JSONData', JSON.stringify(Json.jsonObject.Workbook));
252-
formData.append('saveType', 'Xlsx');
253-
formData.append('fileName', 'Worksheet');
254-
formData.append('pdfLayoutSettings', '{"fitSheetOnOnePage":false,"orientation":"Portrait"}');
255-
// Using fetch API to invoke the server for save processing.
256-
fetch('https://xxxxxxxxxxxxxxxxxxxxxxxxx.amazonaws.com/Prod/api/spreadsheet/save', {
257-
method: 'POST', body: formData
258-
}).then(response => {
259-
if (response.ok) {
260-
return response.blob();
261-
}
262-
}).then(data => {
263-
const reader = new FileReader();
264-
reader.onload = function () {
265-
//Converts the result of the file reading operation into a base64 string.
266-
const textBase64Str = reader.result.toString();
267-
//Converts the base64 string into a Excel base64 string.
268-
const excelBase64Str = atob(textBase64Str.replace('data:text/plain;base64,', ''));
269-
//Converts the Excel base64 string into byte characters.
270-
const byteCharacters = atob(excelBase64Str.replace('data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,', ''));
271-
const byteArrays = [];
272-
for (let i = 0; i < byteCharacters.length; i++) {
273-
byteArrays.push(byteCharacters.charCodeAt(i));
243+
};
244+
const saveAsExcel = (eventArgs) => {
245+
// Convert the spreadsheet workbook to JSON data.
246+
spreadsheet.saveAsJson().then(Json => {
247+
saveInitiated = false;
248+
const formData = new FormData();
249+
// Passing the JSON data to server to perform save operation.
250+
formData.append('JSONData', JSON.stringify(Json.jsonObject.Workbook));
251+
formData.append('saveType', 'Xlsx');
252+
formData.append('fileName', 'Worksheet');
253+
formData.append('pdfLayoutSettings', '{"fitSheetOnOnePage":false,"orientation":"Portrait"}');
254+
// Using fetch API to invoke the server for save processing.
255+
fetch('https://xxxxxxxxxxxxxxxxxxxxxxxxx.amazonaws.com/Prod/api/spreadsheet/save', {
256+
method: 'POST', body: formData
257+
}).then(response => {
258+
if (response.ok) {
259+
return response.blob();
274260
}
275-
const byteArray = new Uint8Array(byteArrays);
276-
//creates a blob data from the byte array with xlsx content type.
277-
const blobData = new Blob([byteArray], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
278-
const blobUrl = URL.createObjectURL(blobData);
279-
const anchor = document.createElement('a');
280-
anchor.download = 'Sample.xlsx';
281-
anchor.href = blobUrl;
282-
document.body.appendChild(anchor);
283-
anchor.click();
284-
URL.revokeObjectURL(blobUrl);
285-
document.body.removeChild(anchor);
286-
}
287-
reader.readAsDataURL(data);
288-
});
289-
});
290-
};
291-
292-
//Render initialized Spreadsheet component
293-
spreadsheet.appendTo('#spreadsheet');
261+
}).then(data => {
262+
const reader = new FileReader();
263+
reader.onload = function () {
264+
//Converts the result of the file reading operation into a base64 string.
265+
const textBase64Str = reader.result.toString();
266+
//Converts the base64 string into a Excel base64 string.
267+
const excelBase64Str = atob(textBase64Str.replace('data:text/plain;base64,', ''));
268+
//Converts the Excel base64 string into byte characters.
269+
const byteCharacters = atob(excelBase64Str.replace('data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,', ''));
270+
const byteArrays = [];
271+
for (let i = 0; i < byteCharacters.length; i++) {
272+
byteArrays.push(byteCharacters.charCodeAt(i));
273+
}
274+
const byteArray = new Uint8Array(byteArrays);
275+
//creates a blob data from the byte array with xlsx content type.
276+
const blobData = new Blob([byteArray], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
277+
const blobUrl = URL.createObjectURL(blobData);
278+
const anchor = document.createElement('a');
279+
anchor.download = 'Sample.xlsx';
280+
anchor.href = blobUrl;
281+
document.body.appendChild(anchor);
282+
anchor.click();
283+
URL.revokeObjectURL(blobUrl);
284+
document.body.removeChild(anchor);
285+
}
286+
reader.readAsDataURL(data);
287+
});
288+
});
289+
};
290+
return (<div className='control-pane'>
291+
<div className='control-section spreadsheet-control'>
292+
<SpreadsheetComponent saveUrl='https://xxxxxxxxxxxxxxxxxxxxxxxxx.amazonaws.com/Prod/api/spreadsheet/save' ref={(ssObj) => { spreadsheet = ssObj; }} beforeSave={beforeSaveHandler}>
293+
</SpreadsheetComponent>
294+
</div>
295+
</div>);
296+
}
297+
export default Default;
294298
```
295299
296300
```csharp
@@ -305,20 +309,20 @@ public string Save([FromForm]SaveSettings saveSettings)
305309
306310
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`.
307311
308-
> You can get the Spreadsheet data as blob in the [saveComplete](https://ej2.syncfusion.com/documentation/api/spreadsheet/index-default#savecomplete) event when you set the `needBlobData` as **true** and `isFullPost` as **false** in the [beforeSave](https://ej2.syncfusion.com/documentation/api/spreadsheet/index-default#beforesave) event.
312+
> You can get the Spreadsheet data as blob in the [saveComplete](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/index-default#savecomplete) event when you set the `needBlobData` as **true** and `isFullPost` as **false** in the [beforeSave](https://ej2.syncfusion.com/react/documentation/api/spreadsheet/index-default#beforesave) event.
309313
310314
The following code example shows how to save the spreadsheet data as base64 string.
311315
312316
{% tabs %}
313-
{% highlight ts tabtitle="index.ts" %}
314-
{% include code-snippet/spreadsheet/javascript-es6/base-64-string/index.ts %}
317+
{% highlight js tabtitle="app.jsx" %}
318+
{% include code-snippet/spreadsheet/react/base-64-string/app/app.jsx %}
315319
{% endhighlight %}
316-
{% highlight html tabtitle="index.html" %}
317-
{% include code-snippet/spreadsheet/javascript-es6/base-64-string/index.html %}
320+
{% highlight ts tabtitle="app.tsx" %}
321+
{% include code-snippet/spreadsheet/react/base-64-string/app/app.tsx %}
318322
{% endhighlight %}
319323
{% endtabs %}
320-
321-
{% previewsample "/document-processing/code-snippet/spreadsheet/javascript-es6/base-64-string" %}
324+
325+
{% previewsample "/document-processing/code-snippet/spreadsheet/react/base-64-string" %}
322326
323327
## Advanced Save options
324328

0 commit comments

Comments
 (0)