Open excel from local or from server and save to server - EJ2

I'm trying Spreadsheet control to display excel file where the file can be opened from server or local computer. When saving the file I want to save it to the server although it's opened from local computer. When the page is loaded, it will automatically open excel file from server where users can replace the file by opening from local computer and save back to server. I just see "Save As" instead of "Save". I'm using ASP.NET Core Razor Pages. This is the code and it failed

 _Layout.cshtml
<script type="text/javascript" src="~/syncfusion/ej2-base/scripts/ej2.min.js"></script>
<script type="text/javascript" src="~/syncfusion/ej2-spreadsheet/dist/global/ej2-spreadsheet.min.js"></script>

<body>
..
<ejs-scripts></ejs-scripts>
    @await RenderSectionAsync("Scripts", required: false)
</body>

Index.chstml
<ejs-spreadsheet id="spreadsheet"
                 enableContextMenu="true"
                 openUrl="/LoadExcel" saveUrl="/SaveExcel"
                 allowOpen="true" allowSave="true"
                 created="onCreated">
</ejs-spreadsheet>

function onCreated() {
var spreadsheetObj = ej.base.getComponent(document.getElementById('spreadsheet'), 'spreadsheet');
var request = new XMLHttpRequest();
request.responseType = "blob";
request.onload = () => {
var file = new File([request.response], "Book.xlsx");
spreadsheetObj.open({ file: file });
}
request.open("GET", "/Index/LoadExcel");
request.send();
}

Index.chstml.cs (IndexModel class)
public IActionResult OnPostSaveExcel(SaveSettings saveSettings)
{
Workbook.Save(saveSettings);

ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
// Convert Spreadsheet data as Stream 
Stream fileStream = Workbook.Save<Stream>(saveSettings);
IWorkbook workbook = application.Workbooks.Open(fileStream);
var filePath = "path_to_excel\Book.xlsx";
FileStream outputStream = new FileStream(filePath, FileMode.Create);
workbook.SaveAs(outputStream);
return Content("Spreadsheet saved in server");
}

public IActionResult OnGetLoadExcel()
{
var filePath = "path_to_excel\Book.xlsx\Book1.xlsx";
ExcelEngine excelEngine = new ExcelEngine();
IWorkbook workbook;
FileStream fs = System.IO.File.Open(filePath, FileMode.Open); // converting excel file to stream 
workbook = excelEngine.Excel.Workbooks.Open(fs, ExcelOpenType.Automatic); // coverting stream to XlsIO workbook 
MemoryStream outputStream = new MemoryStream();
workbook.SaveAs(outputStream);
IFormFile formFile = new FormFile(outputStream, 0, outputStream.Length, "", "Book1.xlsx"); // converting MemoryStream to IFormFile 
OpenRequest open = new OpenRequest();
open.File = formFile;
fs.Close();
var json = Workbook.Open(open);
return Content(json);
}



8 Replies 1 reply marked as answer

SP Sangeetha Priya Murugan Syncfusion Team December 28, 2020 11:04 AM UTC

Hi Moh, 
 
Thank you for contacting Syncfusion support. 
 
We have checked your reported requirement and it can be achievable in our spreadsheet by using save method. For your convenience, we have prepared the sample that save the excel file to the server with the entered filename input in a button click event as like as below. 
 
<input type="text" id="fileName" placeholder="Enter the FileName"/> 
<button class="e-btn" onclick="saveToServer()">SaveAs To Server</button> 
 
<ejs-spreadsheet id="spreadsheet" openUrl="/Index/Open" saveUrl="/Index/Save" created="onCreated"> 
 
</ejs-spreadsheet> 
 
<script> 
    function saveToServer() { 
        var spreadsheetObj = ej.base.getComponent(document.getElementById('spreadsheet'), 'spreadsheet'); 
        var fileName = document.getElementById("fileName").value; 
        spreadsheetObj.save({ fileName: fileName }); // To save the filename with the entered file name input 
    } 
