Back To .NET Standard Tutorials

SpreadsheetGear Engine for .NET Tutorials

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

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

Create a new Android Single View App

  1. Launch Xamarin Studio.
  2. On the File menu, navigate to New Solution...
  3. Select App under the Android Category on the left.
  4. Under the General section, select "Android App" and click Next.
  5. On the next screen, provide an App Name of "SGAndroidApp" and customize the other options as desired. Click Next.
  6. Provide additional details on the next screen as desired and click Create.

Add SpreadsheetGear Engine for .NET to your project

  1. On the Project menu, select Add NuGet Packages... to launch the Add Packages dialog.
  2. Search for "SpreadsheetGear" in the search box.
  3. In the list of NuGet packages, select "SpreadsheetGear Engine for .NET" and click Add Package.
  4. You will be prompted to accept SpreadsheetGear's License Agreement as well as License Agreements of other Microsoft packages that SpreadsheetGear Engine for .NET depends on. Click Accept if you wish to proceed.
  5. Xamarin Studio should begin the installation process and add a reference to SpreadsheetGear in your Project's References folder, under From Packages.

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 Run 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