Loading an Excel File from the Server and Saving it to the Server

8 posts, 1 answers
  1. Cory
    Cory avatar
    2 posts
    Member since:
    Apr 2018

    Posted 17 Apr 2018 Link to this post

    I have a situation where an administrative user creates an Excel file using her desktop copy of Excel and saves it to a location on our server. What I want to do is load that file from the server into a Spreadsheet control, have a user make changes in the Spreadsheet, and then click a button to save it back to a location on the server.

    I see plenty of examples of importing files from the user's local system and exporting them to the user' local system. However, I don't see any examples of loading a file from the SERVER when the spreadsheet first loads. Similarly, I can't find an example of saving the spreadsheet contents as an Excel file on the SERVER.

    Can somebody please provide an example of this?

    Thank you!

  2. Answer
    Veselin Tsvetanov
    Admin
    Veselin Tsvetanov avatar
    1201 posts

    Posted 19 Apr 2018 Link to this post

    Hello Cory,

    The particular example for loading from and saving to the server an xlsx file would depend on the server technology of choice. Nevertheless, in all cases the principles from the Server import / export demo will apply:

    - An AJAX request will be made initially, so the xlsx data gets delivered to the client as JSON;

    - On the server, the xlsx file should be converted to JSON;

    - The fromJSON() method of the Spreadsheet will be used to load the initial data to the widget;

    - Upon save, the toJSON() method of the Spreadsheet will be used;

    - That JSON will be submitted with a form or with an AJAX request to the server;

    - The JSON sent to the server will be processed, converted to xlsx and saved on the server;

    Attached you will find a .NET MVC project, following the above scenario and using the Telerik.Web.Spreadsheet and the Telerik Document Processing library to manipulate the file on the server.

    I hope, that this helps. In case you have any other questions, please do not hesitate to contact us.

    Regards,
    Veselin Tsvetanov
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Cory
    Cory avatar
    2 posts
    Member since:
    Apr 2018

    Posted 19 Apr 2018 in reply to Veselin Tsvetanov Link to this post

    Thank you very much! That step of converting to/from JSON was exactly what I needed.
  4. Franz
    Franz avatar
    1 posts
    Member since:
    Nov 2019

    Posted 16 Dec 2019 Link to this post

    Hello,

    this is exactly what I need but is it also available for asp.NET Core 3 ?

    Regards

    Franz

  5. Veselin Tsvetanov
    Admin
    Veselin Tsvetanov avatar
    1201 posts

    Posted 17 Dec 2019 Link to this post

    Hello Franz,

    I am afraid that the approach described above could not be applied in a .Net Core scenario. The reason for that is the fact that there is no .Net Core implementation of the Telerik.Web.Spreadsheet package (the one which holds the definition of the Workbook class, the workbook.Save() and workbook.ToJson() methods). Even if we are planning to include the Telerik SpreadProcessing within our very next release, the Web.Spreadsheet package would not be available.

    Having that said, I would suggest you continue the communication in the support thread that you have opened on the same topic. Peter is currently working on a possible alternative approach that would allow communication between server and client using XLSX files.

    Regards,
    Veselin Tsvetanov
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  6. Daniel
    Daniel avatar
    2 posts
    Member since:
    Jul 2015

    Posted 15 Jan in reply to Veselin Tsvetanov Link to this post

     

    Hi Veselin,

    I am in the same boat here, we are a .NET Core environment (version 2.1 to be specific), is there an option for us as you mentioned in this post?

    Thanks,

    Daniel

     

    Veselin Tsvetanov said:

    Hello Franz,

    I am afraid that the approach described above could not be applied in a .Net Core scenario. The reason for that is the fact that there is no .Net Core implementation of the Telerik.Web.Spreadsheet package (the one which holds the definition of the Workbook class, the workbook.Save() and workbook.ToJson() methods). Even if we are planning to include the Telerik SpreadProcessing within our very next release, the Web.Spreadsheet package would not be available.

    Having that said, I would suggest you continue the communication in the support thread that you have opened on the same topic. Peter is currently working on a possible alternative approach that would allow communication between server and client using XLSX files.

    Regards,
    Veselin Tsvetanov
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.

     

     

  7. Veselin Tsvetanov
    Admin
    Veselin Tsvetanov avatar
    1201 posts

    Posted 17 Jan Link to this post

    Hello Daniel,

    Attached you will find an alternative approach to the discussed scenario in .Net Core. The content is sent to and received from the server as an XLSX file. Here is how the file is requested:

    var spread = $('#spreadsheet').getKendoSpreadsheet();
    
    function b64toBlob(dataURI) {
    	var byteString = atob(dataURI.split(',')[1]);
    	var ab = new ArrayBuffer(byteString.length);
    	var ia = new Uint8Array(ab);
    
    	for (var i = 0; i < byteString.length; i++) {
    		ia[i] = byteString.charCodeAt(i);
    	}
    
    	return new Blob([ab], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
    }
    
    $.ajax({
        url: '@Url.Action("GetFile", "Home")',
        method: "get",
        success:function(data){
            spread.fromFile(b64toBlob(data));
        }
    })

    And here is how the AJAX call is handled on the server:

    [HttpGet]
    public string GetFile()
    {
        byte[] fileByteArray = System.IO.File.ReadAllBytes(_env.ContentRootPath + "\\App_data\\sample.xlsx");
        string file = Convert.ToBase64String(fileByteArray);
        return "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64," + file;
    }

    As per saving the file, the SPreadsheet is configured to force its proxy call upon Excel export:

    $('#spreadsheet').kendoSpreadsheet({
        excel: {
            proxyURL: '@Url.Action("SaveFile", "Home")',
            forceProxy: true
        }
    });

    Here is the implementation of the proxy action on the server:

    public ActionResult SaveFile(string contentType, string base64, string fileName)
    {
        var fileContents = Convert.FromBase64String(base64);
        System.IO.File.WriteAllBytes(_env.ContentRootPath + "\\App_data\\sample.xlsx", fileContents);
        return View("Index");
    }

    I hope that this helps.

    Regards,
    Veselin Tsvetanov
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  8. Daniel
    Daniel avatar
    2 posts
    Member since:
    Jul 2015

    Posted 22 Jan in reply to Veselin Tsvetanov Link to this post

    Hi Veselin,

    Thank you so much, this was exactly what we needed!

    Daniel

Back to Top