Articles in this section
Category / Section

How to convert Excel worksheet to image in UWP?

2 mins read

Syncfusion Essential XlsIO is a UWP Excel library used to create, read, and edit Excel documents. Using this library, you can start converting Excel worksheet to image in UWP.

Steps to convert Excel worksheet to image  programmatically:

  1. Create a new C# Blank App (Universal Windows) project.

Create UWP application

  1. Install the Syncfusion.XlsIORenderer.Net.Core NuGet package as reference to your .NET Standard application from NuGet.org.
  2. Add a new button in the MainPage.xaml as shown below.

CSHTML

<Page
   x:Class="CreateXlsIOSample_UWP.MainPage"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:local="using:CreateXlsIOSample_UWP"
   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   mc:Ignorable="d">
 
   <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
         <Button x:Name="button" Content="Create Document" Click="OnButtonClicked" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
 
</Page>

 

  1. Include the following namespaces in the Mainpage.xaml.cs file.

C#

using Syncfusion.XlsIO;
using Windows.Storage.Pickers;
using Windows.Storage;
using Syncfusion.XlsIORenderer;

 

  1. Include the below code snippet in the click event of the button in MainPage.xaml.cs, to convert an Excel file to image and save the image as a physical file and open the file for viewing.

C#

//Create an instance of ExcelEngine.
using (ExcelEngine excelEngine = new ExcelEngine())
{
    //Initialize Application.
    IApplication application = excelEngine.Excel;
 
    //Set default version for application.
    application.DefaultVersion = ExcelVersion.Xlsx;
 
    Assembly assembly = typeof(MainPage).GetTypeInfo().Assembly;
    string path = "CreateXlsIOSample_UWP.Assets.sample.xlsx";
    Stream stream = assembly.GetManifestResourceStream(path);
                
    //Create a new workbook.
    IWorkbook workbook = application.Workbooks.Open(stream);
 
    IWorksheet worksheet = workbook.Worksheets[1];
    
    // Initialize XlsIORenderer
    application.XlsIORenderer = new XlsIORenderer();
 
    //Create a new memory stream to save the image.
    Stream imagestream = new MemoryStream();
 
    //Convert worksheet to image and save it to stream.
    worksheet.ConvertToImage(worksheet.UsedRange, imagestream);
    SaveImage(imagestream);
}
 
private async void SaveImage(Stream outputStream)
{
   StorageFile stFile;
   if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
   {
      FileSavePicker savePicker = new FileSavePicker();
      savePicker.DefaultFileExtension = ".png";
      savePicker.SuggestedFileName = "Output";
      savePicker.FileTypeChoices.Add("png image", new List<string>() { ".png" });
      stFile = await savePicker.PickSaveFileAsync();
   }
   else
   {
       StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
       stFile = await local.CreateFileAsync("Output.png", CreationCollisionOption.ReplaceExisting);
    }
 
    if (stFile != null)
    {
       Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite);
       Stream st = fileStream.AsStreamForWrite();
       st.SetLength(0);
       st.Write((outputStream as MemoryStream).ToArray(), 0, (int)outputStream.Length);
       st.Flush();
       st.Dispose();
       fileStream.Dispose();
       MessageDialog msgDialog = new MessageDialog("Do you want to view the Document?", "File created.");
       UICommand yesCmd = new UICommand("Yes");
       msgDialog.Commands.Add(yesCmd);
       UICommand noCmd = new UICommand("No");
       msgDialog.Commands.Add(noCmd);
       IUICommand cmd = await msgDialog.ShowAsync();
       if (cmd == yesCmd)
       {
          // Launch the retrieved file
          bool success = await Windows.System.Launcher.LaunchFileAsync(stFile);
        }
    }
}

 

A complete working example of how to convert Excel worksheet to image can be downloaded from Worksheet-to-Image.zip.

By executing the program, you will get the Excel file as follows. 

Worksheet to Image conversion

Know more about Essential XlsIO through the documentation, where you can find supported features like Worksheet to Image conversion with respective code examples and prerequisite.

Refer here to explore the rich set of Syncfusion Essential XlsIO features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied