How to convert Excel worksheet to image in UWP?
Syncfusion® Essential XlsIO is a UWP Excel library used to create, read, and edit Excel documents. Using this library, you can start converting an Excel worksheet to an image in UWP.
Steps to convert an Excel worksheet to an image programmatically:
- Create a new C# Blank App (Universal Windows) project.
- Install the Syncfusion.XlsIORenderer.Net.Core NuGet package as a reference to your .NET Standard application from NuGet.org.
- 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>
- Include the following namespaces in the Mainpage.xaml.cs file.
C#
using Syncfusion.XlsIO; using Windows.Storage.Pickers; using Windows.Storage; using Syncfusion.XlsIORenderer;
- Include the below code snippet in the click event of the button in MainPage.xaml.cs, to convert an Excel file to an image, 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 an Excel worksheet to an image can be downloaded from Worksheet-to-Image.zip.
By executing the program, you will get the Excel file as follows.
Know more about Essential® XlsIO through the documentation, where you can find supported features like Worksheet to Image conversion with respective code examples and prerequisites.
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 a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about How to convert Excel worksheet to image in UWP.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!