Only convert visible or hidden worksheet to image in C#

Spire.XLS supports to convert the whole excel workbook into Image, PDF, HTML and other files formats; it also supports to hide or show the worksheet in C#. When we have hidden worksheet in the excel files, developers can also use Spire.XLS to only convert the visible or hidden excel worksheets to other file formats by judge the property of WorksheetVisibility. This article will show you how to only convert visible or hidden worksheet to image in C#.

Here comes to the steps:

Step 1: Create a new document and load from file.

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

Step 2: Traverse every worksheet in the excel file.

foreach (Spire.Xls.Worksheet ws2 in book.Worksheets)

Step 3: Call the property of WorksheetVisibility to judge visible or hidden excel worksheet.

//only converts the visible sheet
if (ws2.Visibility == WorksheetVisibility.Visible)
//only converts the hidden sheet
if (ws2.Visibility == WorksheetVisibility.Hidden)

Step 4: Use SaveToImage to convert Excel worksheet to Image.

ws2.SaveToImage("result.jpg");

Effective screenshots:

Only convert the visible worksheet to Image in .jpg format.

Only convert the visible worksheet to Image in .jpg format

Only convert the hidden worksheet to Image in .png format.

Only convert the hidden worksheet to Image in .png format

Full codes:

static void Main(string[] args)
{
    Workbook book = new Workbook();
    book.LoadFromFile("sample.xlsx", ExcelVersion.Version2010);

    foreach (Spire.Xls.Worksheet ws2 in book.Worksheets)
    {
        //only convert the visible sheet
        if (ws2.Visibility == WorksheetVisibility.Visible)
            ////only convert the hidden sheet
            //if (ws2.Visibility == WorksheetVisibility.Hidden)
        ws2.SaveToImage("result.jpg");
    }
}