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
- Launch Visual Studio 2017.
- On the File menu, navigate to New Solution...
- Select Single-View App (Android) under Visual C# → Android.
- Provide a project name of "SGAndroidApp" and click OK.
Add SpreadsheetGear Engine for .NET to your project
- On the Project menu, click Manage NuGet Packages. The NuGet Package Manager window appears.
- In the NuGet Package Manager window, click the Browse tab and enter "SpreadsheetGear" in the search field.
- In the list of NuGet packages, select "SpreadsheetGear" and click Install.
- You might be prompted to confirm this change to your project. Click OK.
- You will be prompted to accept SpreadsheetGear's License Agreement. Click I Accept if you wish to proceed.
- Visual Studio should begin the installation process and add a reference to SpreadsheetGear in your Project's References folder.
- Close the NuGet Package Manager UI Tab.
Modify Button event handler and SpreadsheetGear calculations
- 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.
- Open MainActivity.cs in your project's root folder.
- 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; }
- 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
- From the Debug menu, select Start Without Debugging.
- An Android emulator should launch, as well as your SGAndroidApp.
- 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: