Articles in this section
Category / Section

How to generate and display Excel files in thumbnail view ?

1 min read

This article explains creating thumbnail views of Excel files and open the corresponding excel file using SfSpreadsheet control when clicking the thumbnail image.

Creating thumbnail images from Excel files

You can generate the thumbnail image from Excel file by using XlsIO ‘ConvertToImage()’ worksheet method. Please refer worksheet to image conversion help documentation for more information. You can get the code snippets regarding this below,

 

C#:

DirectoryInfo directory = new DirectoryInfo(@"..\..\ExcelFiles");

 

foreach (var file in directory.GetFiles())

{

   ExcelEngine excelEngine = new ExcelEngine();

   IApplication application = excelEngine.Excel;

   IWorkbook workbook = application.Workbooks.Open(file.FullName);

   IWorksheet worksheet = workbook.Worksheets[0];

   System.Drawing.Image image = worksheet.ConvertToImage(1, 1, worksheet.UsedRange.LastRow + 1, worksheet.UsedRange.LastColumn + 1, ImageType.Bitmap, null);

   image.Save(@"..\..\ExcelFiles\Thumbnails\" +    Path.GetFileNameWithoutExtension(file.Name)+ ".png", ImageFormat.Png);

   workbook.Close();

   excelEngine.Dispose();

}

 

Opening Excel file in SfSpreadsheet control from thumbnail view

Once images generated for corresponding Excel files, we can load the images in thumbnail view. When you click thumbnail image from view, the corresponding excel files will be loaded in SfSpreadsheet control.

C#:

private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

{

    if ((sender as StackPanel).DataContext is Model)

    {

        Model selectedSpreadsheet = (sender as StackPanel).DataContext as Model;

        spreadsheet.Open(@"..\..\ExcelFiles\"+  System.IO.Path.GetFileNameWithoutExtension(selectedSpreadsheet.ImageName) + ".xlsx");

    }

}

 

SfSpreadsheet preview.

We have created sample application which is generate the images from given Excel files and arranged that in thumbnail view along with SfSpreadsheet preview.

 

Sample link: ThumbnailsDemo_WPF

 

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