Skip to content

Commit a7d2f91

Browse files
committed
895338: Added the reviewed content.
1 parent 8947082 commit a7d2f91

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

ej2-asp-core-mvc/spreadsheet/open-save.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,67 @@ The following code example shows how to open the spreadsheet data as base64 stri
247247
{% endtabs %}
248248
{% endif %}
249249

250+
### To open an Excel file located on a server
251+
252+
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 as `JSON data`. On the client side, you should use the `openFromJson` method to load that `JSON data` into the Spreadsheet component.
253+
254+
**Server endpoint code**:
255+
256+
```csharp
257+
258+
public IActionResult Open([FromBody] FileOptions options)
259+
{
260+
OpenRequest open = new OpenRequest();
261+
string filePath = _env.ContentRootPath.ToString() + "\\Files\\" + options.FileName + ".xlsx";
262+
// Getting the file stream from the file path.
263+
FileStream fileStream = new FileStream(filePath, FileMode.Open);
264+
// Converting MemoryStream to IFormFile.
265+
IFormFile formFile = new FormFile(fileStream, 0, fileStream.Length, "", options.FileName + ".xlsx");
266+
open.File = formFile;
267+
// Processing the Excel file and return the workbook JSON.
268+
var result = Workbook.Open(open);
269+
fileStream.Close();
270+
return Content(result);
271+
}
272+
273+
public class FileOptions
274+
{
275+
public string FileName { get; set; } = string.Empty;
276+
}
277+
```
278+
279+
**Client-side code**:
280+
281+
```js
282+
283+
// Fetch call to server to load the Excel.
284+
fetch('https://localhost:{{Your port number}}/Home/Open', {
285+
method: 'POST',
286+
headers: {
287+
'Content-Type': 'application/json',
288+
},
289+
body: JSON.stringify({ FileName: 'Sample' }),
290+
})
291+
.then((response) => response.json())
292+
.then((data) => {
293+
// Load the JSON data into spreadsheet.
294+
spreadsheet.openFromJson({ file: data });
295+
})
296+
297+
```
298+
299+
You can find the server endpoint code to fetch and process the Excel file in this attachment(https://www.syncfusion.com/downloads/support/directtrac/general/ze/WebApplication1_(1)-880363187).
300+
301+
After launching the server endpoint, you need to update the file fetch URL on the client-side sample as shown below.
302+
303+
```js
304+
305+
// To open an Excel file from the server.
306+
307+
fetch('https://localhost:{port number}/Home/Open')
308+
309+
```
310+
250311
### External workbook confirmation dialog
251312

252313
When you open an excel file that contains external workbook references, you will see a confirmation dialog. This dialog allows you to either continue with the file opening or cancel the operation. This confirmation dialog will appear only if you set the `AllowExternalWorkbook` property value to **false** during the open request, as shown below. This prevents the spreadsheet from displaying inconsistent data.
@@ -492,6 +553,78 @@ The following code example shows how to save the spreadsheet data as base64 stri
492553
{% endtabs %}
493554
{% endif %}
494555

556+
### To save an Excel file to a server location
557+
558+
By default, the spreadsheet 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. On the client side, you must convert the spreadsheet data into `JSON` format using the `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`, convert the stream into an Excel file, and then save it to the server location.
559+
560+
**Client-side code**:
561+
562+
```js
563+
564+
// Convert the spreadsheet workbook to JSON data.
565+
spreadsheet.saveAsJson().then((json) => {
566+
const formData = new FormData();
567+
formData.append('FileName', "Sample");
568+
formData.append('saveType', 'Xlsx');
569+
// Passing the JSON data to perform the save operation.
570+
formData.append('JSONData', JSON.stringify(json.jsonObject.Workbook));
571+
formData.append('PdfLayoutSettings', JSON.stringify({ FitSheetOnOnePage: false }));
572+
// Using fetch to invoke the save process.
573+
fetch('https://localhost:{{Your port number}}/Home/Save', {
574+
method: 'POST',
575+
body: formData
576+
}).then((response) => {
577+
console.log(response);
578+
});
579+
});
580+
581+
```
582+
583+
**Server endpoint code**:
584+
585+
```csharp
586+
587+
public string Save(SaveSettings saveSettings)
588+
{
589+
ExcelEngine excelEngine = new ExcelEngine();
590+
IApplication application = excelEngine.Excel;
591+
try
592+
{
593+
594+
// Save the workbook as Stream.
595+
Stream fileStream = Workbook.Save<Stream>(saveSettings);
596+
// Using XLSIO, we are opening the file stream and saving the file in the server under Files folder.
597+
// You can also save the stream file in your server location.
598+
IWorkbook workbook = application.Workbooks.Open(fileStream);
599+
string basePath = _env.ContentRootPath + "\\Files\\" + saveSettings.FileName + ".xlsx";
600+
var file = System.IO.File.Create(basePath);
601+
fileStream.Seek(0, SeekOrigin.Begin);
602+
// To convert the stream to file options.
603+
fileStream.CopyTo(file);
604+
file.Dispose();
605+
fileStream.Dispose();
606+
return string.Empty;
607+
}
608+
catch (Exception ex)
609+
{
610+
return ex.Message;
611+
}
612+
}
613+
614+
```
615+
616+
You can find the server endpoint code to save the spreadsheet JSON data as an Excel file in the attached documentation(https://www.syncfusion.com/downloads/support/directtrac/general/ze/WebApplication1_(1)-880363187).
617+
618+
After launching the server endpoint, you need to update the file fetch URL on the client-side sample as shown below.
619+
620+
```js
621+
622+
//To save an Excel file to the server.
623+
624+
fetch('https://localhost:{port number}/Home/Save')
625+
626+
```
627+
495628
### Supported file formats
496629

497630
The following list of Excel file formats are supported in Spreadsheet:

0 commit comments

Comments
 (0)