We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How do you display the excel sheet on your phone?

I am using the code sample from http://help.syncfusion.com/xamarin/xlsio/getting-started

I have button that when I click on it, it calls method below.  Nothing happens, I know that its hitting the method because i set a breakpoint.
I am testing with Xamarin Android Player, would that be the issue?

        public void OnClicked(object sender, EventArgs e)
        {
            //Instantiate excel engine
            ExcelEngine excelEngine = new ExcelEngine();
            //Excel application
            IApplication application = excelEngine.Excel;
            application.DefaultVersion = ExcelVersion.Excel2013;

            //A new workbook is created.[Equivalent to creating a new workbook in MS Excel]
            //The new workbook will have 1 worksheet
            IWorkbook workbook = application.Workbooks.Create(1);

            //The first worksheet object in the worksheets collection is accessed.
            IWorksheet sheet = workbook.Worksheets[0];

            sheet.Range["A1"].Text = "Hello World!";

            workbook.Version = ExcelVersion.Excel2013;

            //Saving workbook as stream
            MemoryStream stream = new MemoryStream();
            workbook.SaveAs(stream);

            //Closing workbook
            workbook.Close();

            //Disposing excel engine
            excelEngine.Dispose();


        }


1 Reply

DB Dilli Babu Nandha Gopal Syncfusion Team October 30, 2015 08:32 AM UTC

Hi David


Thank you for contacting Syncfusion support.


In Xamarin.Forms, workbook can be saved as stream using XlsIO. This stream can be saved as an Excel file by invoking Save method of ISave which is a user-defined interface. The following code example illustrates how to save an Excel file from  a stream in Xamarin.Forms.


Code example:

ExcelEngine excelEngine = new ExcelEngine();

IApplication application = excelEngine.Excel;

application.DefaultVersion = ExcelVersion.Excel2013;

string resourcePath = "SampleBrowser.Samples.XlsIO.Template.Sample.xlsx";

Assembly assembly = typeof(App).GetTypeInfo().Assembly;

Stream fileStream = assembly.GetManifestResourceStream(resourcePath);


//Opens the workbook.

IWorkbook workbook = application.Workbooks.Open(fileStream);

MemoryStream stream = new MemoryStream();

workbook.SaveAs(stream);

workbook.Close();

excelEngine.Dispose();


//Save the stream into xlsx file

Xamarin.Forms.DependencyService.Get<ISave>().Save("sample.xlsx","application/msexcel", stream);


public interface ISave

{

Task Save(string filename, string contentType, MemoryStream stream);

}


class SaveAndroid: ISave

{

public async Task Save(string fileName, String contentType, MemoryStream stream)

{

string root = null;

if (Android.OS.Environment.IsExternalStorageEmulated)

{

root = Android.OS.Environment.ExternalStorageDirectory.ToString();

}

else

root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);


Java.IO.File myDir = new Java.IO.File(root + "/Syncfusion");

myDir.Mkdir();


Java.IO.File file = new Java.IO.File(myDir, fileName);


if (file.Exists()) file.Delete();


try

{

FileOutputStream outs = new FileOutputStream(file);

outs.Write(stream.ToArray());


outs.Flush();

outs.Close();

}

catch (Exception e)

{


}

if (file.Exists())

{

Android.Net.Uri path = Android.Net.Uri.FromFile(file);

string extension = Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());

string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);

Intent intent = new Intent(Intent.ActionView);

intent.SetDataAndType(path, mimeType);

Forms.Context.StartActivity(Intent.CreateChooser(intent, "Choose App"));

}

}
}


We will update this information in the documentation.


Regards,

Dilli babu.


Loader.
Live Chat Icon For mobile
Up arrow icon