Back To .NET Standard Tutorials

SpreadsheetGear Engine for .NET Tutorials

Xamarin.iOS Single View App for iPhone/iPad - Spreadsheet Calculations using Visual Studio for Windows

Follow these steps to create a simple Xamarin.iOS Single View App for iPhone/iPad with Visual Studio for Windows that utilizes SpreadsheetGear Engine for .NET to perform a basic formula calculation.

Prerequisites:

  • Developing Xamarin.iOS apps in Visual Studio for Windows requires connecting to a Mac with the following installed:
    • OS X El Capitan (10.11) or higher
    • Xamarin Studio 5.10 or higher
    • Xamarin.iOS SDK
    • Apple Xcode IDE and iOS 7 or higher
    Please ensure you have a Mac setup with these requirements before going through this tutorial. You can find further instructions on Xamarin's Connecting to the Mac Getting Started page.

Create a new iOS Single View App

  1. Launch Visual Studio 2017.
  2. On the File menu, navigate to New Solution...
  3. Select iPhone under Visual C# → iOS.
  4. Select Single View App (iPhone)
  5. Provide a project name of "SGPhoneApp" 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.

Create iOS App's UI

  1. From the Solution Explorer, open Main.storyboard.
  2. From the Visual Studio Toolbox, drag and drop a Button Control near the upper-left corner of the Main.storyboard design surface.
  3. Give the Button Control a name of "EarthDiameterButton" from the Properties Panel under Widget → Identity → Name.
  4. Give the Button Control a title of "Calculate Earth's Diameter" from the Properties Panel under Widget → Button → Title. Increase the width of the Button to fit the new Title text (approximately 200).
  5. From the Visual Studio Toolbox, drag and drop a Text Field Control below the Button Control added in the previous step.
  6. Give the Text Field Control a name of "EarthDiameter" from the Properties Panel under Widget → Identity → Name. Also increase the width of the Text Field to approximately 200.

Add Button event handler and SpreadsheetGear calculations

  1. Double-click on the EarthDiameterButton Button Control, which should open ViewController.cs and add a "EarthDiameterButton_TouchUpInside" event handler.
  2. Replace the event handler's code with the following:
    partial void EarthDiameterButton_TouchUpInside(UIButton sender)
    {
        // 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()";
    
        EarthDiameter.Text = a1.Value + " miles";
    }

Build and run the application

  1. From the Standard Toolbox, hit the Drop Down located on the button used to start debugging your application. Choose an iPhone to use while testing this app, for instance, the iPhone 7 iOS 10.3 option.
  2. From the Debug menu, select Start Without Debugging.
  3. An iPhone emulator should launch, as well as your SGPhoneApp.
  4. Clicking on the "Calculate Earth's Diameter" should result in a calculated value in the Text Field of:
    7926.40954629997 miles