Skip to content

Commit c665686

Browse files
1011820: Changed the code snippets to react
1 parent 29c765b commit c665686

1 file changed

Lines changed: 83 additions & 79 deletions

File tree

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

Lines changed: 83 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,21 @@ In user interface, you can save Spreadsheet data as Excel document by clicking `
2727
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.
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" %}
43+
44+
{% previewsample "/document-processing/code-snippet/spreadsheet/react/open-save-cs5" %}
3945

4046
Please find the below table for the [`beforeSave`](https://ej2.syncfusion.com/documentation/api/spreadsheet/index-default#beforesave) event arguments.
4147

@@ -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:
@@ -124,15 +129,15 @@ By default, the Spreadsheet control saves the Excel file and downloads it to the
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
@@ -225,72 +230,71 @@ On the server side, the save service will take the received JSON data, pass it t
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
@@ -310,15 +314,15 @@ In the Spreadsheet component, there is currently no direct option to save data a
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)