Wednesday, November 4, 2015

Set up SpreadCOM for MVC in Visual Studio 2015.

I won a SpreadCOM license at MeasureUP in a raffle and I've finally figured out how to use it thanks to:

 
 

I don't really understand it all yet. It seems to allow to you to do Excelesque stuff in ASP.NET MVC. The happy pass set up is to...

  1. Install Spread Studio 8.
  2. Make a new MVC application that references in FarPoint.Mvc.Spread.dll and FarPoint.Web.Spread.dll which I found at C:\Program Files (x86)\GrapeCity\Spread Studio 8\ASP.NET\v8.40.20151.0\bin on the other side of the Spread Studio 8 installation.
  3. Manually make a licenses.licx file in your Properties folder by just making a .txt file, outside of Visual Studio 2015, and then renaming it. You will need to include this in your project and put these two lines in it:
    FarPoint.Web.Spread.FpSpread, FarPoint.Web.Spread, Version=8.40.20151.1,
          Culture=neutral, PublicKeyToken=327c3516b1b18457
    FarPoint.Mvc.Spread.FpSpread, FarPoint.Mvc.Spread, Version=8.40.20151.1,
          Culture=neutral, PublicKeyToken=327c3516b1b18457

    If you don't add this file you get an error about how the license can't be found.
  4. You need to add a line for FarPoint to Global.asax.cs too. My Global.asax.cs looks like this:
    using System.Web.Mvc;
    using System.Web.Optimization;
    using System.Web.Routing;
    namespace SpreadComExperiment
    {
       public class MvcApplication : System.Web.HttpApplication
       {
          protected void Application_Start()
          {
             FarPoint.Mvc.Spread.MvcSpreadVirtualPathProvider.AppInitialize();
             AreaRegistration.RegisterAllAreas();
             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
             RouteConfig.RegisterRoutes(RouteTable.Routes);
             BundleConfig.RegisterBundles(BundleTable.Bundles);
          }
       }
    }

 
 

Alright, lets go through the M, the V, and the C in MVC in that order:

  • using System;
    namespace SpreadComExperiment.Views
    {
       [Serializable]
       public class Foo
       {
          public DateTime Bar { get; set; }
          public Boolean Baz { get; set; }
          public Int32 Qux { get; set; }
       }
    }

     
  • @model List<SpreadComExperiment.Views.Foo>
    @using FarPoint.Mvc.Spread
    @{
       Layout = null;
    }
    <html>
       <head>
          <title>Whatever</title>
       </head>
       <body>
          <h1>Whatever</h1>
          @Html.FpSpread("My SpreadCOM Thing", x =>
          {
             x.DataSource = Model;
             x.DataBind();
          })
       </body>
    </html>

     
  • using System;
    using System.Collections.Generic;
    using System.Web.Mvc;
    using SpreadComExperiment.Views;
    namespace SpreadComExperiment.Controllers
    {
       public class HomeController : Controller
       {
          public ActionResult Index()
          {
             return View(new List<Foo>()
             {
                new Foo()
                {
                   Bar = DateTime.Now,
                   Baz = true,
                   Qux = 13
                },
                new Foo()
                {
                   Bar = DateTime.UtcNow,
                   Baz = false,
                   Qux = 42
                }
             });
          }
       }
    }

 
 

We end up with something like so:

Two things about this: You have to slap that Serializable attribute on the model or SpreadCOM's stuff will throw a temper tantrum (error) and in this happy pass example I wasn't really impressed with the printer icon's ability to print. I ended up just printing a blank page. Hey, it's just a start, right?

No comments:

Post a Comment