</script> 
 
  public void OnPostSave(SaveSettings saveSettings) 
        { 
            ExcelEngine excelEngine = new ExcelEngine(); 
            IApplication application = excelEngine.Excel; 
                // Convert Spreadsheet data as Stream 
                Stream fileStream = Workbook.Save<Stream>(saveSettings); 
                IWorkbook workbook = application.Workbooks.Open(fileStream); 
                var filePath = _hostingEnvironment.WebRootPath + @"\" + saveSettings.FileName + ".xlsx"; 
                FileStream outputStream = new FileStream(filePath, FileMode.Create); 
                workbook.SaveAs(outputStream); 
                workbook.Close(); 
              outputStream.Dispose(); 
        } 
 
 
Could you please check the above link and get back to us, if you need any further assistance on this. 
 
Regards, 
Sangeetha M 




RA Ramzi December 28, 2020 06:08 PM UTC

When saving to server the file name is always Sample.xls although I open from local with different name. 
function saveToServer() {
        var spreadsheetObj = ej.base.getComponent(document.getElementById('spreadsheet'), 'spreadsheet');
        var element = document.getElementById("fileName"); // it's always null
        if (element)
            spreadsheetObj.save({ fileName: element.value }); // To save the filename with the entered file name input
        else
            spreadsheetObj.save();
    }


RA Ramzi December 29, 2020 07:20 AM UTC

This is the sample code I've created. The only problem now is when saving back to server using custom button or spreadsheet control the file name in saveSettings.FileName is always "Sample" event when I open an excel file locally. It should keep original file name without specifying one.

Attachment: SyncfusionExcelWeb_cb3be3e1.zip


SP Sangeetha Priya Murugan Syncfusion Team December 29, 2020 07:39 AM UTC

Hi Moh, 
 
Thank you for your update. 
 
We have checked your reported requirement and we would like to let you know that in our previously updated sample, you need to enter the file name in the input. After that you need to save the file with the entered filename input in a button click event. For your convenience, we have prepared the video demonstration of this requirement. Please find the link below. 
 
 
Could you please check the above link and get back to us, if you need any further assistance on this. 
 
Regards, 
Sangeetha M 




RA Ramzi December 29, 2020 08:28 AM UTC

No, that's not what I need. In the video I have to specify the file name. All I need is getting the original file name opened both from server or local computer.


SP Sangeetha Priya Murugan Syncfusion Team December 30, 2020 07:34 AM UTC

Hi Moh, 
 
Thank you for your update. 
 
We have checked your reported requirement “To get the filename of the imported excel file and save it again with the same name”. And it can be achievable in our spreadsheet by using beforeOpen event and save method as like as below. 
 
<button class="e-btn" onclick="saveToServer()">SaveAs To Server</button> 
 
<ejs-spreadsheet id="spreadsheet" openUrl="/Index/Open" saveUrl="/Index/Save" created="onCreated" beforeOpen="onBeforeOpen"> 
 
</ejs-spreadsheet> 
 
<script> 
    var fileName; //  variable to get the file name while importing 
    function onCreated() { 
        var spreadsheetObj = ej.base.getComponent(document.getElementById('spreadsheet'), 'spreadsheet'); 
        var request = new XMLHttpRequest(); 
        request.responseType = "blob"; 
        request.onload = () => { 
            var file = new File([request.response], "customfile.xlsx"); 
            spreadsheetObj.open({ file: file }); 
        } 
        request.open("GET", "/" + "customfile.xlsx"); 
        request.send(); 
    } 
    function onBeforeOpen(args) { 
        fileName = args.file.name.split(".xlsx")[0]; // To get the file name while importing 
    } 
    function saveToServer() { 
        var spreadsheetObj = ej.base.getComponent(document.getElementById('spreadsheet'), 'spreadsheet'); 
        spreadsheetObj.save({ fileName: fileName }); // To save the excel with the opened file name 
    } 
</script> 
 
 
Could you please check the above link and get back to us, if you need any further assistance on this. 
 
Regards, 
Sangeetha M 



Marked as answer

RA Ramzi December 30, 2020 03:04 PM UTC

Thank you for the great support. It's now working as expected


SP Sangeetha Priya Murugan Syncfusion Team December 31, 2020 06:10 AM UTC

Hi Moh, 
 
Thanks for your update. 
 
We are happy to hear that your issue has been resolved. Kindly get back to us if you need any further assistance. 
 
Regards, 
Sangeetha M 



Loader.
Up arrow icon