Back To .NET Standard Tutorials

SpreadsheetGear Engine for .NET Tutorials

Xamarin.Android Single View App - Spreadsheet Calculations using Visual Studio for Windows

Follow these steps to create a simple Xamarin.Android Single-View App for Android devices with Visual Studio for Windows that utilizes SpreadsheetGear Engine for .NET to perform a basic formula calculation.

Create a new Android Single-View App

  1. Launch Visual Studio 2017.
  2. On the File menu, navigate to New Solution...
  3. Select Single-View App (Android) under Visual C# → Android.
  4. Provide a project name of "SGAndroidApp" and click OK.

Add SpreadsheetGear Engine for .NET to your project

  1. On the Project menu, click Manage NuGet Packages. The NuGet Package Manager window appears.
  2. In the NuGet Package Manager window, click the Browse tab and enter "SpreadsheetGear" in the search field.
  3. In the list of NuGet packages, select "SpreadsheetGear" and click Install.
  4. You might be prompted to confirm this change to your project. Click OK.
  5. You will be prompted to accept SpreadsheetGear's License Agreement. Click I Accept if you wish to proceed.
  6. Visual Studio should begin the installation process and add a reference to SpreadsheetGear in your Project's References folder.
  7. Close the NuGet Package Manager UI Tab.

Modify Button event handler and SpreadsheetGear calculations

  1. Your project should already have a main layout page with a button named MyButton. This tutorial will use this existing button to call SpreadsheetGear code.
  2. Open MainActivity.cs in your project's root folder.
  3. Add the following method to the MainActivity class:
        private double CalculateEarthDiameter()
        {
            // Create a new empty workbook in a new workbook set.
            SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
    
            // Get a reference to the first worksheet.
            SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets["Sheet1"];
    
            // Get a reference to the top left cell of Sheet1.
            SpreadsheetGear.IRange a1 = worksheet.Cells["A1"];
    
            // Set a formula.
            a1.Formula = "=24901.55 / PI()";
    
            return (double)a1.Value;
        }
  4. Modify the final line of the OnCreate method as follows:
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
    
            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.MyButton);
    
        button.Click += delegate { button.Text = CalculateEarthDiameter() + " miles"; };
        }

Build and run the application

  1. From the Debug menu, select Start Without Debugging.
  2. An Android emulator should launch, as well as your SGAndroidApp.
  3. Clicking on the "HELLO WORLD, CLICK ME!" button should result in this text being replaced with the calculated value representing Earth's diameter, as shown below:
    Android Emulator Screenshot of SpreadsheetGear Calculation