Search Results for

    Show / Hide Table of Contents

    LangWars (C# / ios-unified)

    Note

    This demo is available in your FlexCel installation at <FlexCel Install Folder>\samples\csharp\VS2022\ios-unified\LangWars and also at https:​//​github.​com/​tmssoftware/​TMS-​FlexCel.​NET-​demos/​tree/​master/​csharp/​VS2022/​ios-​unified/​Lang​Wars

    Overview

    This example shows how to do a simple report doing FlexCel. It will fetch the most used tags from Stack Overflow and rank and provide a chart to visualize them. You can work with online or offline data, in case you don't have access to Stack Overflow.

    In this example, we are exporting the results to an html file and showing the results in a web browser. We could also export to pdf, as shown in the "FlexView" demo.

    Files

    AppDelegate.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Foundation;
    using UIKit;
    
    namespace LangWars
    {
        // The UIApplicationDelegate for the application. This class is responsible for launching the
        // User Interface of the application, as well as listening (and optionally responding) to
        // application events from iOS.
        [Register("AppDelegate")]
        public partial class AppDelegate : UIApplicationDelegate
        {
            // class-level declarations
            public override UIWindow Window
            {
                get;
                set;
            }
            // This method is invoked when the application is about to move from active to inactive state.
            // OpenGL applications should use this method to pause.
            public override void OnResignActivation(UIApplication application)
            {
            }
            // This method should be used to release shared resources and it should store the application state.
            // If your application supports background exection this method is called instead of WillTerminate
            // when the user quits.
            public override void DidEnterBackground(UIApplication application)
            {
            }
    
            /// This method is called as part of the transiton from background to active state.
            public override void WillEnterForeground(UIApplication application)
            {
            }
    
            /// This method is called when the application is about to terminate. Save data, if needed. 
            public override void WillTerminate(UIApplication application)
            {
            }
        }
    }
    

    LangData.cs

    using System;
    using System.Drawing;
    using System.Runtime.Serialization;
    using System.Collections.Generic;
    
    namespace LangWars
    {
        [DataContract]
        class LangDataList
        {
            [DataMember]
            public LangData[] items{ get; set; }
        }
    
        [DataContract]
        class LangData
        {
            [DataMember]
            public string name{ get; set; }
            [DataMember]
            public int count{ get; set; }
        }
    }
    

    LangWarsViewController.cs

    using System;
    using System.Drawing;
    using Foundation;
    using UIKit;
    using System.Collections.Generic;
    using FlexCel.Core;
    using System.Net;
    using System.IO;
    using System.Runtime.Serialization.Json;
    using FlexCel.Report;
    using FlexCel.Render;
    using FlexCel.XlsAdapter;
    using System.Threading.Tasks;
    
    namespace LangWars
    {
        public partial class LangWarsViewController : UIViewController
        {
            bool HTMLLoaded = false;
    
            static bool UserInterfaceIdiomIsPhone
            {
                get { return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone; }
            }
    
            public LangWarsViewController(IntPtr handle) : base (handle)
            {
            }
    
            public override void DidReceiveMemoryWarning()
            {
                // Releases the view if it doesn't have a superview.
                base.DidReceiveMemoryWarning();
    
                // Release any cached data, images, etc that aren't in use.
            }
    
            async public override void ViewDidLoad()
            {
                base.ViewDidLoad();
    
                if (File.Exists(TempHtmlPath))
                {
                    try
                    {
                        await DisplayLastReport();
                    }
                    catch (Exception ex)
                    {
                        SetHTML("<html>" + System.Security.SecurityElement.Escape(ex.Message) + "</html>");
                    }
    
                }
    
                // Perform any additional setup after loading the view, typically from a nib.
            }
    
            async partial void FightClick(NSObject sender)
            {
                await TryCreateReport();
    
            }
    
            async Task<bool> TryCreateReport()
            {
                try
                {
                    ProgressIndicator.StartAnimating();
                    try
                    {
                        bool Offline = OfflineSwitch.SelectedSegment == 0;
                        await Task.Run(()=>CreateReport(Offline));
                    }
                    finally
                    {
                        ProgressIndicator.StopAnimating();
                    }
                }
                catch (Exception ex)
                {
                    SetHTML("<html>" + System.Security.SecurityElement.Escape(ex.Message) + "</html>");
                    return false;
                }
    
                LoadHTML();
                return true;
            }
    
            async Task DisplayLastReport()
            {
                if (!File.Exists(TempHtmlPath))
                {
                    await TryCreateReport();
                    return;
                }
    
                LoadHTML();
            }
    
            void CreateReport(bool Offline)
            {
                var Langs = Offline? LoadData(): FetchData();
                var xls = RunReport(Langs);
                xls.Save(TempXlsPath); //we save it to share it and to display it on startup.
                GenerateHTML(xls);
            }
    
            LangDataList FetchData()
            {
                var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.stackexchange.com/2.1/tags?order=desc&sort=popular&site=stackoverflow&pagesize=5");
                httpWebRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
                httpWebRequest.Method = "GET";
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(LangDataList));
                return (LangDataList)ser.ReadObject(httpResponse.GetResponseStream());
            }
    
            LangDataList LoadData()
            {
                using (var offlineData = new FileStream(Path.Combine(NSBundle.MainBundle.BundlePath, "OfflineData.txt"), FileMode.Open, FileAccess.Read))
                {
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(LangDataList));
                    return (LangDataList)ser.ReadObject(offlineData);
                }
            }
    
            ExcelFile RunReport(LangDataList langs)
            {
                ExcelFile Result = new XlsFile(Path.Combine(NSBundle.MainBundle.BundlePath, "report.template.xls"), true);
                using (FlexCelReport fr = new FlexCelReport(true))
                {
                    fr.AddTable("lang", langs.items);
                    fr.Run(Result);
                }
                return Result;
            }
    
            string TempHtmlPath
            { 
                get
                {
                    return Path.Combine(
                        Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), 
                        "langwars.html"); 
                } 
            }
    
            string TempXlsPath
            { 
                get
                {
                    return Path.Combine(
                        Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), 
                        "langwars.xls"); 
                } 
            }
    
            void GenerateHTML(ExcelFile xls)
            {
    
                using (FlexCelHtmlExport html = new FlexCelHtmlExport(xls, true))
                {
                    //If we were using png, we would have to set
                    //a high resolution so this looks nice in high resolution displays.
                    //html.ImageResolution = 326;
    
                    //but we will use SVG, which is vectorial:
                    html.HtmlVersion = THtmlVersion.Html_5;
                    html.SavedImagesFormat = THtmlImageFormat.Svg;
                    html.EmbedImages = true;
    
                    html.Export(TempHtmlPath, ".");
                }
    
            }
    
            void LoadHTML()
            {
                if (!HTMLLoaded)
                {
                    ResultsWindow.LoadRequest(new NSMutableUrlRequest(NSUrl.FromFilename(TempHtmlPath), 
                                                                      NSUrlRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData, 60));
                    HTMLLoaded = true;
                }
                else
                {
                    //WebView doesn't refresh the images if you just load the request again (even if ignoring the cache). So we need to call Reload instead
                    ResultsWindow.Reload();
                }
            }
    
            void SetHTML(string html)
            {
                ResultsWindow.LoadHtmlString(html, null);
                HTMLLoaded = false;
            }
    
            async partial void ShareClick(NSObject sender)
            {
                if (!File.Exists(TempXlsPath))
                {
                    if (!await TryCreateReport()) return;
                }
                UIDocumentInteractionController docController = new UIDocumentInteractionController();
                docController.Url = NSUrl.FromFilename(TempXlsPath);
                docController.PresentOptionsMenu((UIBarButtonItem) sender, true);
    
            }
        }
    }
    

    LangWarsViewController.designer.cs

    // WARNING
    //
    // This file has been generated automatically by Xamarin Studio to store outlets and
    // actions made in the UI designer. If it is removed, they will be lost.
    // Manual changes to this file may not be handled correctly.
    //
    using Foundation;
    using System.CodeDom.Compiler;
    
    namespace LangWars
    {
       [Register ("LangWarsViewController")]
        partial class LangWarsViewController
        {
           [Outlet]
            UIKit.UISegmentedControl OfflineSwitch { get; set; }
    
           [Outlet]
            UIKit.UIActivityIndicatorView ProgressIndicator { get; set; }
    
           [Outlet]
            UIKit.UIWebView ResultsWindow { get; set; }
    
           [Outlet]
            UIKit.UIBarButtonItem ShareButton { get; set; }
    
           [Action ("FightClick:")]
            partial void FightClick (Foundation.NSObject sender);
    
           [Action ("ShareClick:")]
            partial void ShareClick (Foundation.NSObject sender);
    
            void ReleaseDesignerOutlets ()
            {
                if (OfflineSwitch != null) {
                    OfflineSwitch.Dispose ();
                    OfflineSwitch = null;
                }
    
                if (ResultsWindow != null) {
                    ResultsWindow.Dispose ();
                    ResultsWindow = null;
                }
    
                if (ProgressIndicator != null) {
                    ProgressIndicator.Dispose ();
                    ProgressIndicator = null;
                }
    
                if (ShareButton != null) {
                    ShareButton.Dispose ();
                    ShareButton = null;
                }
            }
        }
    }
    

    Main.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Foundation;
    using UIKit;
    
    namespace LangWars
    {
        public class Application
        {
            // This is the main entry point of the application.
            static void Main(string[] args)
            {
                // if you want to use a different Application Delegate class from "AppDelegate"
                // you can specify it here.
                UIApplication.Main(args, null, "AppDelegate");
            }
        }
    }
    
    In This Article
    Back to top FlexCel Studio for the .NET Framework v7.24.0.0
    © 2002 - 2025 tmssoftware.com