Skip to content

Commit e635b90

Browse files
Merge pull request #3174 from syncfusion-content/EJ2-895338-hotfix
documentation(EJ2-895338) Need to split the Save and Load Excel files to/from to the Server section in Spreadsheet UG
2 parents 7c146b3 + 7e4bb3e commit e635b90

File tree

1 file changed

+104
-1
lines changed

1 file changed

+104
-1
lines changed

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

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,52 @@ Please find the code to fetch the blob data and load it into the Spreadsheet con
276276
{% endtabs %}
277277
{% endif %}
278278

279+
### To open an Excel file located on a server
280+
281+
By default, the Spreadsheet control provides an option to browse files from the local file system and open them within the control. 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` method to load that `JSON data` into the Spreadsheet control.
282+
283+
```csharp
284+
public IActionResult Open([FromBody] FileOptions options)
285+
{
286+
OpenRequest open = new OpenRequest();
287+
string filePath = _env.ContentRootPath.ToString() + "\\Files\\" + options.FileName + ".xlsx";
288+
// Getting the file stream from the file path.
289+
FileStream fileStream = new FileStream(filePath, FileMode.Open);
290+
// Converting "MemoryStream" to "IFormFile".
291+
IFormFile formFile = new FormFile(fileStream, 0, fileStream.Length, "", options.FileName + ".xlsx");
292+
open.File = formFile;
293+
// Processing the Excel file and return the workbook JSON.
294+
var result = Workbook.Open(open);
295+
fileStream.Close();
296+
return Content(result);
297+
}
298+
299+
public class FileOptions
300+
{
301+
public string FileName { get; set; } = string.Empty;
302+
}
303+
```
304+
305+
```js
306+
307+
// Fetch call to server to load the Excel file.
308+
fetch('Home/Open', {
309+
method: 'POST',
310+
headers: {
311+
'Content-Type': 'application/json',
312+
},
313+
body: JSON.stringify({ FileName: 'Sample' }),
314+
})
315+
.then((response) => response.json())
316+
.then((data) => {
317+
// Load the JSON data into spreadsheet.
318+
spreadsheet.openFromJson({ file: data });
319+
})
320+
321+
```
322+
323+
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).
324+
279325
### External workbook confirmation dialog
280326

281327
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.
@@ -550,6 +596,64 @@ Please find below the code to retrieve blob data from the Spreadsheet control be
550596
{% endtabs %}
551597
{% endif %}
552598

599+
### To save an Excel file to a server location
600+
601+
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` 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.
602+
603+
```js
604+
605+
// Convert the spreadsheet workbook to JSON data.
606+
spreadsheet.saveAsJson().then((json) => {
607+
const formData = new FormData();
608+
formData.append('FileName', "Sample");
609+
formData.append('saveType', 'Xlsx');
610+
// Passing the JSON data to perform the save operation.
611+
formData.append('JSONData', JSON.stringify(json.jsonObject.Workbook));
612+
formData.append('PdfLayoutSettings', JSON.stringify({ FitSheetOnOnePage: false }));
613+
// Using fetch to invoke the save process.
614+
fetch('Home/Save', {
615+
method: 'POST',
616+
body: formData
617+
}).then((response) => {
618+
console.log(response);
619+
});
620+
});
621+
622+
```
623+
624+
```csharp
625+
626+
public string Save(SaveSettings saveSettings)
627+
{
628+
ExcelEngine excelEngine = new ExcelEngine();
629+
IApplication application = excelEngine.Excel;
630+
try
631+
{
632+
633+
// Save the workbook as stream.
634+
Stream fileStream = Workbook.Save<Stream>(saveSettings);
635+
// Using XLSIO, we are opening the file stream and saving the file in the server under "Files" folder.
636+
// You can also save the stream file in your server location.
637+
IWorkbook workbook = application.Workbooks.Open(fileStream);
638+
string basePath = _env.ContentRootPath + "\\Files\\" + saveSettings.FileName + ".xlsx";
639+
var file = System.IO.File.Create(basePath);
640+
fileStream.Seek(0, SeekOrigin.Begin);
641+
// To convert the stream to file options.
642+
fileStream.CopyTo(file);
643+
file.Dispose();
644+
fileStream.Dispose();
645+
return string.Empty;
646+
}
647+
catch (Exception ex)
648+
{
649+
return ex.Message;
650+
}
651+
}
652+
653+
```
654+
655+
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).
656+
553657
### Supported file formats
554658

555659
The following list of Excel file formats are supported in Spreadsheet:
@@ -587,7 +691,6 @@ To save the Spreadsheet document as an `xlsx, xls, csv, or pdf` file, by using `
587691
{% endif %}
588692

589693

590-
591694
## Server Configuration
592695

593696
In Spreadsheet component, import and export operation processed in `server-side`, to use importing and exporting in your projects, it is required to create a server with any of the following web services.

0 commit comments

Comments
 (0)