ASP.NET MVC Controls | ComponentOne
Working with Controls / FlexSheet / Work with FlexSheet / Remote Loading and Saving of Excel
In This Topic
    Remote Loading and Saving of Excel
    In This Topic

    FlexSheet allows you to remotely load and save excel file using C1JSONRequest. This specifies remote URLs to load data from excel file or workbook in FlexSheet control, and save FlexSheet data to server as Excel file or workbook through action.

    The below example loads an excel file, placed on server, in FlexSheet using Load method of FlexSheetHelper class.

    Note that FlexSheet requires jszip.min.js file for remote loading and saving of excel. Therefore, you need to add this file to your application and provide reference to it in the respective view or in <head> section of _Layout.cshtml file.

    The following code examples demonstrate how to remotely load data from excel file or workbook in FlexSheet control:

    In Code

    Include the MVC references as shown below.

    C#
    Copy Code
    using C1.Web.Mvc.Sheet;
    using System;
    using System.Collections;
    using System.Globalization;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Collections.Generic;
    using C1.Web.Mvc.Serializition;
    

    RemoteLoadController.cs

    C#
    Copy Code
    public class RemoteController : Controller
    {
        // GET: /<controller>/
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult RemoteLoadXlsx()
        {
            return this.C1Json(FlexSheetHelper.Load("~/Content/ExcelFiles/WorkBook.xlsx"), null, null, JsonRequestBehavior.AllowGet);
        }
    }
    

    Here, the Excel workbook that is loaded remotely is placed in Content folder of the application.

    RemoteLoading.cshtml

    Razor
    Copy Code
    @using C1.Web.Mvc.Sheet;
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
    
    <div>   
    @(Html.C1().FlexSheet().CssClass("flexSheet").Height(700).Width(700)
            .RemoteLoad(Url.Action("RemoteLoadXlsx"))
            )
    </div>
    

    Back to Top

    Remote Save

    In Code

    RemoteSaveController.cs 

    C#
    Copy Code
    public class RemoteSaveController : Controller
    {
        // GET: /<controller>/
        public static List<Sale> SALES = Sale.GetData(50).ToList();
        public ActionResult Index()
        {
            return View(SALES);
        }
    
        private const string FILE_PATH = "\\wwwroot\\uploadFile\\save.xlsx";
        public JsonResult RemoteSaveFile([FlexSheetRequest]FlexSheetSaveRequest request)
        {
            var success = true;
            var error = "";
            var app = GetService<IApplicationEnvironment>();
            var savePath = app.ApplicationBasePath + FILE_PATH;
            try
            {
                Stream st = request.GetFileStream();
                using (FileStream fs = new FileStream(savePath, FileMode.Create))
                {
                    if (st != null)
                    {
                        st.CopyTo(fs);
                    }
                }
            }
            catch (Exception e)
            {
                success = false;
                error = e.ToString();
            }
    
            return this.C1Json(FlexSheetHelper.Save(success, error));
        }
    
        public FileResult DownloadFile()
        {
            var app = GetService<IApplicationEnvironment>();
            var savePath = app.ApplicationBasePath + FILE_PATH;
            var name = Path.GetFileName(FILE_PATH);
            return File(new FileStream(savePath, FileMode.Open, FileAccess.Read),
                "application/msexcel", name);
        }
    
        public static T GetService<T>() where T : class
        {
            var serviceProvider = CallContextServiceLocator.Locator.ServiceProvider;
            return serviceProvider.GetService(typeof(T)) as T;
        }
    }
    


    RemoteSaving.cshtml

    Razor
    Copy Code
    @using C1MvcFSheetNew.Models;
    @model IEnumerable<sale>
    
    <script>
            function remoteSave() {
                var flexSheet = wijmo.Control.getControl('#flexSheet');
                flexSheet.remoteSave(c1.mvc.grid.sheet.ContentType.Xlsx);
            }
    
            function onFileSaved(sender, args) {
                if (args.success) {
                    window.location.href = '@Url.Action("DownloadFile")';
                } else {
                    alert(args.error);
                }
            }
    </script>
    
    <div>
        @(Html.C1().FlexSheet().CssClass("flexSheet").Width("500px").Height("700px").RemoteSave(Url.Action("RemoteSaveFile"))
            .OnClientRemoteSaved("onFileSaved")
            .AddBoundSheet(sheet =>
                sheet.Bind(cv =>
                                cv.Bind(Model)))
        )
    </div>
    

    Back to Top

    See Also