Convert Excel Worksheet to Image in WPF

2014-11-14 06:05:44 Written by  support iceblue
Rate this item
(0 votes)

Since Excel spreadsheets or diagrams are difficult to distribute, it is reasonable that we frequently convert our Excel files to a web-friendly format, such as image. Plus, if we convert Excel to image, the data cannot be formatted and modified directly. In this article, I’ll introduce how to save each worksheet in an Excel file as an image to local folder in WPF using Spire.XLS for WPF.

The sample file for test contains three worksheets, sheet 1 and sheet 2 have some contents in them. What we need is to convert each worksheet that contains contents to image respectively.

Convert Excel Worksheet to Image in WPF

Detailed Steps:

Step 1: Create a new project by choosing WPF Application in Visual Studio, add a button in MainWindow, double click the button to write code.

Step 2: Create a new instance of Spire.Xls.Workbook class and load the sample Excel file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("sample.xlsx", ExcelVersion.Version2010);

Step 3: Use for sentence to traverse each worksheet in the Excel. Call Worksheet.SaveToImage() to save worksheet as image, set image format as png.

for (int i = 0; i < workbook.Worksheets.Count; i++)
{
    workbook.Worksheets[i].SaveToImage(string.Format("result-{0}.png", i));
}

Result:

Image 1

Convert Excel Worksheet to Image in WPF

Image 2

Convert Excel Worksheet to Image in WPF

Full Code:

using Spire.Xls;

namespace WpfApplication
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("sample.xlsx", ExcelVersion.Version2010);
            for (int i = 0; i < workbook.Worksheets.Count; i++)
            {
                workbook.Worksheets[i].SaveToImage(string.Format("result-{0}.png", i));
            }
        }
    }
}

Additional Info

  • tutorial_title:
Last modified on Friday, 24 September 2021 09:47