FlexView (C# / ios-unified)
Note
This demo is available in your FlexCel installation at <FlexCel Install Folder>\samples\csharp\VS2022\ios-unified\FlexView and also at https://github.com/tmssoftware/TMS-FlexCel.NET-demos/tree/master/csharp/VS2022/ios-unified/FlexView
Overview
This example shows how to import xls and xlsx files from other applications (like mail or dropbox) into a Xamarin.iOS app using FlexCel, and also how to export pdf files from your app to other applications and for printing.
In order to use this sample, you need to first open a different app, like mail, locate an xls or xlsx attachment, press "Share", and select "FlexView". FlexView will convert that file to pdf and show it in a browser.
Once you have the file in FlexView, you can export the generated pdf back to mail, or dropbox, or any other app that can handle pdf files in your device. You can also print the pdf file.
There is a step-by-step tutorial on how this app was created available at https://doc.tmssoftware.com/flexcel/net/tutorials/ios-tutorial.html
Files
AppDelegate.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;
namespace FlexView
{
// 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)
{
}
public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
{
return ((FlexViewViewController)Window.RootViewController).Open(url);
}
}
}
FlexViewViewController.cs
using System;
using System.Drawing;
using Foundation;
using UIKit;
using FlexCel.Render;
using FlexCel.XlsAdapter;
using System.IO;
using System.Collections.Generic;
namespace FlexView
{
public partial class FlexViewViewController : UIViewController
{
static bool UserInterfaceIdiomIsPhone
{
get { return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone; }
}
public FlexViewViewController(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.
}
#region View lifecycle
public override void ViewDidLoad()
{
base.ViewDidLoad();
Viewer.LoadHtmlString("<html><h1>Please share an xls or xlsx file <br>from other app into FlexView.<br/ >" +
"For help on how to use this example, please read the " + "" +
"<a href=\"https://www.tmssoftware.com/flexcel/docs/net/FlexCelViewTutorial.pdf\">tutorial</a></h1></html>", null);
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
}
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
}
public override void ViewDidDisappear(bool animated)
{
base.ViewDidDisappear(animated);
}
#endregion
NSUrl XlsUrl;
string XlsPath;
string PdfPath;
public bool Open(NSUrl url)
{
XlsUrl = url;
XlsPath = url.Path;
return Refresh();
}
private void RemoveOldPdf()
{
if (PdfPath != null)
{
try
{
File.Delete(PdfPath);
}
catch
{
//do nothing, this was just a cache that will get deleted anyway.
}
PdfPath = null;
}
}
private bool Refresh()
{
RemoveOldPdf();
try
{
XlsFile xls = new XlsFile(XlsPath);
PdfPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.InternetCache),
Path.ChangeExtension(Path.GetFileName(XlsUrl.Path), ".pdf"));
using (FlexCelPdfExport pdf = new FlexCelPdfExport(xls, true))
{
using (FileStream fs = new FileStream(PdfPath, FileMode.Create))
{
pdf.Export(fs);
}
}
Viewer.LoadRequest(new NSUrlRequest(NSUrl.FromFilename(PdfPath)));
}
catch (Exception ex)
{
Viewer.LoadHtmlString("<html>Error opening " + System.Security.SecurityElement.Escape(Path.GetFileName(XlsUrl.Path))
+ "<br><br>" + System.Security.SecurityElement.Escape(ex.Message) + "</html>", null);
return false;
}
return true;
}
partial void ShareClick(UIKit.UIBarButtonItem sender)
{
if (PdfPath == null)
{
ShowHowToUse();
return;
}
UIDocumentInteractionController docController = new UIDocumentInteractionController();
docController.Url = NSUrl.FromFilename(PdfPath);
docController.PresentOptionsMenu(ShareButton, true);
}
partial void RandomizeClick(UIKit.UIBarButtonItem sender)
{
if (XlsUrl == null)
{
ShowHowToUse();
return;
}
XlsFile xls = new XlsFile(XlsPath, true);
//We'll go through all the numeric cells and make them random numbers
Random rnd = new Random();
for (int row = 1; row <= xls.RowCount; row++)
{
for (int colIndex = 1; colIndex < xls.ColCountInRow(row); colIndex++)
{
int XF = -1;
object val = xls.GetCellValueIndexed(row, colIndex, ref XF);
if (val is double) xls.SetCellValue(row, xls.ColFromIndex(row, colIndex), rnd.Next());
}
}
//We can't save to the original file, we don't have permissions.
XlsPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.InternetCache),
"tmpFlexCel" + Path.GetExtension(XlsUrl.Path));
xls.Save(XlsPath);
Refresh();
}
void ShowHowToUse()
{
using (var alert = UIAlertController.Create("Please open FlexView from other Application.",
"In order to use this example you need to go to another app"
+ " like dropbox or mail, and share an xls or xlsx file with FlexView."
+ " When you click \"Share\" and select \"FlexView\" in the other app,"
+ " the file will be converted to pdf and previewed with FlexView.", UIAlertControllerStyle.Alert))
{
alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Cancel, null));
PresentViewController(alert, animated: true, completionHandler: null);
}
}
}
}
FlexViewViewController.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 FlexView
{
[Register ("FlexViewViewController")]
partial class FlexViewViewController
{
[Outlet]
UIKit.UIBarButtonItem RandomizeButton { get; set; }
[Outlet]
UIKit.UIBarButtonItem ShareButton { get; set; }
[Outlet]
UIKit.UIWebView Viewer { get; set; }
[Action ("RandomizeClick:")]
[GeneratedCodeAttribute ("iOS Designer", "1.0")]
partial void RandomizeClick (UIKit.UIBarButtonItem sender);
[Action ("ShareClick:")]
partial void ShareClick (UIKit.UIBarButtonItem sender);
void ReleaseDesignerOutlets ()
{
if (RandomizeButton != null) {
RandomizeButton.Dispose ();
RandomizeButton = null;
}
if (ShareButton != null) {
ShareButton.Dispose ();
ShareButton = null;
}
if (Viewer != null) {
Viewer.Dispose ();
Viewer = null;
}
}
}
}
Main.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;
namespace FlexView
{
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");
}
}
}